Click here to Skip to main content
5,785,816 members and growing! (18,866 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, Visual Studio, ASP.NET, Dev

Posted: 11 Oct 2006
Updated: 11 Oct 2006
Views: 38,886
Bookmarked: 40 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
26 votes for this Article.
Popularity: 5.61 Rating: 3.97 out of 5
3 votes, 11.5%
1
0 votes, 0.0%
2
1 vote, 3.8%
3
8 votes, 30.8%
4
14 votes, 53.8%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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


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
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
Generallog4net is not working with Access and SQL-Server using ASP.NETmemberBimal Kothari5:33 13 Aug '08  
Generallog4net is not working with Access and SQL-ServermemberBimal Kothari5:32 13 Aug '08  
GeneralNeed log4net Solution and sample for Access and SQL-ServermemberBimal Kothari5:30 13 Aug '08  
GeneralSecurity ExceptionmemberManojkumar Sharma1:49 6 Jun '08  
Generalhimembermanoj221843:59 9 May '08  
GeneralThanks !memberandy_sinclair6:49 1 Nov '07  
GeneralThank you, it's helpful!memberliuruicai17:21 11 Oct '07  
Generalblank filememberRADman5:26 10 Oct '07  
GeneralWhere should i add the new sectionmembersatyamythily7:00 26 Jul '07  
GeneralRe: Where should i add the new section [modified]memberNoman Mohammad7:33 6 Aug '07  
GeneralNicemembernsebastian12:47 20 Jun '07  
GeneralBest log4net guide I foundmemberreach4thelasers7:57 28 May '07  
QuestionlogsmemberLeleHalfon7:55 8 May '07  
GeneralThank-you!memberGarbage Can14:00 10 Dec '06  
GeneralXmlConfiguratorAttributtememberTomO24:57 17 Oct '06  
GeneralRe: XmlConfiguratorAttributtememberdnh5:28 11 Apr '07  
GeneralRe: XmlConfiguratorAttributtemembertodeti7:44 23 Oct '07