Logging is an essential part of any application. Following is a simple step by step guide to integrate Log4net in your ASP.NET Web Application.
Create a new website
- open Microsoft Visual Studio 2005 or 2008, then select File - > New - > Website
- Enter your desired application path and name for the website.
- Select your preferred language ( however, in this example we will use C# )

An Illustration of adding a new Website in Microsoft Visual Studio 2008
Include Log4net in your website
- Download Log4net.
- extract the downloaded folder and look for <log4net_folder>/bin/net/2.0/Log4net.dll
- Right-Click the project name and Add a Reference to Log4net.dll

An Illustration of adding Log4net.dll to your website
Log4net configuration
- Add a config file to your project with the name log4net.config
- Copy paste the following log4net configuration settings to log4net.config file.
<?xml version="1.0" encoding="utf-8" ?> <log4net> <root> <level value="DEBUG" /> <appender-ref ref="RollingLogFileAppender"/> </root> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="logsapp.log" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="100KB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d [%t]%-5p %c [%x] - %m%n" /> </layout> </appender> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="logsapp.log" /> <appendToFile value="false" /> <datePattern value="-dddd" /> <rollingStyle value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d [%t]%-5p %c [%x] - %m%n" /> </layout> </appender> </log4net>
- Create a folder logs in your project directory

An Illustration of log4net.config file
Initializing Log4net
- Add Global.asax to your project and then add the following function to it.
protected void ConfigureLogging()
{
string logFile = HttpContext.Current.Request.PhysicalApplicationPath + "log4net.config";
if (System.IO.File.Exists(logFile))
{
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(logFile));
}
}
- make a call to this function in Application_Start Event.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
ConfigureLogging();
}
Logging Errors and Debugging Information
- now we are ready to log
- declare a log4net.ILog variable in your page which needs to log.
#region Logger Setup protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #endregion
- Following code in Page_Load demonstrates how to log Debug Information and Errors.
#region Logger Setup
protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
protected void Page_Load(object sender, EventArgs e)
{
try
{
log.Debug("In Page Load Function");
log.Debug("Going to generate divide by zero");
int a = 0;
int b = 10 / a;
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
}
}
- Run you website and see the results in the logs folder we created earlier. It should contain the file app.log with contents like
2008-11-02 15:13:09,093 [4]DEBUG _Default [(null)] - In Page Load Function 2008-11-02 15:13:09,140 [4]DEBUG _Default [(null)] - Going to generate divide by zero 2008-11-02 15:13:09,140 [4]ERROR _Default [(null)] - Attempted to divide by zero. System.DivideByZeroException: Attempted to divide by zero. at _Default.Page_Load(Object sender, EventArgs e) in d:WorkTestsLoggingSampleDefault.aspx.cs:line 26
- thats it, your application is logging properly.
- VB.NET developers would need little syntax conversions.
- here is the sample code to download: Logging Sample


Very nice article … thanks
Mr Babar. That’s a very nice post. It helped me solve my problem & now I know what was I doing wrong. Thanks alot
Nice article, really a job saver.
thanks for that. broke down well…
Thank you so much for this post. I have been trying to get log4net setup for a quite a while, looking at many web pages includng the info on the Apache web site for log4net.
Because of you, I now have logging through log4net.
Thank you !!!!!!!
Works great! Thanks. I spent alot of time on this and went through some websites that I do not really comprehend. Thanks again!
Hello,
I tried to make log4net work on a windows service using vs 2008 .net framework 3.5 but it was displaying a message: Could not find schema information for the element ‘log4net.
I downgraded the .net FW to 2.0, built the service, and im still receiving this message.
I tried the same thing on a windows form application using VS2005 and it worked.
do you have any solution for this issue?
Thanks
Hi Ghinwa,
replace the ConfigureLogging function with the following function for windows forms/service type projects.
public static void ConfigureLogging()
{
log.Debug(”Configuring Logging”);
string logFile = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + “\\log4net.config”;
if (System.IO.File.Exists(logFile))
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(logFile));
log.Debug(”Log Configured”);
}
and you should be good
thank for the article it is working good in file system when it is moved to http it is not working may be i missed some please let me know if i missed some thing
make sure the log folder and read and write permissions for the IUSR_MACHINENAME user.
log4net.dll