如何通过电子邮件发送NLog记录的错误?

Kei*_*ard 34 .net vb.net email nlog

我第一次使用NLog,我想出了如何写入文本文件,但现在我想给自己发一封电子邮件.我假设您向NLog提供SMTP凭据.程序集调用System.Net.Mail命名空间并处理发送电子邮件.如果这是错的,请告诉我.如果您已经完成此操作之前我会感谢任何有关您完成发送电子邮件的信息.

以下是我的配置.

    <?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <targets>
    <!--<target name="logfile" xsi:type="File" fileName="C:\Users\keithb\Desktop\TestLog.txt" />-->
    <target name="Mail" xsi:type="Mail" html="true" subject="Error Received" body="${message}"         
         to="user2@someemaill.com"
         from="user@someemail.com"
         Encoding="UTF8"
         smtpUsername="user@someemail.com"
         enableSsl="False"
         smtpPassword="pa$$word"
         smtpAuthentication="Basic"
         smtpServer="mail.someemail.com"
         smtpPort="25" />
  </targets>
  <rules>
    <!--<logger name="*" minlevel="Debug" writeTo="logfile" />-->
    <logger name="*" level="Error" writeTo="Mail" />
    <logger name="*" level="Fatal" writeTo="Mail" />
  </rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)

我这样称呼错误

Imports NLog

Public Class HandleGetRouteInfo
    Private Shared logger As Logger = LogManager.GetCurrentClassLogger()

    Public Shared Function GetRouteInfo(ByVal routeInfo As RequestGetRouteInfo) As GetRouteInfoResponse
        'TODO: Check route ID, username, password
        logger.Fatal("User" & routeInfo.UserName & "has entered the GetRouteInfo Method.")
        logger.Error("User" & routeInfo.UserName & "has entered the GetRouteInfo Method.")
        Dim str As String = logger.Name
End Function
End Class
Run Code Online (Sandbox Code Playgroud)

我试图让它将错误,包含消息,以及它通常记录到文件的内容发送到我的电子邮件.我知道如何捕获异常并通过电子邮件发送给自己,但我认为NLog有能力做到这一点.任何使用电子邮件功能的简单示例的教程都可以使用.我找到了很多东西,但无法让它发挥作用.如果您已经这样做了,我需要做的一些示例代码或解释将有所帮助.我无法弄清楚我做错了什么.有人有主意吗?

Ric*_*ard 21

将编码从更改UTF8UTF-8.

假设您的SMTP设置中没有其他错误(这通常是消息未被发送的原因),它应该可以工作.