• Now Online : 39
  • admin@codemyne.net


log4net is used to output the log statements. log4net is the .NET version of the popular Java logger, log4j.

To get log4net working, you will need to copy the log4net dll and the log4net.xml files into bin directory. Once it is there, add a reference to log4net.

Note:To download log4net binaries use the following link. http://logging.apache.org/log4net/download.html

Step 1: Add the following line into your Application_Start event of Global.asax file


Step :2 Add the following line in web.config file before the closing tag of configSections as shown below

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

Step 3: Next add the given lines next to <connectionStrings/> in the web.config file

    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <!-- Please make sure the ..\\Logs directory exists! -->
      <param name="File" value="Logs\\test.log"/>
      <!--<param name="AppendToFile" value="true"/>-->
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <to value=""/>
      <from value=""/>
      <subject value=""/>
      <smtpHost value=""/>
      <bufferSize value="512"/>
      <lossy value="true"/>
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="WARN"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date
            [%thread] %-5level %logger [%property] - %message%newline%newline%newline"/>
    <logger name="File">
      <level value="All"/>
      <appender-ref ref="LogFileAppender"/>
    <logger name="EmailLog">
      <level value="All"/>
      <appender-ref ref="SmtpAppender"/>

log4net is based on appenders and loggers. Appender writes logs to some target. These may be a FileAppender, SmtpAppender, EventLogAppender, AdoNetAppender ....etc . Appenders may only be defined as child elements of the <log4net> element. Each appender must be uniquely named. The implementing type for the appender must be specified.

Loggers determine which appender should certain namespace use. logger element supports the following child elements

  • appender-ref: Allows the logger to reference appenders by name
  • level: Defines the logging level for this logger. This logger will only accept event that are at this level or above.
  • param: Logger specific parameters.

FileAppender: Writes logging events to a file in the file system.

SmtpAppender: Sends logging events to an email address. The To, From, Subject and SmtpHost are required parameters. A LevelEvaluator is specified with a threshold of WARN. This means that an email will be sent for each WARN or higher level message that is logged.

EventLogAppender: Writes logging events to the Windows Event Log.

AdoNetAppender: Writes logging events to a database using either prepared statements or stored procedures.

TraceAppender: Writes logging events to the .NET trace system.

Now you can use the log4net object from code behind as shown below. The log statements are appended to the file 'test.log' in the Logs folder (The file test.log should exists).

    log4net.ILog logger = log4net.LogManager.GetLogger(typeof(_Default));
private void TestMethod1()
        logger.Info("TestMethod1  Start");
        //code goes here
        logger.Info("TestMethod1  End");
    catch (Exception ex)
        logger.Warn("Error in method TestMethod1" + ex.Message);

A complete working example is given as attachement.

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment

test 4/13/2011 (IST) / Reply


eswar 6/6/2011 (IST) / Reply

Hi,,,,,,,,,,, this simple and easy to understand,,, i am first to in this site .... so really i am impressed.

Mujtaba 1/11/2017 (IST) / Reply

Hi, I want to write "<appender-ref ref="SmtpAppender"/>" line of code in behind in if condition and rest of the code will be as it is in config file . how i can do this. Thanks

aslam 6/8/2011 (IST) / Reply


riya shah 2/14/2012 (IST) / Reply

how to create a log4net.dll

Admin 2/26/2013 (IST) / Reply

Riya Shah: From step1 you can download the dll of log4net

Gugai 5/20/2013 (IST) / Reply