Configure Log4Net in web application

Kris-I picture Kris-I · Apr 18, 2012 · Viewed 121.7k times · Source

I have this code and the config file below:

ILog log = LogManager.GetLogger(typeof(MyClass));
log.Debug("Testing");

TestProj directory is not created and if I create it, no TestLog.txt file, no log ... nothing.

Any idea?

Thanks,

The config file

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

<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="C:\\TestProj\\TestLog.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
  </layout>
</appender>

<root>
  <level value="DEBUG" />
  <appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>

Answer

shriek picture shriek · Apr 18, 2012

You need to call the Configurefunction of the XmlConfigurator

log4net.Config.XmlConfigurator.Configure();

Either call before your first loggin call or in your Global.asax like this:

protected void Application_Start(Object sender, EventArgs e) {
   log4net.Config.XmlConfigurator.Configure();
}