The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.

Follow below steps to configure and use Log4Net

1. Add log4net.dll to application references

2. Add configuration to web.cofig

  1. <configSections>
  2. <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" requirePermission="false"/>
  3. </configSections>
  4. <log4net>
  5. <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  6. <file value=".\.\log\DataWarehouse-%property{LogName}.log" type="log4net.Util.PatternString"/>
  7. <appendToFile value="true"/>
  8. <rollingStyle value="Date"/>
  9. <datePattern value="-yyyy-MM-dd"/>
  10. <layout type="log4net.Layout.PatternLayout">
  11. <conversionPattern value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %C{1} - %m%n"/>
  12. </layout>
  13. </appender>
  14. <root>
  15. <level value="ALL"/>
  16. <appender-ref ref="RollingLogFileAppender"/>
  17. </root>
  18. </log4net>


  1. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
  2. System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  3. log4net.Config.XmlConfigurator.Configure();
  4. log.Error(exp);