Click here to Skip to main content
Click here to Skip to main content

Configure Log4Net in ASP.NET 2.0

By , 11 Oct 2006
 

Introduction

Log4net is an Open Source utility used for log/report statements to various kinds of output targets (text file, email, database, event viewer etc.): http://logging.apache.org/. It can be a very handy utility for application instrumentation purposes.

Configuring Log4net

Step 1: Add a reference of Log4net.dll to the project.

Step 2: Add Global.asax to the project, if not already added. In the "Application_Start" event of global.asax, add the following code:

log4net.Config.XmlConfigurator.Configure();

Step 3: In the web.config file, under Configuration->Configsections, add the following section:

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

Step 4: In web.config, add a new section "<log4net>". This section will contain all the settings related to the Log4net configuration.

Step 5: In web.config, under "log4net" section, add the required appenders (output target) configuration sections in the following way:

File appender configuration, used for log statements in a text file
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
  <param name="File" value="Logs\\Log4Net.log"/>
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
  </layout>
</appender>
SMTP appender configuration, used for log statements by sending email
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="" />
<from value="" />
<subject value="" />
<smtpHost value="" />
<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] - %message%newline%newline%newline" />
</layout>
</appender>

Step 6: In the web.config file, under "log4net" section, for each appender, add loggers in the following way:

<logger name="File">
<level value="All" />
<appender-ref ref="LogFileAppender" />
</logger>
<logger name="EmailLog">
<level value="All" />
<appender-ref ref="SmtpAppender" />
</logger>

Step 7: Now, whenever we want to log any information/error/warning, call the appropriate method in the following manner:

//for logging to file
log4net.ILog logger = log4net.LogManager.GetLogger("File");

//for emailing
log4net.ILog logger = log4net.LogManager.GetLogger("EmailLog");

logger.Info("Starting page load");

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Vikramaditya S Shekhawat
Architect
India India
Member
Originally from Jaipur(Rajasthan).
Working in Norway for a Software company.Holding diploma from CDAC and MCSD.NET (C#).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionthanks . for the quick article it helped mememberrama charan13 Jul '11 - 21:34 
QuestionHelped me with PayPal API Medium Trust Issue!memberMember 80371729 Jul '11 - 20:47 
GeneralMy vote of 5memberdaylightdj22 Apr '11 - 4:54 
I'm giving you a 5, because I am using log4net for the first time. I followed the steps outlined in this article and everything worked perfectly. Thanks for taking the time to document.
GeneralMy vote of 5memberoleg_7920 Feb '11 - 21:59 
GeneralMy vote of 5memberMember 23636832 Jan '11 - 11:16 
GeneralMy vote of 4mvpMd. Marufuzzaman9 Nov '10 - 20:17 
GeneralMy vote of 1memberAnuj Tripathi5 Aug '10 - 20:54 
GeneralRe: My vote of 1memberjeremychild10 Aug '11 - 17:16 
GeneralUser Level LoggingmemberMember 367943826 Nov '09 - 16:11 
GeneralBlank Pagememberdhaval.upadhyay9 Feb '09 - 1:17 
Generallog4net is not working with Access and SQL-Server using ASP.NETmemberBimal Kothari13 Aug '08 - 4:33 
Generallog4net is not working with Access and SQL-ServermemberBimal Kothari13 Aug '08 - 4:32 
GeneralNeed log4net Solution and sample for Access and SQL-ServermemberBimal Kothari13 Aug '08 - 4:30 
GeneralSecurity ExceptionmemberManojkumar Sharma6 Jun '08 - 0:49 
Generalhimembermanoj221849 May '08 - 2:59 
GeneralThanks !memberandy_sinclair1 Nov '07 - 5:49 
GeneralThank you, it's helpful!memberliuruicai11 Oct '07 - 16:21 
Generalblank filememberRADman10 Oct '07 - 4:26 
GeneralWhere should i add the new sectionmembersatyamythily26 Jul '07 - 6:00 
GeneralRe: Where should i add the new section [modified]memberNoman Mohammad6 Aug '07 - 6:33 
GeneralNicemembernsebastian20 Jun '07 - 11:47 
GeneralBest log4net guide I foundmemberreach4thelasers28 May '07 - 6:57 
QuestionlogsmemberLeleHalfon8 May '07 - 6:55 
GeneralThank-you!memberGarbage Can10 Dec '06 - 13:00 
GeneralXmlConfiguratorAttributtememberTomO217 Oct '06 - 3:57 
GeneralRe: XmlConfiguratorAttributtememberdnh11 Apr '07 - 4:28 
GeneralRe: XmlConfiguratorAttributtemembertodeti23 Oct '07 - 6:44 
GeneralClosing log filesmemberDejan Grujic16 Oct '06 - 22:29 
GeneralRe: Closing log filesmemberVikramaditya S Shekhawat16 Oct '06 - 22:32 
GeneralDirectory write permissionsmemberRodrigo Dias16 Oct '06 - 1:36 
GeneralRe: Directory write permissionsmemberVikramaditya S Shekhawat16 Oct '06 - 1:39 
GeneralRe: Directory write permissionsmemberKrishanChaminda19 Oct '06 - 14:11 
Generalweb.configmemberVladimir_V13 Oct '06 - 8:05 
GeneralRe: web.configmemberVikramaditya S Shekhawat14 Oct '06 - 21:12 
GeneralRe: web.configmemberdarkrd3 Nov '06 - 17:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 11 Oct 2006
Article Copyright 2006 by Vikramaditya S Shekhawat
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid