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 thatILog 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