Follow in Twitter & Facebook

Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools

Wednesday, April 4, 2012

Log4net.dll in Asp.Net

In this post we will see what is Log4Net and how we can use Log4Net in You can also visit this article which describes how you can use Log4Net in SharePoint.

Log4Net is really helpful in production environment where we can not debug our application (Because Visual Studio will not be available to us).
Also it happens sometimes that error will appear in the production server but don't appear in the development or testing server.
So by using Log4net , we can store or log or collect that errors which are going to happen at production time by giving some location there like in c drive by making one folder in that c drive.

After that you can able to get the error information from that location i.e that c drive with in that folder.Using Log4net , it will help you to collect errors in different ways like
  • File Appender (collect messages in file)
  • SMTP Appender  (collect messages in emails)
  • ADONet Appender (Collects messages in database)
  • EventLog Appender(collects messages in event viewer)
Using file appender helps you to print all messages you need in file. The file will be appended to rather than overwritten each time logging starts. There are different levels like:
  • Debug
  • Info
  • Warn
  • Error
  • Fatal
Here are the steps to use Log4Net:

Download the Log4net.dll from

Then add reference that dll to your website by rightclicking on your website and choose Addreference. Its better you should make one dll folder and copy and paste the Log4net dll on dll folder and then add reference from that folder.

Make the following two changes in the web.config file.

1st change:
Put the code just after the configsection like
<Section name ="log4net" type = "log4net.Config.Log4netConfigurationSectionHandler,log4net"/>.

2nd change:
Put the below code after the end of System.web
<appender name = "LogFileAppender"
  type="log4net.Appender.FileAppender" Param =""
<layout type ="log4net.Layout.PatternLayout"
  Param="" name = "ConversionPattern"
<level value="ALL">
<appender-ref ref = "LogfileAppender">

Write in the Global.asax
inside the Application_start
void Application_start(object sender,EverArgs e)

Now go to the .cs file where you trap the information like in my case Default.aspx.cs
Add below two namespaces

using log4net;
using log4net.Config;

Write the below code in side the class declaration, above the page_load

Ilog logger = log4net.LogManager.GetLogger(type of(_Default));

-Default is the class name
Ilog is the interface.

Then in side the pageload or button click where you want to trap the error can mention like below

logger.Info("This is info msgs");
 logger.Error("This is Error msgs");
logger.Warn("This is warn msgs");
logger.Fatal("This is Fatal msgs");
logger.Debug("This is Debug msgs");

After this open the path provided in the web.config in my case C:\XXX\Log4netExample.log file , you will find the error messages here.

For Example You can mention in the catch block while doing some try-catch operation in C#
Catch(Exception ex)
logger.Error("SelectByStoredProcedure:btnSubmit-click"+ ex.Message.Tostring());

Here SelectByStoredProcedure is the class name and btnSubmit-click is the submit button click event name.
If you mention all this class and all the details then you can able to know where the error occured and about the exact location details.

You can also visit this article for more information.

0 on: "Log4net.dll in Asp.Net"