'log4net doesn't work for Windows Forms Application

I really wish someone could help me with that. It is really driving me crazy.
I have a simple simple Windows Forms application and I am trying to use the log4net library for logging (I am just testing it in this project because it didn't work out in my main project).

So I have the regular Form1.cs, app.config, AssemblyInfo.cs and Program.cs.

In my app.config I have:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <file value="log-file.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <header value="[Your Header text here]" />
        <footer value="[Your Footer text here]" />
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] 
                 &lt;%property{auth}&gt; - %message%newline" />
      </layout>
    </appender>

  </log4net>

  <startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

In the Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using log4net.Config;

namespace log4net.test
{
    public partial class Form1 : Form
    {        
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public Form1()
        {

            InitializeComponent();      
        }

        private void button1_Click(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
            log.Debug("This is a DEBUG level message. The most VERBOSE level.");
            log.Info("Extended information, with higher importance than the Debug call");
            log.Warn("An unexpected but recoverable situation occurred");
        }
    }
}

And in the end of the AssemblyInfo.cs file I have added:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)]

When I debug and go to the button1_Click I can see that nothing is happening. The log object has its:
IsInfoEnabled, IsDebugEnabled, IsErrorEnabled, IsWarnEnabled set to false and just nothing happens.

I've been trying to find a solution all day long and nothing. Can somebody help?



Solution 1:[1]

You need to set the root logging level in the configuration:

    </appender>

    <!-- Set root logger level to DEBUG and its only appender to A1 -->
    <root>
        <level value="DEBUG" />
        <appender-ref ref="A1" />
    </root>
</log4net>

Solution 2:[2]

The problem is in this line:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)]

At runtime, there is no file "app.config" in the bin directory. You must either specify the correct file name like this:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "<your assembly name>.exe.config", Watch = true)]

or better just omit the name

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

I ran into a similar problem recently. If the logging does not work, I usually enable debug output as shown here: How to track down log4net problems

Solution 3:[3]

You will also need to set the configuration section for log4net. Setting the configuration section allows log4net to read settings from the app.config See Below:

<?xml version="1.0" encoding="utf-8"?>
<configuration>


    /// Add this section before the log4net node
    <configSections>
       <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>


   <log4net>
     <root>
       <level value="DEBUG" />
       <appender-ref ref="LogFileAppender" />
     </root>
     <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
       <file value="log-file.txt" />
       <appendToFile value="true" />
       <layout type="log4net.Layout.PatternLayout">
         <header value="[Your Header text here]" />
         <footer value="[Your Footer text here]" />
         <conversionPattern value="%date [%thread] %-5level %logger [%ndc] 
                 &lt;%property{auth}&gt; - %message%newline" />
       </layout>
     </appender>

   </log4net>

   <startup>
   <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
 </startup>
 </configuration>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mike Perrenoud
Solution 2 Community
Solution 3 TheMiddleMan