我用smtpAppender(gmail)配置了log4net。奇怪的問題是,當我在IIS 7.5上部署應用程序時,smtpAppender無法正常工作。SmtpAppender使用Gmail的log4net smtp
我試圖用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="[email protected]" />
<from value="[email protected]" />
<subject value="Error logging message" />
<smtpHost value="smtp.gmail.com" />
<port value="587"/>
<authentication value="Basic" />
<username value="[email protected]"/>
<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)
感謝Claudio。啓用log4net調試模式後。我發現這個異常:System.Net.Mail.SmtpException:SMTP服務器需要安全連接或客戶端未通過身份驗證。服務器響應是:5.5.1需要身份驗證... –