简体   繁体   English

log4net smtp附加程序未发送电子邮件

[英]log4net smtp appender not sending emails

I'm trying to implement log4net to send email. 我正在尝试实现log4net来发送电子邮件。
The following is my code but it's not sending emails. 以下是我的代码,但未发送电子邮件。

 <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  <to value="...." />
  <from value="..." />
  <subject value="Logging Message" />
  <smtpHost value="smtp.gmail.com" />
  <port value="465"/>
  <authentication value="Basic" />
  <username value="..."/>
  <password value="..."/>
  <EnableSsl value="true" />
  <bufferSize value="1" />
  <lossy value="true" />
  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="WARN"/>
  </evaluator>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception" />
  </layout>
</appender>

and

<root>
  <level value="WARN" />
  <appender-ref ref="SmtpAppender" />
</root>

in the AssemblyInfo.cs 在AssemblyInfo.cs中

 [assembly: log4net.Config.XmlConfiguratorAttribute(Watch = true)]

and that's how I create the log object 这就是我创建日志对象的方式

  private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

This configuration is working fine for file output ie RollingFileAppender but not for SmtpAppender. 此配置对于文件输出(即RollingFileAppender)工作正常,但不适用于SmtpAppender。

N i have tried many solutions from the internet but were not really helpful. 我没有尝试过互联网上的许多解决方案,但并没有真正的帮助。

Please show me the right directions. 请告诉我正确的方向。 thankx in advance :) 提前感谢:)

I'm using a very similar appender for SMTP messages to Gmail, but in my case I use a different port: 我使用与Gmail SMTP邮件非常类似的附加程序,但在我的情况下,我使用了不同的端口:

<port value="587"/>

All the other settings are the same, so give that a try and see if it works for you. 所有其他设置都相同,因此请尝试一下,看看它是否对您有用。 It's the port Gmail uses for TLS, referenced here . 这是Gmail用于TLS的端口, 在此引用

For those running into issues with SmtpAppender I would recommend putting the following into your appSettings node. 对于那些遇到SmtpAppender问题的人,我建议将以下内容放入您的appSettings节点中。

<appSettings>
  <add key="log4net.Internal.Debug" value="true"/>
</appSettings>

It will then output diagnostics such as this below to point you in the right direction 然后它将在下面输出诸如此类的诊断信息,以指示您正确的方向

log4net: Setting Property [From] to String value [tomas@kodi.is]
log4net: Setting Property [Subject] to String value [Kodiak OMS Shortcode service]
log4net: Setting Property [SmtpHost] to String value [mail.sip.is]
log4net: Setting Property [Port] to Int32 value [25]
log4net: Setting Property [BufferSize] to Int32 value [1]
log4net: Setting Property [EnableSsl] to Boolean value [True]
log4net: Setting Property [Threshold] to Level value [DEBUG]
log4net: Setting Property [Lossy] to Boolean value [False]
log4net: Converter [message] Option [] Format  [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [ConversionPattern] to String value [%utcdate [%level] - %message%newline%exception]
log4net: Converter [utcdate] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [level] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [] - ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [exception] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: Created Appender [EmailLog]
log4net: Adding appender named [EmailLog] to logger [EmailLogger].
log4net: Hierarchy Threshold []
log4net:ERROR [SmtpAppender] ErrorCode: GenericFailure. Error occurred while sending e-mail notification.
System.Net.Mail.SmtpException: Server does not support secure connections.
   at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at log4net.Appender.SmtpAppender.SendEmail(String messageBody)
   at log4net.Appender.SmtpAppender.SendBuffer(LoggingEvent[] events)

Please see my working example. 请参阅我的工作示例。 If you use 2-factor authentication with GMail dont forget to generate a password and use it here: 如果您对GMail使用2要素身份验证,请不要忘记生成密码并在此处使用它:

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  <to value="****" />
  <from value="****" />
  <subject value="Crash log" />
  <smtpHost value="smtp.gmail.com" />
  <authentication value="Basic" />
  <port value="587" />
  <username value="****" />
  <password value="****" />
  <bufferSize value="10" />
  <EnableSsl value="true"/>
  <lossy value="true" />
  <threshold value="DEBUG" />
  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="WARN"/>
  </evaluator>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{dd/MM/yyyy hh:mm:ss.fff}&#9;%-5level&#9;%-15logger&#9;%message%newline" />
  </layout>
</appender>

Hope it helps. 希望能帮助到你。

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

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