29

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");
    }
}
Philipp M
  • 1,877
  • 7
  • 27
  • 38
Even Mien
  • 44,393
  • 43
  • 115
  • 119
  • Related post - [Configuring log4net with xml file](https://stackoverflow.com/q/1321261/465053) – RBT Sep 15 '21 at 05:36

2 Answers2

51

It seems that the app.config file was not configured to be watched by log4net.

I added the following line to AssemblyInfo.cs and logging is now enabled:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Weird, since I didn't add this line to the the project that was working.

EDIT:

Looks like the project that was working did have the line after all. I must have forgotten.

// This will cause log4net to look for a configuration file 
// called [ThisApp].exe.config in the application base 
// directory (i.e. the directory containing [ThisApp].exe) 
// The config file will be watched for changes. 
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Even Mien
  • 44,393
  • 43
  • 115
  • 119
  • 5
    my case was approximately the same - i copied a project to another project, but "wrapped" log4net in custom assembly with my utility code - although the magic line you mention was in the new project, it was missing from my custom assembly's AssemblyInfo.cs (i had the impression that all configuration had to be done in the assembly that contained the entry point - wrong) – hello_earth Apr 19 '12 at 14:15
  • 2
    I had to do this for a VB.NET application... syntax: – Glen Little Jan 16 '13 at 21:31
  • 1
    Having not used log4net before, I just spent 3 hours trying to figure out why it was not working even though settings looked correct, before landing on this. Thank you! Also, obscure configuration settings like these in any library are the reason why I pull my hair and make me want to leave the profession of programming... :) – Punit Vora May 29 '20 at 00:01
1

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.

Josh Gallagher
  • 5,211
  • 2
  • 33
  • 60