.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
satyapriyanayak
abhays
SP
Ashutosh Jha
Mahesh

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Log error or exception using Log4Net

Posted By:Pankaj Mishra       Posted Date: January 16, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

Log4net is an open source Logging library for .NET Framework to log error and exception. Its one of most popular library to Log error. We will see how can we use Log4Net in our application. Below is the sample  code based on Asp.Net and C#.

To use Log4Net in your  application first download log4net library from here

Lets see step by step method to use Log4Net

Step 1) Unzip that folder and add the reference of Log4Net.dll in your application. You can find the Log4net.dll in debug folder of net (eg \incubating-log4net-1.2.10\log4net-1.2.10\bin\net\2.0\debug).



Step 2) Add this line of code in your Global.asax file under Application_Start method.

Note: if you are using Asp.Net 3.5 then you have to add manually by right click on the solution explorer "Add New Item" the add "Global Application Configuration" file.

void Application_Start(object sender, EventArgs e)

{

log4net.Config.XmlConfigurator.Configure();

}


Step 3 ) Add this line of code in your web.config file under

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

Step 4 ) Add this lines of code just after

<log4net>

<appender name="LogFileAppender" type="log4net.Appender.FileAppender">

<param name="File" value="e:\Log4NetExample.log"/>

<layout type="log4net.Layout.PatternLayout">

<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>

layout>

appender>

<root>

<level value="All"/>

<appender-ref ref="LogFileAppender"/>

root>

log4net>

After adding the above line your web.config file will look like this

Now you are done with the set up of log4net configuration.


Step 5) To use the Log4Net add this line of code where ever you want to log error or exception.

protected void btnLog4Net_Click(object sender, EventArgs e)

{

int a = 23, b=0;

try

{

int result = a / b;

}

catch (Exception ex)

{

// Divide by Zero exception occurred and in the next line we are going to log that

ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

logger.Debug(ex.Message);

}

}

One you run your application its will automatically create Log4NetExample.log file under configured folder in the above example it will create in "c:\LogFiles\Log4NetExample.log".

You can change the file location by setting in Web.Config file.

Hope this will help all the beginner a basic idea to implement Log4Net in your application.

Cheers
Pankaj
 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend