Send Exceptions or Error in Email using Log4Net

In web.config, under “log4net” section, add required appenders (output target) configuration sections as below


< appender name="SmtpAppender" type="log4net.Appender.SmtpAppender,log4net">
< to value="to@domain.com" />
< from value="from@domain.com" />
< subject value="test logging message" />
< smtpHost value="SMTPServer.domain.com" />
< bufferSize value="512" />
< lossy value="false" />
< evaluator type="log4net.Core.LevelEvaluator,log4net">
< threshold value="WARN" />
< /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>


for SMTPAppender, add loggers as below
< logger name="EmailLogger"> 
< level value="All" />
< appender-ref ref="SmtpAppender" />
< /logger>


When logging, get the emailLogger form the LogManager
log4net.ILog logger = log4net.LogManager.GetLogger("EmailLog");


Log at appropriate level


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: