C# – Log4Net/C# – Disable default logging

clog4net

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.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <log4net debug="false">

        <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>
</configuration>

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));

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

Best Answer

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

Related Topic