moomi
moomi

Reputation: 151

log4net only logs when running in the Visual Studio debugger

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>

Upvotes: 4

Views: 2158

Answers (3)

Rohit Sachan
Rohit Sachan

Reputation: 1222

I had the same issue, and I was doing clickonce deployment.

I found that i need to add log4net.xml in Project properties > Publish > ApplicationFiles and make this log4net.xml as datafile.

Upvotes: 1

moomi
moomi

Reputation: 151

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.

Upvotes: 1

galets
galets

Reputation: 18502

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.

Upvotes: 2

Related Questions