【问题标题】:How to send e-mails using NLog?如何使用 NLog 发送电子邮件?
【发布时间】:2017-08-14 14:43:45
【问题描述】:

我正在使用 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);  }

而且我对设置和配置的位置也很困惑。谢谢。

【问题讨论】:

    标签: c# logging model-view-controller nlog


    【解决方案1】:

    body是一种布局格式,不确定是否会处理${message}标签。将 ${message] 更改为 nlog.config 中的布局名称,或者将其保留为 ${message}${newline} 默认值。

    您可以开启Internal Debugging

    将顶部 XML 父级更改为

    <nlog internalLogFile="c:\log.txt" internalLogLevel="Trace">
    

    这可能会让您对这个问题有所了解。可能是服务器的身份验证问题或其他问题。

    由于您在目标中指定了所有服务器信息,因此不需要 web.config 设置。

    不确定它是否已修复,但您应该向目标添加 timeout="10000" 以便它关闭连接。

    在 NLog Wiki 中有一个很好的例子,用于 GMail 的工作

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 2023-03-18
    • 2018-08-15
    • 2022-01-10
    • 2014-01-28
    • 2015-09-06
    相关资源
    最近更新 更多