Thursday, February 05, 2009

log4Net – Logger not working?

If you are using log4Net for the first time in your application and logging is not working, then first check if you are calling:


Configure() needs to be called only once during the lifetime of your application and a good place for you to insert it in a ASP.Net web-app is in the global.asax file’s Application_Start method.

Here is a basic log4Net configuration for the web.config file that you can test for basic log4Net issues.

Right under the <configuration><configSections> tag, add the following code:

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net,Version=,Culture=neutral,PublicKeyToken=1b44e1d426115821" requirePermission="false"/>

Next add the following log4Net configuration section:

    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
        <threshold value="DEBUG" />
        <evaluator type="log4net.Core.LevelEvaluator,log4net">
          <threshold value="DEBUG" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        <appender-ref ref="EventLogAppender" />

Finally call the following code – to see if your event log gets populated with some events.

log.Debug("Debug Message");
log.Info("Info Message");
log.Error("Error Message");


Still having problem?

Turn on log4Net’s internal debugging. Easiest way to do this is to add the following KV pair under <AppSettings>:

<add key="log4net.Internal.Debug" value="true" />

And then add the following text as a child of <configuration>:

    <trace autoflush="true">
            initializeData="logs\trace1.txt" />

This will spew out log4Net’s trace information to a file called “trace1.txt” under the logs folder of your app.

Trace not giving you any information?

Try and move the log4Net settings to a separate file.

Once the log4Net settings are in a separate file, call “log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath(null) +\\log4net.xml));” in your Global Application_Start method, instead of log4net.Config.XmlConfigurator.Configure(); as shown earlier.

Most of the above information is available at:


Anonymous said...

Thanks a lot, that save me from having a massive headache :)

Ron Cyrier said...

Thanks for this. Things just weren't happening. Now they are.

Chen Noam said...

Thanks alot.

Exactly what I needed.