tags:

views:

2680

answers:

2

Hi All,

I am using log4net in a C# project, in the production environment, I want to disable all the logging, but when some fatal error occures it should log all the previous 512 messages in to a file.I have successfully configured this, and it is working fine. It logs the messages in to a file when some fatal error occures.

But when I run it from Visual Studio, I can see all the log messages are written to the Output window, regardless of whether it is a Fatal or not. (I cant see these messages when I run from the Windows Explorer - my application is a WinForm exe and there is no Console window to see the output)

Is there any way to disable this logging? I need my logs only in file, that too when some fatal error occures.

 <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="log.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="1MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
   <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
 </appender>

 <appender name="BufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender" >
  <bufferSize value="512" />
  <lossy value="true" />
  <evaluator type="log4net.Core.LevelEvaluator">
   <threshold value="FATAL"/>
  </evaluator>
  <appender-ref ref="RollingFileAppender" />
 </appender> 

 <root>
  <level value="DEBUG" />
  <appender-ref ref="BufferingForwardingAppender" />   
 </root>
</log4net>

And this is how I configure it in the static initializer of Windows Forms.

static Window1()
    {
      Stream vStream = typeof(Window1).Assembly.GetManifestResourceStream("TestLogNet.log4net.config");
      XmlConfigurator.Configure(vStream);
      BasicConfigurator.Configure();
    }

And I have the logger object initialized in the constructor of WinForm

logger = LogManager.GetLogger(typeof(Window1));

Please help,

[language - C#, .NET Framework - 3.5, Visual Studio 2008, log4net 1.2.10, project type - WinForms]

A: 

Do you still see the messages in Visual Studio if the application is compiled in release mode? It's possible that log4net uses Debug.Write to show the errors anyway. If that's the case then those messages shouldn't appear in release mode.

rslite
+4  A: 

Remove the BasicConfigurator.Configure() line. That's what that line does -- adds a ConsoleAppender pointing to Console.Out.

Jonathan
Thanks Jonathan, it worked.
Crazy88