我用 smtpAppender (gmail) 配置了 log4net。奇怪的问题是当我在 IIS 7.5 上部署应用程序时 smtpAppender 不起作用。
我尝试使用 telnet 命令测试与 gmail smtp 的连接。所以我可以检查是否有任何东西被阻止,但测试工作正常。
我的日志配置:
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="C:\logs\MyLog.log"/>
<appendToFile value="true"/>
<maximumFileSize value="500KB"/>
<maxSizeRollBackups value="2"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline"/>
</layout>
</appender>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender,log4net">
<to value="test1@test.com" />
<from value="test2@test.com" />
<subject value="Error logging message" />
<smtpHost value="smtp.gmail.com" />
<port value="587"/>
<authentication value="Basic" />
<username value="test05@gmail.com"/>
<password value="password"/>
<EnableSsl value="true" />
<bufferSize value="1" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator,log4net">
<threshold value="ERROR" />
</evaluator>
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%property{log4net:HostName} :: %level :: %message %newlineLogger: %logger%newlineThread: %thread%newlineDate: %date%newlineNDC: %property{NDC}%newline%newline" />
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFile"/>
<appender-ref ref="SmtpAppender"/>
</root>
</log4net>
调试后发现以下异常:
log4net:ERROR [SmtpAppender] ErrorCode: GenericFailure. Error occurred while sending e-mail notification.
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at log4net.Appender.SmtpAppender.SendEmail(String messageBody)
at log4net.Appender.SmtpAppender.SendBuffer(LoggingEvent[] events)