Home  »  Articles  »  ASP.Net  »

Use log4net to store logging information in your .Net application

 Posted By : Manoranjan SahooPosted Date : 12/10/2010 08:32:14 AM Category : ASP.Net Points : 5 (Rs 1.00)

Hello,

i am going to explain that how to configure the log4net in your .net based applications.

To use log4net, you have to download the log4net from the link below DOWNLOAD HERE

After downloading, you will need to set the configurations file related to Log4net. (web.config in case of webapplication else app.config)

CODE :



<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
 <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <file value="Logs/Notification" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <StaticLogFileName value="false"/>
      <datePattern value="-dd-MMM-yy'.log'" />
      <!--Roll every Day-->
      <maxSizeRollBackups value="-1" />
      <!--Infinite backups-->
      <maximumFileSize value="2MB" />
      <!--Maximum size of 1 file-->
      <layout type="log4net.Layout.PatternLayout">
        <!-- http://logging.apache.org/log4net /release/sdk/log4net.Layout.PatternLayout.html -->
        <conversionPattern value="%date{dd-MMM-yy, HH:mm:ss} - %5level [%thread] - %message%newline " />
      </layout>
    </appender>
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <to value="EMAIL" />
      <from value="EMAIL" />
      <subject value="Error in Component" />
      <smtpHost value="SMTP SERVER" />
      <bufferSize value="512" />
      <lossy value="true" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="FATAL"/>
      </evaluator>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%5level [%thread] [%file]- %message%newline " />
      </layout>
    </appender>
    <root>
      <!-- Ascending Priority: Debug,Info,Warn,Error,Fatal,ALL -->
      <priority value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="SmtpAppender" />
    </root>
  </log4net>



To use log4net in code, after configuration you have to initialize the object of log4net like below line of code.


private static readonly ILog logger = LogManager.GetLogger(typeof(Program));


After initialization you have to configure the log4net.

XmlConfigurator.Configure();


Log Levels :
To log we can decide the log levels:
There are five log levels:

  •    Fatal (Highest Level)
  •    Warn
  •    Error
  •    Debug
  •    Info (Lowest Level)

 

CODE :


XmlConfigurator.Configure();
logger.Info(new Exception("This is test Exception"));
logger.Debug(new Exception("This is test Exception"));
logger.Error(new Exception("This is test Exception"));
logger.Warn(new Exception("This is test Exception"));
logger.Fatal(new Exception("This is test Exception"));



Thank You.

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.