EnjoySharePoint YouTube Channel

Log4Net in asp.net

In this post we will see how to use Log4Net in asp.net.

Get Office 365 Enterprise E3 Or Business Premium Subscription & Try out all the features

You can check my previous posts on:

Working with enterprise library for data access in asp.net Part-3

WCF tutorial and example in C#.Net

What is the AutoEventWireup attribute in Asp.Net?

Follow below steps in to use Log4Net in Asp.Net.

Dowonload log4net.dll from this web site.

After downloading give the reference in the Bin folder.

Then open the web.config file. Here we need to add two things:
– Add
<section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler,log4net”/>
inside the <configSections> tag.

– Then search for </system.web> and add the below code after this.

<appender name=”FileAppender” type=”log4net.Appender.FileAppender”>
<param name=”File” value=”c:\\AspDotNetHelp\MyLogs.log”/>
<!– Example using environment variables in params –>
<!– <param name=”File” value=”${TMP}\\ApplicationKit.log” /> –>
<param name=”AppendToFile” value=”true”/>
<layout type=”log4net.Layout.PatternLayout”>
<param name=”ConversionPattern” value=”%d [%t] %-2p %c [%x] – %m%n”/>
<level value=”ALL”/>
<appender-ref ref=”FileAppender”/>

Then open the global.asax file and add the below code in the Application_Start event.


Now in this example we will log something in the page load. Below is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using log4net.Config;

public partial class _Default : System.Web.UI.Page
ILog logger = log4net.LogManager.GetLogger(typeof(_Default).Name);
protected void Page_Load(object sender, EventArgs e)
logger.Info(“This is info message”);

logger.Debug(“This is debug message”);

logger.Error(“This is error message”);

logger.Warn(“This is warn message”);

logger.Fatal(“This is fatal message”);

Now if you open this c:\\AspDotNetHelp location then you will able to see the log.

Check out Best Alternative to InfoPath -> Try Now
You May Also like the Following SharePoint Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →