Wednesday, February 2, 2011

How to use log4net in sharepoint 2010?

Introduction:
In this article we will discuss how can we use Log4Net in our SharePoint 2010 application. You can visit article how to use Log4Net in Asp.Net.

You can download the log4net dll from here. The next step is to deploy the log4net DLL to the GAC. Then open the web.config present in the port no 80.
Put the below code before the end tag like

<!--Section Added by Bijay Kumar for Log4Net-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
Put the below code before

<!-- Section added by Bijaya Kumar for log4net-->
<log4net debug="true">
<appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">
<file value="c:\Logs1\SparshLog.txt" />
<appendtofile value="true" />
<rollingstyle value="Composite" />
<datepattern value="yyyyMMdd" />
<maxsizerollbackups value="10" />
<maximumfilesize value="1MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionpattern value="%d{dd MMM yyyy HH:mm:ss} [%p] %c - %m%n" />
</layout>
<securitycontext type="log4net.Util.WindowsSecurityContext">
<credentials value="Process" />
</securityContext>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="GeneralLog" />
</root>
</log4net>

Now you are ready to use the Log4Net class like below. You can write different logging levels like:

FATAL
ERROR
WARN
INFO
DEBUG etc

Then import using log4net; in the class where you want to log.
Logger.info("This is a dummy information trapped");
Logger.error("This ia dummy error to trap");

No comments:

Post a Comment