Subhash Sharma

Subhash Sharma
Subhash Sharma

This is Subhash Sharma(Software Engineer) Blog

Welcome to this blog and find every solution.............

Search This Blog

Software Engineer(Subhash Sharma)

Software Engineer(Subhash Sharma)
Software Engineer

Tuesday, January 18, 2011

Log4Net(for Making logger file)

//This is the link where you can read about log4Net
//open source provide by Microsoft

http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx
http://www.beefycode.com/post/Log4Net-Tutorial-pt-1-Getting-Started.aspx

http://shiman.wordpress.com/2008/07/09/how-to-log-in-c-net-with-log4net-a-tutorial/Here is
great example for sending logfile into mail......

use the logger to log a few statements with various severity levels. log4net defines 5 such levels:
Log4Net supports five levels of logs with five methods.

Debug: fine-grained statements concerning program state, typically used for debugging;

Info: informational statements concerning program state, representing program events or behavior tracking;

Warn: statements that describe potentially harmful events or states in the program;

Error: statements that describe non-fatal errors in the application; this level is used quite often for logging handled exceptions;

Fatal: statements representing the most severe of error conditions, assumedly resulting in program termination.

we have to add Log4Net Dll in Add reference section.

after that Inside the AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]

and add some code in web.config file..

section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" requirePermission="false"/>


log4net debug="true">
appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
file value="Log4net\\log4net.txt"/>
appendToFile value="true"/>
rollingStyle value="Size"/>
maxSizeRollBackups value="10"/>
maximumFileSize value="50KB"/>
staticLogFileName value="true"/>
lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
layout type="log4net.Layout.PatternLayout">
conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n"/>
/layout>
/appender>
root>
level value="ALL"/>
appender-ref ref="RollingLogFileAppender"/>
/root>
/log4net>



add key="log4net.Internal.Debug" value="true" />
/appSettings>



//////////and Inside the .cs page

using log4net;//Add class in page

//Define Globally
private static readonly log4net.ILog log = log4net.LogManager.GetLogger("_Default");

//use like this
protected void Page_Load(object sender, EventArgs e)
{
try
{
int a = 0;
int b = 1;
int c = b / a;
}
catch (Exception ex)
{
log.Error(ex.Message, ex);//here is using
}
}

No comments:

Post a Comment