Skip to main content
Email Password   helpLost your password?

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

           

 

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralBlank Page Pin
dhaval.upadhyay
2:17 9 Feb '09  
Generallog4net is not working with Access and SQL-Server using ASP.NET Pin
Bimal Kothari
5:33 13 Aug '08  
Generallog4net is not working with Access and SQL-Server Pin
Bimal Kothari
5:32 13 Aug '08  
GeneralNeed log4net Solution and sample for Access and SQL-Server Pin
Bimal Kothari
5:30 13 Aug '08  
GeneralSecurity Exception Pin
Manojkumar Sharma
1:49 6 Jun '08  
Generalhi Pin
manoj22184
3:59 9 May '08  
GeneralThanks ! Pin
andy_sinclair
6:49 1 Nov '07  
GeneralThank you, it's helpful! Pin
liuruicai
17:21 11 Oct '07  
Generalblank file Pin
RADman
5:26 10 Oct '07  
GeneralWhere should i add the new section Pin
satyamythily
7:00 26 Jul '07  
GeneralRe: Where should i add the new section [modified] Pin
Noman Mohammad
7:33 6 Aug '07  
GeneralNice Pin
nsebastian
12:47 20 Jun '07  
GeneralBest log4net guide I found Pin
reach4thelasers
7:57 28 May '07  
Questionlogs Pin
LeleHalfon
7:55 8 May '07  
GeneralThank-you! Pin
Garbage Can
14:00 10 Dec '06  
GeneralXmlConfiguratorAttributte Pin
TomO2
4:57 17 Oct '06  
GeneralRe: XmlConfiguratorAttributte Pin
dnh
5:28 11 Apr '07  
GeneralRe: XmlConfiguratorAttributte Pin
todeti
7:44 23 Oct '07  
GeneralClosing log files Pin
Dejan Grujic
23:29 16 Oct '06  
GeneralRe: Closing log files Pin
Vikramaditya S Shekhawat
23:32 16 Oct '06  
GeneralDirectory write permissions Pin
Rodrigo Dias
2:36 16 Oct '06  
GeneralRe: Directory write permissions Pin
Vikramaditya S Shekhawat
2:39 16 Oct '06  
GeneralRe: Directory write permissions Pin
KrishanChaminda
15:11 19 Oct '06  
Generalweb.config Pin
Vladimir_V
9:05 13 Oct '06  
GeneralRe: web.config Pin
Vikramaditya S Shekhawat
22:12 14 Oct '06  


Last Updated 11 Oct 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009