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.

Features

  • 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

clip_image002

2) Add Reference to Log4net.dll [I guess you have downloaded the Log4NET project. If not then please download from here: http://logging.apache.org/log4net/download.html. 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

clip_image004

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

 
<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
<!--Configure your configuration Section Handler which is Log4NetConfigurationSectionHandler--> 
<configSections> 
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/> 
</configSections> 
<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> 
<!--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.--> 
</root> 
<!--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 http://www.beefycode.com/post/Log4Net-Tutorial-pt-4-Layouts-and-Patterns.aspx 
--> 
</layout> 
</appender> 
</log4net> 
</configuration> 

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


XmlConfigurator.Configure();

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

clip_image006

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.

clip_image008

Webliography

http://en.wikipedia.org/wiki/Apache_Log4net

Wiki

http://logging.apache.org/log4net/release/features.html

Features

http://logging.apache.org/log4net/download.html

log4net 1.2.10

http://www.codeproject.com/KB/trace/log4net_intro.aspx

Log4J

http://safari.oreilly.com/0321228103/part01#X2ludGVybmFsX1RvYz94bWxpZD0wMzIxMjI4MTAzL2NoMDhsZXYxc2VjMTQ=

http://weblogs.asp.net/lorenh/archive/2005/02/18/376191.aspx

MS Logging Block And Log4net

http://logging.apache.org/log4net/release/manual/configuration.html

Configuration information

http://www.codeproject.com/KB/trace/Logger_using_log4net.aspx

Quickstart

http://www.beefycode.com/post/Log4Net-Tutorial-pt-4-Layouts-and-Patterns.aspx

http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html

Patterns

http://www.beefycode.com/category/log4net.aspx

Nice resource with all information

7 Comments

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 spring.net.

    thanks in advance

  5. http://Tinyurl.com / 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

Trackbacks

  1. logview4net

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: