How to use Log4Net

11 Apr 2014, 02:10 AM

Introduction

Logging is the vital part of any crucial application that helps you to diagnose issues of you live application. It is an intuitive way to make robust application and you can provide good support to your customer. It also helps to reduces development time if used properly.

There are different types of logging frameworks available for the .Net and you can use anyone that you want. Log4Net is very easy to use you can add its reference in your project using Nuget packages. Right click on your project were you want to use Log4Net. Click on “Manage NuGet Packages…” and on the window search for Log4Net and from the searched result list install the package.

It will add the require references in you project. Now you just need to use the logger in you code and you also need to add some configuration in your config file. In this blog we will save log in text file but Log4Net provide different ways to log data in different db, files etc.

Use Log4Net in Your Code

In this blog i created a wrapper class for using Log4net. LoggerService class is only a wrapper class that is using Log4net library for logging.

 public static class LoggerService
 {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger
    (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static void LogInformation(string information)
        {
            log.Info(information);
        }

        public static void LogError(string error)
        {

            log.Error(error);
        }

        public static void LogError(Exception ex)
        {
            log.Error("", ex);
        }

        public static void LogError(string error, Exception ex)
        {
            log.Error(error, ex);
        }

 }

    

Configuration for Log4net

You also need to add some configuration settings for the Log4net to tell him where & how you want to save logging information.

Here I want log file for each day. I configured my appender “FileAppender” and set its type “log4net.Appender.RollingFileAppender”. Then I assigned the pattern in the file and did some other changes.

<configuration>

  <configSections>
    
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

  </configSections>
    
   <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
      </layout>
    </appender>
    <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
      <file  type="log4net.Util.PatternString" value="Log4net/mylogfile_%date{yyyyMMdd}.txt"  />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="FATAL" />
      </filter>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="FileAppender" />
    </root>
  </log4net>

<configuration>
    

We can add multiple appender in the configuration file. But we one use one of them and specify its name in <appender-ref ref="FileAppender" />.

Add Info in AssemblyInfo.cs

You may also need to add the assembly info at the end of the AssemblyInfo.cs file.

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
About Blog
How to use Log4Net to log the your application's activities.

Date 24 Oct 2013, 03:30 PM
Views 2,689
;