2

First time playing with Log4Net and I'm running into trouble. I've followed various tutorials but I've been unable to get it to log anything out. Let me show you my code and hopefully you can tell me what I'm doing wrong.

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" 
             type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>  
  <log4net>    
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="CurrentLog.txt"/>
      <staticLogFileName value="true"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd"/>
      <filter type="log4net.Filter.LevelRangeFilter">
        <acceptOnMatch value="true" />
        <levelMin value="DEBUG" />
        <levelMax value="FATAL" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{dd MMM yyy HH:mm:ss} %level %message. %newline %exception" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>
</configuration>

AssemblyInfo.cs

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

Presenter.cs

At the top of the class I have this:

private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Then I try to use the log variable later in the class:

bool isDebugEnabled = log.IsDebugEnabled;
log.Debug("Failed to save", e);

Whenever I inspect the isDebugEnabled variable, it is false, as are all of the other isBlahEnabled if I inspect the log variable.

My suspicion is that I have not hooked up my app.config file correctly because this is the first time I have tried to use one. I created by right clicking on the project in solution explorer, adding a new item, choosing Application Configuration File and naming it app.config.

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Stuart Leyland-Cole
  • 1,243
  • 7
  • 19
  • 35
  • 1
    Does the account running the application have permissions to create and write to a file in the configured location? – Oded Aug 20 '11 at 16:32
  • It does. See my reply to @Waldo's answer, I now have the logging working without changing any permissions, etc. – Stuart Leyland-Cole Aug 20 '11 at 17:01

3 Answers3

14

This one works for me:

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>
    <log4net>
      <appender name="Main" type="log4net.Appender.RollingFileAppender">
        <file value="${USERPROFILE}\My Documents\MyApp\Logs\Main.log" />
        <appendToFile value="false" />
        <maximumFileSize value="1GB" />
        <maxSizeRollBackups value="3" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date{ABSOLUTE} %-5level %-18logger %message%newline" />
        </layout>
      </appender>
      <root>
        <level value="DEBUG" />
        <appender-ref ref="Main" />
      </root>
    </log4net>
</configuration>

Also be sure the build action on app.config is set to None and Copy to output dir is set to "Copy if newer". You can set these settings in the file properties.

Program.cs

public static ILog Log;

static void Main(string[] args)
{
    // Setup Logging
    log4net.Config.XmlConfigurator.Configure();
    Log = LogManager.GetLogger("MyAwesomeApp");

    // ...
}
Philipp M
  • 1,877
  • 7
  • 27
  • 38
Waldo Bronchart
  • 10,152
  • 4
  • 23
  • 29
  • 2
    I removed the line I had in AssemblyInfo.cs and added the first of your lines in Main() to the constructor of App.xaml.cs (I am working on a WPF project) and now everything works. Any idea why it didn't work before? – Stuart Leyland-Cole Aug 20 '11 at 17:00
  • I'm not familiar with the assemblyinfo method. Where in the documentation is this described? I guess it does something slightly different than actually configuring. – Waldo Bronchart Aug 20 '11 at 17:04
  • Both of these tutorials use the AssemblyInfo.cs method: http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx and http://mitch-wheat.blogspot.com/2007/04/log4net-net-logging-tool.html but no bother now that this is working! Thanks very much! – Stuart Leyland-Cole Aug 20 '11 at 17:16
4

Just to add my 2 cents: I had the same Problem, but I use the above config in separate log4net.config (so I have on all apps the same config), but I simply forgot to set the file property on build 'Copy when newer' (or similar wording, have German Version).

Rolfi
  • 452
  • 5
  • 13
3

Just to add my 2p in case this helps anyone else searching for a resolution to this problem:

In my case I was using the NServicebus.Logging package in addition to Log4net, and this package contains types with the exact same names as those in Log4net, differing only in the namespace.

Fixing the 'using' statements or fully-qualifying the type names resolved it, but it was very hard to spot the problem.

Will Westrop
  • 431
  • 6
  • 7