34

Currently my application is using log4net to log errors, the web.config for this is as followed:

<log4net>
    <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="../../logs/gateway_%date{yyyyMMdd}.log" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppenderOutput" />
    </root>
  </log4net>

However, the client now wants each error to be emailled to them.

What is the easiest way to do this, can you do it within the web.config file?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
swade1987
  • 1,663
  • 5
  • 21
  • 28
  • Refer this for adding Smtp username password in log4net config https://stackoverflow.com/questions/15523613/log4net-smtp-appender-not-sending-emails – Varun Jan 08 '19 at 10:18

1 Answers1

55

You should use SmtpAppender

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
    <to value="to@example.com" />
    <from value="from@example.com" />
    <subject value="test logging message" />
    <smtpHost value="SMTPServer.example.com" />
    <bufferSize value="512" />
    <lossy value="true" />
    <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="WARN"/>
    </evaluator>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
    </layout>
</appender>

<logger name="ErrorLogger">
    <level value="Error" />
    <appender-ref ref="FileAppender" />
</logger>
<logger name="EmailLogger">
    <level value="Error" />
    <appender-ref ref="SmtpAppender" />
</logger>

In order to send emails only for an specific error you could do something like this

try
{
  // your logic
}
catch (MySpecificException ex)
{
   // I only send emails for exception of type MySpecificException
   LogManager.GetLogger("EmailLogger").Error(ex);
}
catch (Exception ex)
{
   // Just log to a file for the rest
   LogManager.GetLogger("ErrorLogger").Error(ex);
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • you will also need to have access to an smtp server, if you do not have one there are a few good ones available online, just do a google search – pengibot Apr 30 '12 at 14:03
  • however, this would therefore mean 'every' message is sent correct? Is their a way to only email 'certain' errors? – swade1987 Apr 30 '12 at 14:07
  • @swade1987: with this configuration you won't send all errors automatically, you'll have to add a code line to log. Since you have to manually log errors, you can decide which errors to log and which not. – Claudio Redi Apr 30 '12 at 14:10
  • @ClaudioRedi - currently when I log an error I just write "ErrorHandler.LogError(ex, "Gateway - Security - IsNonceSentFromDeviceValid - Failed");" so am I not going to have two types ErrorHandler.LogErrorWithMail() and ErrorHandler.LogError() – swade1987 Apr 30 '12 at 14:25
  • @swade1987: I can't come up at this moment to anything that would solve your specific problem without changing you logging logic. Let you know if I come up with something. – Claudio Redi Apr 30 '12 at 16:23
  • I think the first answer of Claudio was OK. It just needs to be combined with either a string match filter (if that is enough) or some custom filter (that can filter on exception type or whatever is necessary). – Stefan Egli May 02 '12 at 17:58
  • link to smtpappender is 404'ing – user3791372 Oct 17 '16 at 14:20
  • @user3791372: thanks, removed the link. Will try to find a replace for it – Claudio Redi Oct 17 '16 at 14:28