views:

440

answers:

2

I'm running Visual Studio 2008 - and have a problem with log4net logging (v1.2.10). I have a small console test program with a single log statement. I have log4net configured for RollingLogFileAppender and ConsoleAppender.

When I run the compiled exe from the command line, I see the correct creation of the log file in my runtime directory. The log file is created (or appended to, when it exists), but the only output is the [Header] and [Footer] as configured. There is no output to console.

However, when I run under the debugger, the log message appears both in the log file and on the console. Following is my log4net configuration:

<log4net>
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
  </appender>
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="quicktest-log-" />
    <appendToFile value="true" />
    <immediateFlush value="true" />
    <datepattern value="yyyy-MM-dd" />
    <maxSizeRollBackups value="100" />
    <maximumFileSize value="1048576" />
    <rollingStyle value="Composite" />
    <staticLogFileName value="false" />
    <layout type="log4net.Layout.PatternLayout">
      <header value="[Begin Quicktest program log]&#13;&#10;" />
      <footer value="[End Quicktest program log]&#13;&#10;" />
      <conversionPattern value="%date{HH:mm:ss} [%thread] %-5level %logger: %message%newline" />
    </layout>
  </appender>
  <root>
    <level value="ALL" />
    <appender-ref ref="ConsoleAppender" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>
A: 

This theory could be a stretch, but have you ensured that the app.config has been copied to the folder where your executable is? App.config must be copied to where the output executable is, and you also must rename it to <executablename>.config, so if your executable is MyProgram.exe, config must be in MyProgram.exe.config.

galets
Thanks for the speedy response and sanity check ... the app.config is getting handled correctly.
moomi
A: 

It is working now, but the mystery remains. Apparently a bellyful of Chinese buffet is the only solution to a problem like this, for once I had one, the problem went away.

My test program was a single file with

class Test 
{
    static void Main (string[] args)
    {
       .
       . // some logging attempted here.
       .
    }
}

When I had the problem originally, I was doing the logging within Main(). Afterwards, I created a method on the class Test, instantiated class Test in Main, and moved the logging to the method. This removed the problem:

class Test
{
    static void Main (string[] args)
    {
      var p = new Test();
      p.Go ();
    }
    public void Go ()
    {
       . // some logging here.
    }
}

This is still inconclusive. I moved it back the way it was originally and it began working. So, I must conclude that the answer to this conundrum is: Do not attempt first-time log4net test programs without a bellyful of Chinese food.

moomi
I have the same problem - no chinese food in my town though :(
UpTheCreek