log4net的SMTP附加器不发送电子邮件(log4net smtp appender not s

2019-07-22 03:21发布

我想实现的log4net发送电子邮件。
下面是我的代码,但它不是发送电子邮件。

 <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>

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

在AssemblyInfo.cs中

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

这就是我如何创建日志对象

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

此配置文件输出即RollingFileAppender进行而不是SmtpAppender工作的罚款。

N I试图从互联网上很多解决方案,但并没有真正的帮助。

请告诉我正确的方向。 thankx提前:)

Answer 1:

我使用SMTP邮件给Gmail一个非常类似的附加目的地,但对我来说我使用不同的端口:

<port value="587"/>

所有其他设置都是一样的,所以给一个尝试,看看它是否适合你。 这对TLS端口Gmail使用, 在这里引用 。



Answer 2:

请参阅我的工作示例。 如果您使用Gmail的双因素身份验证,不要忘记在这里生成一个密码,并使用它:

<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>

希望能帮助到你。



Answer 3:

对于运行遇到与SmtpAppender我建议把以下到您的appSettings节点。

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

然后它会输出诊断,比如下面这点你在正确的方向

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)


文章来源: log4net smtp appender not sending emails