简体   繁体   English

如何使用NLog发送电子邮件?

[英]How to send e-mails using NLog?

I'm using NLog, and I'm pretty new at logging. 我正在使用NLog,并且在记录日志方面还很新。 I created the log files but somehow I have a problem whit sending e-mails. 我创建了日志文件,但是无论如何我在发送电子邮件时遇到了问题。 I followed all instructions but couldn't make it. 我遵循了所有指示,但未能成功。

Mail settings in configuration tags in web.config: web.config中配置标签中的邮件设置:

<system.net>
<mailSettings>
  <smtp deliveryMethod="SpecifiedPickupDirectory" from="some@some.org">
    <network host="localhost"/>
    <specifiedPickupDirectory pickupDirectoryLocation="d:\tmp\email"/>
  </smtp>

</mailSettings>
</system.net>

And this is the target inside the nlog.config: 这是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" />

The rule I used: 我使用的规则:

       <logger name="*" minlevel="Error" writeTo="Mail" />

And I called the logger like this: 我这样称呼记录器:

     Logger logger = LogManager.GetCurrentClassLogger();
     try{ //something }
     catch(Exception ex){  logger.Error(ex);  }

And also I'm pretty confused about the places of the settings and configurations. 而且,我对设置和配置的位置也很困惑。 Thank you. 谢谢。

The body is a layout format, not sure if it will process the ${message} tag. 该主体是一种布局格式,不确定是否会处理$ {message}标签。 Change ${message] to a layout name that is in nlog.config or leave it off for the ${message}${newline} default. 将$ {message]更改为nlog.config中的布局名称,或将其保留为$ {message} $ {newline}默认值。

You can turn on the Internal Debugging 您可以打开内部调试

Change the top XML parent to 将顶级XML父级更改为

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

This might give you an idea on the issue. 这可能会让您对此有所了解。 Could be an authentication issue with the server or another issue. 可能是服务器的身份验证问题或其他问题。

Since you are specifying all the server information in the target, you don't need the web.config settings. 由于您要在目标中指定所有服务器信息,因此不需要web.config设置。

Not sure if it is fixed yet, but you should add a timeout="10000" to the target so it closes the connection. 不知道它是否已修复,但是您应该向目标添加timeout =“ 10000”,以便它关闭连接。

There is a pretty good example in the NLog Wiki for GMail that works NLog Wiki中有一个很好的GMail示例,可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM