I'm trying to implement log4net to send email.
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
[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 i.e. RollingFileAppender but not for 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:
<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.
Please see my working example. If you use 2-factor authentication with GMail dont forget to generate a password and use it here:
<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}	%-5level	%-15logger	%message%newline" />
</layout>
</appender>
Hope it helps.
For those running into issues with SmtpAppender I would recommend putting the following into your appSettings node.
<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)