I configured log4net with a smtpAppender (gmail). The weird issue is that the smtpAppender is not working when I deploy the application on IIS 7.5.
I tried to test the connection with the gmail smtp using telnet command. So I can check if anything is blocked but the test worked fine.
My log config:
<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>
After debugging I found the below exception:
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)
Although this is not a direct answer to your question, I believe it could help you to detect the problem. Log4net fails in silence and this is by design. In order to get information about what's wrong you could enable log4net debugging
Check How do I enable log4net internal debugging? on log4net FAQS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With