.net 如何通过电子邮件发送使用 NLog 记录的错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8766263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I email errors logged with NLog?
提问by Keith Beard
I am using NLog for the first time, i figured out how to write to a text file, but now i want to send an email to myself. am making the assumption that when you supply SMTP credentials to NLog. The assembly calls the System.Net.Mail namespace and handles sending an email. If this is wrong please tell me. And if you have done this before i would appreciate any information on what it took you to accomplish send emails.
我第一次使用 NLog,我想出了如何写入文本文件,但现在我想给自己发送一封电子邮件。我假设当您向 NLog 提供 SMTP 凭据时。该程序集调用 System.Net.Mail 命名空间并处理发送电子邮件。如果这是错误的,请告诉我。如果您以前这样做过,我将不胜感激有关您完成发送电子邮件所需的任何信息。
Below is my configuration.
下面是我的配置。
<?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="[email protected]"
from="[email protected]"
Encoding="UTF8"
smtpUsername="[email protected]"
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>
I call the error like so
我这样称呼错误
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
I am trying to get it to send the error, containing message, and the things it normally logs to a file, to my email. I know how to catch exceptions and email it to myself, but i thought NLog had the capabilities to do this. Any tutorials with dead simple examples of using email functionality would work. I found a lot of things but cant get it to work. If you have done this, some sample code or explanation of what else i need to do would help. I cant figure out what it is i am doing wrong. Anyone have any ideas?
我试图让它将包含消息的错误以及它通常记录到文件的内容发送到我的电子邮件。我知道如何捕获异常并将其通过电子邮件发送给自己,但我认为 NLog 有能力做到这一点。任何带有使用电子邮件功能的简单示例的教程都可以使用。我发现了很多东西,但无法让它发挥作用。如果你已经这样做了,一些示例代码或我需要做的其他事情的解释会有所帮助。我不知道我做错了什么。谁有想法?
采纳答案by Richard
Change your encoding from UTF8to UTF-8.
将您的编码从 更改UTF8为UTF-8.
Assuming there are no other errors in your SMTP settings (which is usually the cause of messages not being sent), it should work.
假设您的 SMTP 设置中没有其他错误(这通常是邮件未发送的原因),它应该可以工作。
回答by Petar Vu?etin
I think you need to setup the mailSettingsin the system.net. Something like this:
我认为您需要mailSettings在 system.net 中进行设置。像这样的东西:
<system.net>
<mailSettings>
<smtp from="[email protected]">
<network host="server.net" userName="[email protected]" password="somepassword"/>
</smtp>
<!--Just an example for testing. Can't have both-->
<smtp deliveryMethod="SpecifiedPickupDirectory" from="[email protected]">
<network host="localhost"/>
<specifiedPickupDirectory pickupDirectoryLocation="d:\tmp\email"/>
</smtp>
</mailSettings>
</system.net>
First option is for using the SMTP server and second to deliver email to your local folder. Second option is good for testing.
第一个选项是使用 SMTP 服务器,第二个选项是将电子邮件发送到您的本地文件夹。第二种选择适合测试。

