Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 444
user2128702 Avatar asked May 21 '26 16:05

user2128702


1 Answers

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

like image 135
spatialguy Avatar answered May 23 '26 06:05

spatialguy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!