Click here to Skip to main content
6,822,123 members and growing! (18,612 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Configure Log4Net in ASP.NET 2.0

By Vikramaditya S Shekhawat

How to configure Log4Net in Asp.Net 2.0 application
Windows, .NET, ASP.NET, Visual-Studio, Dev
Posted:11 Oct 2006
Views:67,986
Bookmarked:52 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
29 votes for this article.
Popularity: 5.78 Rating: 3.95 out of 5
3 votes, 10.3%
1

2
2 votes, 6.9%
3
9 votes, 31.0%
4
15 votes, 51.7%
5

Introduction

Download Sample web.Config - 1.2 Kb

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

 

 

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 following code:

 

log4net.Config.XmlConfigurator.Configure();

 

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

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

 

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

 

Step 5: In web.config, under �log4net� section, add 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 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 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Vikramaditya S Shekhawat


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

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh)FirstPrevNext
GeneralUser Level Logging PinmemberMember 367943817:11 26 Nov '09  
GeneralBlank Page Pinmemberdhaval.upadhyay2:17 9 Feb '09  
Generallog4net is not working with Access and SQL-Server using ASP.NET PinmemberBimal Kothari5:33 13 Aug '08  
Generallog4net is not working with Access and SQL-Server PinmemberBimal Kothari5:32 13 Aug '08  
GeneralNeed log4net Solution and sample for Access and SQL-Server PinmemberBimal Kothari5:30 13 Aug '08  
GeneralSecurity Exception PinmemberManojkumar Sharma1:49 6 Jun '08  
Generalhi Pinmembermanoj221843:59 9 May '08  
GeneralThanks ! Pinmemberandy_sinclair6:49 1 Nov '07  
GeneralThank you, it's helpful! Pinmemberliuruicai17:21 11 Oct '07  
Generalblank file PinmemberRADman5:26 10 Oct '07  
GeneralWhere should i add the new section Pinmembersatyamythily7:00 26 Jul '07  
GeneralRe: Where should i add the new section [modified] PinmemberNoman Mohammad7:33 6 Aug '07  
GeneralNice Pinmembernsebastian12:47 20 Jun '07  
GeneralBest log4net guide I found Pinmemberreach4thelasers7:57 28 May '07  
Questionlogs PinmemberLeleHalfon7:55 8 May '07  
GeneralThank-you! PinmemberGarbage Can14:00 10 Dec '06  
GeneralXmlConfiguratorAttributte PinmemberTomO24:57 17 Oct '06  
GeneralRe: XmlConfiguratorAttributte Pinmemberdnh5:28 11 Apr '07  
GeneralRe: XmlConfiguratorAttributte Pinmembertodeti7:44 23 Oct '07  
GeneralClosing log files PinmemberDejan Grujic23:29 16 Oct '06  
GeneralRe: Closing log files PinmemberVikramaditya S Shekhawat23:32 16 Oct '06  
GeneralDirectory write permissions PinmemberRodrigo Dias2:36 16 Oct '06  
GeneralRe: Directory write permissions PinmemberVikramaditya S Shekhawat2:39 16 Oct '06  
GeneralRe: Directory write permissions PinmemberKrishanChaminda15:11 19 Oct '06  
Generalweb.config PinmemberVladimir_V9:05 13 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 11 Oct 2006
Editor:
Copyright 2006 by Vikramaditya S Shekhawat
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project