Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / C#
Tip/Trick

Simplest Implementation of log4net

Rate me:
Please Sign up or sign in to vote.
4.60/5 (9 votes)
21 Dec 2012CPOL 75.5K   26   4
Simplest implementation of log4net

Introduction

log4net is a powerful logging framework for .NET. However most people go for their own implementation for logging framework to handle logs. I personally feel it's may be due to lack of simple implementation of sample with log4net.dll.

Using the Code

Simplest implementation of log4net:

Step 1: Create a Central Logging Class

C#
public static class Logger
{
    private static log4net.ILog Log { get; set; }

    static Logger()
    { 
          Log = log4net.LogManager.GetLogger(typeof(Logger));
    }

    public static void Error(object msg)
    {
        Log.Error(msg);
    }

    public static void Error(object msg, Exception ex)
    {
        Log.Error(msg, ex);
    }

    public static void Error(Exception ex)
    {
        Log.Error(ex.Message, ex);
    }

    public static void Info(object msg)
    {
        Log.Info(msg);
    }
}

Step 2: Update the AssemblyInfo.cs File in the Properties folder

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)] 

Step 3: Create a config named log4net.config (as specified in step 2)

XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration> 
    <configSections>
        <section name="log4net" 
               type="log4net.Config.Log4NetConfigurationSectionHandler,
         log4net" />
    </configSections>

    <log4net>  
        <appender name="LogFileAppender" 
                        type="log4net.Appender.RollingFileAppender" >
            <param name="File" value="C:\Temp\Log\log.txt" />
            <param name="AppendToFile" value="true" />
            <rollingStyle value="Size" />
            <maxSizeRollBackups value="2" />
            <maximumFileSize value="10MB" />
            <staticLogFileName value="true" />
            <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
            <layout type="log4net.Layout.PatternLayout">
                  <param name="ConversionPattern" 
                       value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
                  <conversionPattern
                       value="%newline%newline%date %newline%logger 
                       [%property{NDC}] %newline>> %message%newline" />
            </layout>
        </appender>

        <root>
            <level value="ALL" /> 
            <appender-ref ref="LogFileAppender" />                     
        </root>
    </log4net> 
</configuration>

In this config file, have one appender only. Refer to Apache site for more config file samples.

Step 4: Done!!!!!

Step 5: Using it

Since all the logging methods exposed by Logger class are a static, we simply call them to do the logging, like:

C#
try 
{ 
    return 5/0;
} 
catch (Exception ex)
{ 
    Logger.Error(ex);
}

Happy coding!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLog is not getting Created Pin
dheerajindian28-Jun-16 17:55
dheerajindian28-Jun-16 17:55 
QuestionLog is not writing on a file Pin
maheshmaddi26-Dec-13 22:46
maheshmaddi26-Dec-13 22:46 
Log is not writing on a file
AnswerRe: Log is not writing on a file Pin
RijuKK4-Feb-14 19:32
RijuKK4-Feb-14 19:32 
GeneralMy vote of 4 Pin
Klaus Luedenscheidt21-Dec-12 19:16
Klaus Luedenscheidt21-Dec-12 19:16 

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.