log4net will not read from app.config

Viewed 20409

I have two projects configured identically for log4net. One project logs fine; however, the other does not log at all.

The Logger in the project that is not logging returns IsFatalEnabled = false, IsErrorEnabled = false, IsWarnEnabled = false, IsInforEnabled = false and IsDebugEnabled = false.

I've copied and pasted from one project to the other, replaced the file completely and tried removing all whitespace.

What could be causing the one project not to properly be reading the correct levels from the app.config?

app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="logfile.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date: %-5level – %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="FileAppender" />
    </root>
  </log4net>
</configuration>

Program.cs

using log4net;

class Program
{
    private static readonly ILog Log = LogManager.GetLogger("SO");

    static void Main(string[] args)
    {
        Log.Info("SO starting");
    }
}
2 Answers

The solution for me was to use XmlConfigurator.Configure() in the program's startup code. However, my situation differed in that I was using the Autofac.log4net module to register the ILog in an Autofac container. In this instance, using XmlConfiguratorAttribute (with Watch set to either true or false) left the logger unconfigured.

Related