Click here to Skip to main content
Licence CPOL
First Posted 11 Oct 2006
Views 116,597
Downloads 1,094
Bookmarked 60 times

Configure Log4Net in ASP.NET 2.0

By Vikramaditya S Shekhawat | 11 Oct 2006
How to configure Log4Net in an ASP.NET 2.0 application.
4 votes, 11.1%
1

2
2 votes, 5.6%
3
10 votes, 27.8%
4
20 votes, 55.6%
5
4.41/5 - 36 votes
4 removed
μ 3.99, σa 2.25 [?]

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionthanks . for the quick article it helped me Pinmemberrama charan22:34 13 Jul '11  
QuestionHelped me with PayPal API Medium Trust Issue! PinmemberMember 803717221:47 9 Jul '11  
GeneralMy vote of 5 Pinmemberdaylightdj5:54 22 Apr '11  
GeneralMy vote of 5 Pinmemberoleg_7922:59 20 Feb '11  
GeneralMy vote of 5 PinmemberMember 236368312:16 2 Jan '11  
GeneralMy vote of 4 PinmvpMd. Marufuzzaman21:17 9 Nov '10  
GeneralMy vote of 1 PinmemberAnuj Tripathi21:54 5 Aug '10  
GeneralRe: My vote of 1 Pinmemberjeremychild18:16 10 Aug '11  
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  
I am facing security exception in asp.net 2.0. Below is the error
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
 
Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
 
Source Error:
 

Line 5: void Application_Start(object sender, EventArgs e)
Line 6: {
Line 7: log4net.Config.XmlConfigurator.Configure();
Line 8:
Line 9: }

Can anyone pls guide?
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  

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

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

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