Skip to content
October 14, 2008 / kiranpatils

Log4Net – Quickstart guide

Q.What it is?

Ans. It is a logging framework for Microsoft .NET. The initial work was done by NeoWorks and was donated to Apache Software foundation in February 2004.

It is a tool which will help programmers to output a log to a variety of output targets. [Text file, eventLog, database etc.]

Provides Nested Diagnostic Context (NDC) and Mapped Diagnostic Context (MDC).

Log4 net is designed with two main goals: speed and flexibility.


  • Supports for multiple frameworks
  • Output to Multiple Logging Targets
  • Hierarchical logging architecture
  • XML Configuration
  • Dynamic Configuration
  • Logging Context
  • Proven Architecture

Q. Developing Application using Log4NET.

In this demo Application we will see how we can write a log to the text file. This has been configured in Web.config

1) Create a new console Application


2) Add Reference to Log4net.dll [I guess you have downloaded the Log4NET project. If not then please download from here: And you will get bin\net\2.0\ log4net.dll this is the guy whose reference we are going to add]…and ya don’t forget to include using statement using log4net; and using log4net.config;

3) Add Configuration file


4) Now add the following stuff in your configuration file.

<?xml version="1.0" encoding="utf-8" ?> 
<!--Configure your configuration Section Handler which is Log4NetConfigurationSectionHandler--> 
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/> 
<!--Only one root logger element may only be defined and it must be a child of <log4net> 
element. The root logger is the root of the logger hierarchy. All loggers ultimately inherit from this logger.--> 
<!--Root Element has no attributes--> 
<level value="DEBUG" /> 
<!--Optional element, maximum of one allowed. Defines the logging level for this logger.  
This logger will only accept event that are at this level or above.--> 
<appender-ref ref="LogFileAppender" /> 
<!--Zero or more elements allowed. Allows the logger to reference appenders by name.--> 
<!--Appenders may only be defined as child elements of the <log4net> 
element. Each appender must be uniquely named. The implementing type for the appender must be specified.--> 
<!--name = Required attribute. Value must be a string name for this appender.  
The name must be unique among all the appenders defined in this configuration file. This name is used by the <appender-ref> 
element of a Logger to reference an appender.--> 
<!--type = Required attribute. Value must be the type name for this appender.  
If the appender is not defined in the log4net assembly this type name must be fully assembly qualified.--> 
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" > 
<!--RollingFileAppender extends FileAppender to backup the log files when they reach a certain size.--> 
<param name="File" value="C:\log.txt" /> 
<param name="AppendToFile" value="true" /> 
<rollingStyle value="Size" /> 
<maxSizeRollBackups value="10" /> 
<maximumFileSize value="10MB" /> 
<staticLogFileName value="true" /> 
<!-- layout = Optional element, maximum of one allowed. Defines the layout used by this appender.--> 
<layout type="log4net.Layout.PatternLayout"> 
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} - %m%n" /> 
<!--%p - will show the level of message 
%d - will show the date 
%l - will show the class, method and line number where error occurred and  
%m - will show message to display 
See more pattern from Here 

This configuration says that I want to write a log in C:\log.txt file. And it should be appended…Will explore more on each bit and write it down..but for this point of time I don’t think it is required.

5) Now we are ready with configuration part so let’s log something.

Open your program.cs file and declare this variable

/*CREATE A LOGGER - you can create different Logger for each Package/namsepace

* No need to create Logger for each Class.


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

Now under Main let’s use this logger to log


//Write down Error/Debug/Fatal/Information Message

logger.Debug("..This is Debug Message...");

logger.Error("..This is Error Message...");

logger.Fatal("..This is Fatal Message...");

logger.Info("..This is Information Message...");

So, finally your main looks like this:


6) Run the application. Close the console which get opened. And locate c:\log.txt file. Which we have configured on step 4.

7) If you can see the following output then its good time to think about starting using it within your project.





log4net 1.2.10


MS Logging Block And Log4net

Configuration information



Nice resource with all information



Leave a Comment
  1. Paul Solheim / Oct 27 2008 3:32 pm

    Just wanted to add that is a webapplication that you can use to monitor and analyze your log4net output.

    You can

  2. Nur / Jan 15 2009 9:02 am

    Thanks a million brother. I tried other resources but did not succeed your code works as charm.

  3. ajay / Apr 23 2009 9:39 am

    thanks i am looking for this only

  4. shyam / Jan 19 2011 6:06 am

    Hey Hi, it was really a good example to understand and i got the basic idea.
    but can you help me out with something like, i want to log the details like a particular function has been invoked.
    without writing any logging calls in function.
    i want to carryout that stuff with

    thanks in advance

  5. / Apr 14 2013 12:35 pm

    Precisely how much time did it require you to post “Log4Net – Quickstart
    guide A Place for C Sharpers/.Netters”? It contains quite a bit of
    wonderful information. With thanks -Staci

  6. Heather / Nov 12 2013 1:04 am

    Many thanks for taking free time in order to publish “Log4Net – Quickstart guide | A Place for C Sharpers/.
    Netters”. Many thanks again ,Twila


  1. logview4net

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: