3

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?

Philipp M
  • 1,877
  • 7
  • 27
  • 38
user2128702
  • 2,059
  • 2
  • 29
  • 74
  • Since you don't have the log4net configuration section introduced in the configSections section of the app.config, I presume your app.config is ignored. Otherwise you would get an exception saying that the log4net section is not legal in the app.config. – Wiktor Zychla Jul 25 '13 at 20:32

3 Answers3

1

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

Community
  • 1
  • 1
spatialguy
  • 464
  • 5
  • 11
0

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>
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • It is already set.Above the element.I have tried after the appender element but it is the same. – user2128702 Jul 25 '13 at 20:27
  • @user2128702, have a look at [this post](http://stackoverflow.com/questions/10295945/log4net-log-being-created-but-remaining-empty?rq=1). – Mike Perrenoud Jul 25 '13 at 20:29
  • I DO APPRECIATE YOUR ANSWER!!!THANK YOU!!!I have been trying all day long to do that but it comes up that I have missed to add the in the beggining.I just wanna ask you : Why is it supposed to be there?Otherwise the reading of the config doesnt happen if we don't have that first ? – user2128702 Jul 25 '13 at 20:38
  • @user, that's correct. The entire section isn't recognized because it's not configured and so when the log4net runtime looks for its configuration it can't find it and doesn't load. – Mike Perrenoud Jul 25 '13 at 20:43
  • Well, now my IsInfoEnabled,IsDebugEnabled etc. are set to true but when I MyLogger.Info("Some Info") it doesn't write it to the file. It is always the same content. – user2128702 Jul 25 '13 at 20:49
0

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>
Agrejus
  • 722
  • 7
  • 18