Log4net is registered only when working in the Visual Studio debugger

I am running Visual Studio 2008 - and I have a problem with log4net logging (v1.2.10). I have a small console test program with one 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. A log file is created (or added to it when it exists), but the only options are the [Header] and [Footer] settings. The console is not displayed.

However, when I run under the debugger, a log message appears both in the log file and on the console . Below 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> 
+4
source share
3 answers

Now it works, but the mystery remains. Apparently, the belly of the Chinese buffet is the only solution to such a problem, since I had one, the problem disappeared.

My test program was the only file with

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

When I had a problem, I was recording in Main() . Subsequently, I created a method of the Test class, an instance of the Test class in Main, and moved the record to the method. This fixed the problem:

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

This is still unconvincing. I moved it the way it was originally, and it started working. So, I must conclude that the answer to this riddle is: Do not try to use the first log4net test programs without the belly of Chinese food.

+1
source

This theory may be tense, but did you ensure that app.config was copied to the folder where your executable is located? App.config should be copied to where the output executable is, and you should also rename it to <executablename> .config, so if your executable is MyProgram.exe, the config should be in MyProgram.exe.config.

+2
source

I had the same problem and I was doing clickonce deployment.

I found that I need to add log4net.xml in the project properties> Publish> ApplicationFiles and make this log4net.xml file as a data file.

+1
source

All Articles