log4net smtp appender not sending emails
Asked Answered
P

3

12

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

Postliminy answered 20/3, 2013 at 12:30 Comment(6)
what does the SMTP settings look like in your config file can you show that portion of the config you need SMTP.AppenderGrimaud
"Not sending emails" is really, really vague. The problem could be in lots of places. Your first step is to use an SMTP host you can control. This would narrow down the problem to whether or not it's using the appender vs. whether or not the SMTP host is rejecting the messages. (Which is a very big difference.) Try using something like smtp4dev (smtp4dev.codeplex.com) as a local SMTP host to test if the logger is at least doing what you think it's doing.Sepulcher
As far as I remember, Gmail supports SMTP only over encrypted connection. And I doubt that plain stmp appender can establish it.Hadhramaut
I just tested my answer using my own gmail account and it works using log4net Appender C# .NET 4.0 VS2010 so not sure why my answer did not help you. I marked my answer for deletion good luck also need to see what your code behind looks like in regards to how you are sending over your Credentials ..Grimaud
Your problem also looks like you are not getting the proper name of the logger for example var log = LogManager.GetLogger("SMTPAppender"); then you will need to do this as well to read the configuration log4net.Config.XmlConfigurator.Configure();Grimaud
In my case, I need to set Allow apps that use less secure sign in to gmail, see https://mcmap.net/q/455321/-smtpappender-log4net-using-gmail-smtpGavel
S
10

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.

Sepulcher answered 20/3, 2013 at 12:36 Comment(0)
L
1

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 [[email protected]]
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)
Lenity answered 19/12, 2017 at 9:58 Comment(2)
I have same issue... where do you see this... output diagnosticsDiderot
@Diderot In Visual Studio Output windowCallable
M
0

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}&#9;%-5level&#9;%-15logger&#9;%message%newline" />
  </layout>
</appender>

Hope it helps.

Musaceous answered 6/2, 2014 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.