Recently i had used log4net error handling plugin in one of my recent project. Previously i was using enterprise library for this task. But this is very flexible and essay to use in application.
We can use Log4net in our application using this few steps
Step 1: Install the Log4net plugin like this
Step2: Inside the Configration section of Webconfig, keep this code related with log4net like this
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"></section> </configSections> <log4net> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <param name="File" value="C:\MyLog\test.txt"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/> </layout> </appender> <root> <level value="All" /> <appender-ref ref="LogFileAppender" /> </root> </log4net>
Step 3: In global.aspx file, keep the code like this
protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}
Step 4: Now implement the error handling in your code like this
protected void btnClick_Click(object sender, EventArgs e)
{
log4net.ILog logger = log4net.LogManager.GetLogger(typeof(_Default));
try
{
// This is used for generating the error in code.
int a;
a = Convert.ToInt32(txtNum.Text);
Response.Write(a);
}
catch (Exception ex)
{
logger.Error(ex.StackTrace);
}}
Now if there will be any exception occur in application then it will create the log in C:\MyLog\test.txt