Using Apache's log4net in ASP.NET
page 1 of 4
Published: 07 Dec 2004
Unedited - Community Contributed
In this simple, seven-step guide, Anubhav (Ash) Srivastava shows how to use Apache's log4net component to add logging capabilities to your ASP.NET applications.
by Anubhav(Ash) Srivastava
Average Rating: 
Views (Total / Last 10 Days): 34863/ 62


[Download Code]

Log4net is a .NET component from Apache Foundation that provides a simple mechanism for logging messages. Log4net provides seven levels of logging, which are listed below from lowest to highest priority:

1. ALL
7. OFF

These levels help control the output to log files. If in the configuration file the level for a class is set to ERROR, then all messages from that class at a level below ERROR (i.e., DEBUG, INFO or WARN) will not be logged. This will come in very handy when the application goes into production. If you find yourself a little confused, do not worry. Let's proceed with a sample application and you will soon be a log4net pro.

View Entire Article

User Comments

Title: Mr   
Name: Nagaraj
Date: 2011-01-24 11:24:01 PM
Great.. and simple example. Good Work
Title: How to read ConversionPattern?   
Name: Nash
Date: 2010-02-18 2:18:23 AM

Can you please tell me how to read and then parse the conversionPattern from log file.

Title: Excelent Artical   
Name: Mallik
Date: 2009-07-02 6:15:27 AM
This is superb article for Log4Net, I want to know about log4net more and more, can you please give me some URLs or send meterials on Log4Net? My mail id is

Title: Great Article   
Name: Mahanth
Date: 2008-06-19 12:08:36 PM
This is a very Great article to get a complete knowledge of log4net.
Thanks for the article.
Title: missing step in application   
Name: a
Date: 2008-02-06 4:15:02 AM
The one part missing in the application is a call to log4net.Config.XmlConfigurator.Configure();

before calling GetLogger
log = LogManager.GetLogger("WebForm1");
Title: no logging bwing done   
Name: Priya Hurry
Date: 2007-12-25 5:08:08 AM
Hello...I have done exactly what you have been doing...I have tried with the sample application as well...but not log is being changed...
When I debug the application, I can see that the isDebugEnabled,isInfoEnabled,isWarnEnabled,isErrorEnabled,isFatalEnabled is false....Do u think that this is the problem??

Pls reply me on
Title: Untitled   
Name: Harry
Date: 2007-08-17 8:56:59 PM
Why is this Dick Author not responding to any of the Questions posted by Readers ??
Title: Hello this is chary, I have one question   
Name: Rameswara chary
Date: 2007-08-16 9:27:37 AM
Hello Srivastava,
Its good, but i have one question. That is can't we have a global logger which can be declared in Global.asax.cs. That can be used throught the Application.Instead declaring in each and every class.

Title: question   
Name: brucenan
Date: 2007-08-09 10:51:04 PM
If I put the log4net into lower layer component, and use it in ASP.Net project, why I can't generate the log file?
Title: Nice article, great explanation   
Name: pradeep
Date: 2007-08-03 6:41:18 AM
This is a great article, and proved really helpful.
Title: log4Net Extension   
Name: prapro
Date: 2007-06-11 3:02:21 AM
Really a good article on Log4Net. what will be the best way to have the EventID in the event viewer.

Please provide a sample example.


Title: Worked for me   
Name: Andrew Young
Date: 2007-04-11 8:12:45 AM
Hi Anubhav,

I was looking for some guidance on setting up the log4net & your article got me going in no time. :)
Thanks so much for this saved me a lot of time.

Best wishes
Andrew Young
Title: Out of date   
Name: KG
Date: 2006-12-05 1:44:31 PM
I can't get this to work. I'm using VS2005 and ASP.NET 2.0. I think maybe this example is out of date?
Title: thankU   
Name: Bobby
Date: 2006-12-03 12:42:19 PM
thanks for UR Article, this hwlped me a little in learning log4net

please send other urls of log4net like this articles if Possible
Title: Very good   
Name: Viklas
Date: 2006-11-22 7:09:32 AM
Nice and straight to the point.
Title: DotnetSpace simple log4net example   
Name: DotnetSpace simple log4net example
Date: 2006-10-26 2:02:39 PM
This is a very practical guide on how to start using log4net quickly:
Title: Great ... simple yet informative   
Name: shahar shelly
Date: 2006-09-26 8:55:14 AM
This is great info presented in simple and easy to understand way.I like it and hope it will be useful for many
it sure got me started...
Title: Incomlpete in one sense   
Name: Aidit
Date: 2006-08-04 5:31:15 AM
this is a beautiful article and will definately give a heads up to many people but you forgot to talk about logging in a database.
you should have some info around that too.
sorry dude u misssed to complete it.
Title: Need help   
Name: Raj
Date: 2006-07-11 8:54:44 AM
Thanks for this helpful article.I am trying to add a new config named as log4Net.Config. when i am trying to write this line

"[assembly: log4net.Config.DOMConfigurator(ConfigFile="filename",ConfigFileExtension="log4net",Watch=true)]"
It is giving error like ConfigFile not declared ,ConfigFileExtension not declared.
as u said to write the code in same web cofig That's is working fine.
Can u guide for this?
Thanks in advance
Title: Log4net   
Name: J. Pinky
Date: 2006-07-03 4:33:36 AM
A good comprehensive explanation for a beginner to log4net
Title: Helpful tips   
Name: Ryan
Date: 2006-06-01 5:39:18 PM
Title: Thanks   
Name: Ryan
Date: 2006-06-01 5:36:22 PM
I found this article very helpful,
Title: Very Helpful   
Name: Greg
Date: 2006-05-06 1:02:09 AM
A very helpful article. I've used log4net in desktop apps for years, but have just now started using it to debug my ASP.NET apps.

For some reason, in my ASP.NET app I had to make an additional call to DOMConfigurator.Configure() after the LogManager.GetLogger() call, even though I placed the attribute in the AssemblyInfo.cs file.
Title: Visual Studio 2005 + C# + log4net + .NET 2.0 + ASPs   
Name: Joao Lopes
Date: 2006-03-01 11:00:52 AM
There's a full example on how to this at the following url:

The example covers Visual Studio 2005 + C# + log4net + .NET 2.0 + ASPs.
Title: exelent explanation   
Name: harshada
Date: 2006-02-28 5:33:25 AM
thax anubhav for giving us the info. about log4net in the easiest manner.
Title: Reading from web.config   
Name: sridhar kidambi
Date: 2005-11-28 8:05:36 AM
i want to read the file name(log) from web.config.
Title: Doesn't Scale   
Name: Jay R. Wren
Date: 2005-11-11 9:04:09 AM
This is great for debugging and this information does fill an excellent introductory configuration gap for log4net.

Unfortunately it doesn't scale. The log4net logger is based on a singleton and singletons are only unique(for lack of a better word) within a single app domain. As soon as worker processes are increased or utilize another app domain, logging will fail (or be incomplete).

The only solution which I am aware of is to use something other than a file. Use the EventLog or Database appenders. If you really want a file appender, you would need to write some kind of logging server and use remoting or some other method to communicate to the server, the messages to be logged.
Title: Great Information   
Name: Dipan Roy
Date: 2005-11-11 5:49:14 AM
This is great information presented in simple and easy to understand way.I like it and hope it will be usage for many.
Title: log file not created   
Name: sridhar kidambi
Date: 2005-11-09 4:31:02 AM
i did the same way as given in the article but, i could not get anything written on log file .Is there anything wrong
Title: Re: how to view??   
Name: Ash
Date: 2005-11-02 6:44:10 PM
>how cani view the log file i created? i there a tool to >view it?

My friend the log is a text file...u can simple open it with notepad. There is no tool in log4net to view your logs that i am familiar with. You still have to go through that 2GB of log:)Hope this answers your question

Title: how to view??   
Name: itay bar
Date: 2005-11-02 2:38:41 PM
how cani view the log file i created? i there a tool to view it?
Title: Awesome stuff!   
Name: Piyush Arya
Date: 2005-07-12 5:50:43 AM
This is precisely what I had been looking for. I had been trying to get hold of some sample guide that would demonstrate the bare essentials. Found it lacking in the log4net documentation.
Great job Ash dude!
Title: Why not working?   
Name: Leo
Date: 2005-06-20 1:26:27 PM
Hi, Ash,

Thanks for put together this clear instruction for how to use log4net.

I am not sure why the code I downloaded from this site is not working. These are what I have done:

1. I set a virtual directory: LogHelloWorld, and point it to the LogHelloWorld folder I created. This folder contain all the code I downloaded.

2. In the project, I set a reference to log4net.dll in the LogHelloWorld/bin folder.

When I run the the code and click the button, nothing has logged in LogHelloWorld/LogHelloWorld.log file.

Have I missed something here?


Title: Great Article for a Winform Application   
Name: Tom Gilkison
Date: 2005-01-12 2:15:56 AM
If you are working with an ASP.NET application and want to use log4net, you should read my log4net tutorial that deals with the security issues of ASP.NET:

log4net Tutorial: Using log4net in an ASP.NET Application

Here's a couple lines of code that may come in handy...
log4net.Config.DOMConfigurator.Configure(new System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) );
log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Title: Excellent Article   
Name: Raja Narayanan
Date: 2005-01-04 2:43:23 AM
we are using log4net in java side and the information provided by u is great. The same component we can use in .Net too. This info i get from ur web site only. Thanks. Great job.
Title: Re: Log4net WMI support   
Name: Ash
Date: 2004-12-10 1:54:53 AM
Hi Priya,

WMI is not supported by log4net but you can make custom extensions. You should check out the article EIF vs log4net(
I have never tried implementing WMI functionality and the only other person i know who has done this is Daniel Cazzulino. Check out the link

Daniel is a nice guy ask him to send you some sample code and he might.
Daniel's Blog:

Title: Log4net WMI support   
Name: Priya
Date: 2004-12-09 11:43:46 PM
Hello Ash,

I'd like to know how to implement the extra functionality for WMI that is supported by EIF, and how you can implement it using custom extensions with Log4net.
Title: Geat info   
Name: S Kumarmanngal
Date: 2004-12-07 9:41:30 PM
This is great info presented in simple and easy to understand way.I like it and hope it will be useful for many.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-07-12 11:27:26 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search