Click here to Skip to main content
11,481,916 members (65,580 online)
Click here to Skip to main content

Configure Log4Net in ASP.NET 2.0

, 11 Oct 2006 CPOL 162.4K 2.2K 66
Rate this:
Please Sign up or sign in to vote.
How to configure Log4Net in an ASP.NET 2.0 application.


Log4net is an Open Source utility used for log/report statements to various kinds of output targets (text file, email, database, event viewer etc.): 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:


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"/>
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"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level 
          %logger [%property] - %message%newline%newline%newline" />

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 name="EmailLog">
<level value="All" />
<appender-ref ref="SmtpAppender" />

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");


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
India India
Originally from Jaipur(Rajasthan).
Working in Norway for a Software company.Holding diploma from CDAC and MCSD.NET (C#).

Comments and Discussions

QuestionNeed some info Pin
Tridip Bhattacharjee18-Oct-14 6:51
memberTridip Bhattacharjee18-Oct-14 6:51 
Questionthanks . for the quick article it helped me Pin
rama charan13-Jul-11 22:34
memberrama charan13-Jul-11 22:34 
QuestionHelped me with PayPal API Medium Trust Issue! Pin
Member 80371729-Jul-11 21:47
memberMember 80371729-Jul-11 21:47 
GeneralMy vote of 5 Pin
daylightdj22-Apr-11 5:54
memberdaylightdj22-Apr-11 5:54 
GeneralMy vote of 5 Pin
oleg_7920-Feb-11 22:59
memberoleg_7920-Feb-11 22:59 
GeneralMy vote of 5 Pin
Member 23636832-Jan-11 12:16
memberMember 23636832-Jan-11 12:16 
GeneralMy vote of 4 Pin
Md. Marufuzzaman9-Nov-10 21:17
mvpMd. Marufuzzaman9-Nov-10 21:17 
Useful work.
GeneralMy vote of 1 Pin
Anuj Tripathi5-Aug-10 21:54
memberAnuj Tripathi5-Aug-10 21:54 
GeneralRe: My vote of 1 Pin
jeremychild10-Aug-11 18:16
memberjeremychild10-Aug-11 18:16 
GeneralUser Level Logging Pin
Member 367943826-Nov-09 17:11
memberMember 367943826-Nov-09 17:11 
GeneralBlank Page Pin
dhaval.upadhyay9-Feb-09 2:17
memberdhaval.upadhyay9-Feb-09 2:17 
GeneralSecurity Exception Pin
Manojkumar Sharma6-Jun-08 1:49
memberManojkumar Sharma6-Jun-08 1:49 
Generalhi Pin
manoj221849-May-08 3:59
membermanoj221849-May-08 3:59 
GeneralThanks ! Pin
andy_sinclair1-Nov-07 6:49
memberandy_sinclair1-Nov-07 6:49 
GeneralThank you, it's helpful! Pin
liuruicai11-Oct-07 17:21
memberliuruicai11-Oct-07 17:21 
Generalblank file Pin
RADman10-Oct-07 5:26
memberRADman10-Oct-07 5:26 
GeneralWhere should i add the new section Pin
satyamythily26-Jul-07 7:00
membersatyamythily26-Jul-07 7:00 
GeneralRe: Where should i add the new section [modified] Pin
Noman Mohammad6-Aug-07 7:33
memberNoman Mohammad6-Aug-07 7:33 
GeneralNice Pin
nsebastian20-Jun-07 12:47
membernsebastian20-Jun-07 12:47 
GeneralBest log4net guide I found Pin
reach4thelasers28-May-07 7:57
memberreach4thelasers28-May-07 7:57 
Questionlogs Pin
LeleHalfon8-May-07 7:55
memberLeleHalfon8-May-07 7:55 
GeneralThank-you! Pin
Garbage Can10-Dec-06 14:00
memberGarbage Can10-Dec-06 14:00 
GeneralXmlConfiguratorAttributte Pin
TomO217-Oct-06 4:57
memberTomO217-Oct-06 4:57 
GeneralRe: XmlConfiguratorAttributte Pin
dnh11-Apr-07 5:28
memberdnh11-Apr-07 5:28 
GeneralRe: XmlConfiguratorAttributte Pin
todeti23-Oct-07 7:44
membertodeti23-Oct-07 7:44 

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.

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