Friday, August 6, 2010

Using log4net in ASP.NET for capturing exceptions to both email and a log file

Here is how I am using log4net to capture exceptions to both
a log file and an email:

1.
Created the following method:

private void
LogError(Exception ex)

{

ILog loggerFile = LogManager.GetLogger("File");



ILog loggerEmail = LogManager.GetLogger("EmailLog");


loggerFile.Error("Exception in " +
Request.RawUrl, ex);


loggerEmail.Error("Exception in "
+ Request.RawUrl, ex);


  1. }


    Call this method in the “catch” block
    passing the error object instance:

catch (Exception ex)



{



LogError(ex);



}





3.
Added the following in Global.asax:Application_Start():


void Application_Start(object sender, EventArgs
e)



{




// Code that runs on application startup




log4net.Config.XmlConfigurator.Configure();





}





4.
Added the following <section> in web.config:



<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net
"/>





5

Added
the following section to web.config:

<log4net>



<appender name="LogFileAppender" type="log4net.Appender.FileAppender">



<!-- Please make
shure the ..\\Logs directory exists!
-->



<param name="File" value="Logs\\MyLog4Net.log"/>



<layout type="log4net.Layout.PatternLayout">



<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] -
%message%newline
"/>



</layout>



</appender>



<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">



<to value="someone@whatever.com"/&gt;



<from value="application@whatever.com"/&gt;



<subject value="My Application System Error (Log4Net)"/>



<Authentication value="Basic"></Authentication>



<Username value="user@whatever.com"></Username&gt;



<Password value="password"></Password>



<Port value="25"></Port>



<smtpHost value="SMTP.whatever.com"/>



<bufferSize value="512"/>



<lossy value="false"/>



<Priority value="High"></Priority>



<evaluator type="log4net.Core.LevelEvaluator">



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



<logger name="File">



<level value="DEBUG"/>



<appender-ref ref="LogFileAppender"/>



</logger>



<logger name="EmailLog">



<level value="WARN"/>



<appender-ref ref="SmtpAppender"/>



</logger>



</log4net>







5 comments:

Anonymous said...

Another new post! Just wondering if anybody has any any links or screenshots of
excellent Email Templates used? It's always good to see templates, one more thing to see examples to benchmark as good communication. Will be desperate to see any links.

Also visit my page: Visit The Up Coming Internet Site

Anonymous said...

I will right away clutch your rss feed as I can't find your e-mail subscription link or newsletter service. Do you've any?
Please permit me know in order that I may just subscribe.
Thanks.

my web blog: html email templates free

Anonymous said...

Please let me know if you're looking for a article writer for your weblog. You have some really good posts and I believe I would be a good asset. If you ever want to take some of the load off, I'd absolutely love to write some material for your blog in exchange for a link
back to mine. Please shoot me an email if interested.
Cheers!

my site; free Email html templates

Anonymous said...

Spot on with this write-up, I absolutely believe this amazing site needs a lot more attention.
I'll probably be back again to read more, thanks for the advice!

Take a look at my webpage :: saccharomyces boulardii

Anonymous said...

Hi to all, the contents existing at this web site are really remarkable for people knowledge, well,
keep up the good work fellows.

Visit my web blog :: Email Newsletters Templates