Click here to Skip to main content
15,876,783 members
Articles / Web Development / ASP.NET

Configure Log4Net in ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.41/5 (41 votes)
11 Oct 2006CPOL 231.9K   2.6K   65   38
How to configure Log4Net in an ASP.NET 2.0 application.

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:

C#
log4net.Config.XmlConfigurator.Configure();

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

XML
<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
XML
<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
XML
<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:

XML
<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:

C#
//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)


Written By
Architect
Norway Norway
Growing up with the world wide web, witnessing many bubbles and the rise of information technology to becoming an indispensable part of human existence, I was intrigued and astonished by the drastic changes it brought to the way we lived then. I was drawn into this whirlpool of internet and technology as an enthusiast only to come out as a qualified software professional. Worked extensively on technologies, starting with Visual Basic, ASP, Oracle and MS SQL to WCF, .NET Core , Azure service fabric and Kubernetes. Architected applications for on-premises to hybrid clouds to cloud native environments.
Fortunately, I like what I do and work has been fulfilling.
It has been 20+ years of working for various organisations in several roles both technical and managerial but not a
dull day.

Comments and Discussions

 
GeneralThank You! Pin
Priyanka Kale6-Feb-18 22:30
Priyanka Kale6-Feb-18 22:30 
QuestionNeed some info Pin
Tridip Bhattacharjee18-Oct-14 5:51
professionalTridip Bhattacharjee18-Oct-14 5:51 
AnswerRe: Need some info Pin
Vikramaditya S Shekhawat17-Aug-15 1:12
Vikramaditya S Shekhawat17-Aug-15 1:12 
Questionthanks . for the quick article it helped me Pin
rama charan13-Jul-11 21:34
rama charan13-Jul-11 21:34 
QuestionHelped me with PayPal API Medium Trust Issue! Pin
army_man716559-Jul-11 20:47
army_man716559-Jul-11 20:47 
GeneralMy vote of 5 Pin
daylightdj22-Apr-11 4:54
daylightdj22-Apr-11 4:54 
GeneralMy vote of 5 Pin
olehY0920-Feb-11 21:59
olehY0920-Feb-11 21:59 
GeneralMy vote of 5 Pin
Member 23636832-Jan-11 11:16
Member 23636832-Jan-11 11:16 
GeneralMy vote of 4 Pin
Md. Marufuzzaman9-Nov-10 20:17
professionalMd. Marufuzzaman9-Nov-10 20:17 
GeneralMy vote of 1 Pin
Anuj Tripathi5-Aug-10 20:54
Anuj Tripathi5-Aug-10 20:54 
GeneralRe: My vote of 1 Pin
jeremychild10-Aug-11 17:16
jeremychild10-Aug-11 17:16 
GeneralUser Level Logging Pin
RaMuthusamy26-Nov-09 16:11
RaMuthusamy26-Nov-09 16:11 
GeneralBlank Page Pin
dhaval.upadhyay9-Feb-09 1:17
professionaldhaval.upadhyay9-Feb-09 1:17 
Generallog4net is not working with Access and SQL-Server using ASP.NET Pin
Bimal Kothari13-Aug-08 4:33
Bimal Kothari13-Aug-08 4:33 
Generallog4net is not working with Access and SQL-Server Pin
Bimal Kothari13-Aug-08 4:32
Bimal Kothari13-Aug-08 4:32 
GeneralNeed log4net Solution and sample for Access and SQL-Server Pin
Bimal Kothari13-Aug-08 4:30
Bimal Kothari13-Aug-08 4:30 
Hi,

Can I have complete sample to insert log data in to Access and SQL-Server.

I have verified that log4net is not working with Access and SQL-Server using ASP.NET after making all
configuration correct and proper.

Please provide me complete sample if it possible soon.

Thanks,
Bimal
GeneralSecurity Exception Pin
Manojkumar Sharma6-Jun-08 0:49
Manojkumar Sharma6-Jun-08 0:49 
Generalhi Pin
manoj221849-May-08 2:59
manoj221849-May-08 2:59 
GeneralThanks ! Pin
andy_sinclair1-Nov-07 5:49
andy_sinclair1-Nov-07 5:49 
GeneralThank you, it's helpful! Pin
liuruicai11-Oct-07 16:21
liuruicai11-Oct-07 16:21 
Generalblank file Pin
RADman10-Oct-07 4:26
RADman10-Oct-07 4:26 
GeneralWhere should i add the new section Pin
satyamythily26-Jul-07 6:00
satyamythily26-Jul-07 6:00 
GeneralRe: Where should i add the new section [modified] Pin
Noman Mohammad Tee6-Aug-07 6:33
Noman Mohammad Tee6-Aug-07 6:33 
GeneralNice Pin
nsebastian20-Jun-07 11:47
nsebastian20-Jun-07 11:47 
GeneralBest log4net guide I found Pin
reach4thelasers28-May-07 6:57
reach4thelasers28-May-07 6:57 

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

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