【发布时间】:2012-01-07 00:32:16
【问题描述】:
我第一次使用 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>
我这样称呼错误
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
我正试图让它将错误、包含消息以及它通常记录到文件的内容发送到我的电子邮件。我知道如何捕获异常并将其通过电子邮件发送给自己,但我认为 NLog 有能力做到这一点。任何包含使用电子邮件功能的简单示例的教程都可以使用。我发现了很多东西,但无法让它发挥作用。如果你已经这样做了,一些示例代码或我需要做的其他事情的解释会有所帮助。我无法弄清楚我做错了什么。有人有什么想法吗?
【问题讨论】: