Logging made easy in .NET – log 4 net

Logging is the part of development which is normally not included in Product / Software release but at the same time it is necessary to create the logging mechanism in your software module.

implement log4net in .net

Without logging, how you will track

  1. run time errors and exceptions in Websites
  2. Status of Windows service.
  3. Softwares which will run in background.
  4. Crash reports etc.

In this article i am going to explain that how to configure the log 4 net in you .net based applications.

To start with, you have to download the log 4 net from here.

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

code snapshot in configuration file:

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

You can find the description inline in configuration.

For more info, you can visit this site.

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

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

After initialization, We have to configure the log 4 net.

XmlConfigurator.Configure();

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

  • Fatal (Highest Level)
  • Warn
  • Error
  • Debug
  • Info (Lowest Level)
            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"));

Posted

in

,

by

Tags:


Related Posts

Comments

One response to “Logging made easy in .NET – log 4 net”

  1. Mathew Birdsell Avatar
    Mathew Birdsell

    Hey very cool site!! Man .. Beautiful .. Amazing .. I’ll bookmark your web site and take the feeds also…I’m happy to find numerous useful information here in the post, we need work out more strategies in this regard, thanks for sharing. . . . . .

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading