如何使用 NLog 发送电子邮件?
How to send e-mails using NLog?
我正在使用 NLog,而且我对日志记录还很陌生。我创建了日志文件,但不知何故我在发送电子邮件时遇到了问题。我已按照所有说明进行操作,但无法成功。
web.config 中配置标记中的邮件设置:
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory" from="some@some.org">
<network host="localhost"/>
<specifiedPickupDirectory pickupDirectoryLocation="d:\tmp\email"/>
</smtp>
</mailSettings>
</system.net>
这是 nlog.config 中的目标:
<target name="Mail" xsi:type="Mail" html="true" subject="Error Received"
body="${message}"
to="some1@some.com"
from="some@gmail.com"
encoding="UTF-8"
smtpUserName="some@some.com"
enableSsl="false"
smtpPassword="password"
smtpAuthentication="Basic"
smtpServer="smtp.some.com"
smtpPort="25" />
我使用的规则:
<logger name="*" minlevel="Error" writeTo="Mail" />
我这样调用记录器:
Logger logger = LogManager.GetCurrentClassLogger();
try{ //something }
catch(Exception ex){ logger.Error(ex); }
而且我对设置和配置的位置也很困惑。谢谢。
正文是布局格式,不确定是否会处理${message} 标签。将 ${message] 更改为 nlog.config 中的布局名称,或者将其保留为 ${message}${newline} 默认值。
您可以开启 Internal Debugging
将顶部 XML 父级更改为
<nlog internalLogFile="c:\log.txt" internalLogLevel="Trace">
这可能会让您对这个问题有所了解。可能是服务器身份验证问题或其他问题。
由于您在目标中指定了所有服务器信息,因此不需要 web.config 设置。
不确定它是否已修复,但您应该向目标添加 timeout="10000" 以关闭连接。
NLog Wiki 中有一个很好的 GMail 示例
我正在使用 NLog,而且我对日志记录还很陌生。我创建了日志文件,但不知何故我在发送电子邮件时遇到了问题。我已按照所有说明进行操作,但无法成功。
web.config 中配置标记中的邮件设置:
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory" from="some@some.org">
<network host="localhost"/>
<specifiedPickupDirectory pickupDirectoryLocation="d:\tmp\email"/>
</smtp>
</mailSettings>
</system.net>
这是 nlog.config 中的目标:
<target name="Mail" xsi:type="Mail" html="true" subject="Error Received"
body="${message}"
to="some1@some.com"
from="some@gmail.com"
encoding="UTF-8"
smtpUserName="some@some.com"
enableSsl="false"
smtpPassword="password"
smtpAuthentication="Basic"
smtpServer="smtp.some.com"
smtpPort="25" />
我使用的规则:
<logger name="*" minlevel="Error" writeTo="Mail" />
我这样调用记录器:
Logger logger = LogManager.GetCurrentClassLogger();
try{ //something }
catch(Exception ex){ logger.Error(ex); }
而且我对设置和配置的位置也很困惑。谢谢。
正文是布局格式,不确定是否会处理${message} 标签。将 ${message] 更改为 nlog.config 中的布局名称,或者将其保留为 ${message}${newline} 默认值。
您可以开启 Internal Debugging
将顶部 XML 父级更改为
<nlog internalLogFile="c:\log.txt" internalLogLevel="Trace">
这可能会让您对这个问题有所了解。可能是服务器身份验证问题或其他问题。
由于您在目标中指定了所有服务器信息,因此不需要 web.config 设置。
不确定它是否已修复,但您应该向目标添加 timeout="10000" 以关闭连接。
NLog Wiki 中有一个很好的 GMail 示例