Log4Net in asp.net

InfoPath alternatives for form designing SharePoint

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

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.

Step-1:
Dowonload log4net.dll from this web site.
http://logging.apache.org/log4net/index.html

After downloading give the reference in the Bin folder.

Step-2:
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.

<log4net>
<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”/>
</layout>
</appender>
<root>
<level value=”ALL”/>
<appender-ref ref=”FileAppender”/>
</root>
</log4net>

Step-3:
Then open the global.asax file and add the below code in the Application_Start event.

log4net.Config.DOMConfigurator.Configure();

Step-4:
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.


You May Also like the Following SharePoint Online 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 →

Leave a Reply