How to use log4net in Project ?

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

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"></section>

    <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"/>
      <level value="All" />
      <appender-ref ref="LogFileAppender" />

Step 3: In global.aspx file, keep the code like this

protected void Application_Start(object sender, EventArgs e)

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));
// This is used for generating the error in code.
int a;
a = Convert.ToInt32(txtNum.Text);
catch (Exception ex)


Now if there will be any exception occur in application then it will create the log in C:\MyLog\test.txt

Posted in Tags: . Comments Off on How to use log4net in Project ?
%d bloggers like this: