.NET Power Tip 8: Troubleshooting log4net Configurations

The configuration part of Log4Net often causes severe headaches. Most of the times, a small mistakes prevents any log output at all.

By adding the following snippet to your projects config file, log4net will print out its diagnostics information that often helps to pinpoint any problems:

1. Set debug=”true” in the first line of your log4net config element:

<log4net debug="true">
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <filter type="log4net.Filter.LogerMatchFilter">
        <loggerToMatch value="Log" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level - %message%newline" />
      </layout>
    </appender>

 

2. Add this to the <diagnostics> section of your config file:

 <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add 
          name="textWriterTraceListener"
          type="System.Diagnostics.TextWriterTraceListener"
          initializeData="log4netDiagnostics.log"/>
      </listeners>
    </trace>
  </system.diagnostics>

And voilà, you get a log4net diagnostics log file like this one:

log4net: Configuration update mode [Merge].
log4net: Logger [root] Level string is [ALL].
log4net: Logger [root] level set to [name="ALL",value=-2147483648].
log4net: Loading Appender [ConsoleAppender] type: [log4net.Appender.ConsoleAppender]
log4net:ERROR Failed to find type [log4net.Filter.LogerMatchFilter]
System.TypeLoadException: Could not load type [log4net.Filter.LogerMatchFilter]. Tried assembly [log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a] and all loaded assemblies
  at log4net.Util.SystemInfo.GetTypeFromString(Assembly relativeAssembly, String typeName, Boolean throwOnError, Boolean ignoreCase)
  at log4net.Util.SystemInfo.GetTypeFromString(String typeName, Boolean throwOnError, Boolean ignoreCase)
  at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.CreateObjectFromXml(XmlElement element, Type defaultTargetType, Type typeConstraint)