logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject Re: Disable/enable appender for all loggers
Date Fri, 03 Aug 2007 02:53:33 GMT
Doh! I keep posted without reading all the messages. John's suggestion
is the same as mine except he posted it a few days earlier.

----- Original Message ----
From: Ron Grabowski <rongrabowski@yahoo.com>
To: Log4NET User <log4net-user@logging.apache.org>
Sent: Thursday, August 2, 2007 10:51:36 PM
Subject: Re: Disable/enable appender for all loggers

Instead of removing the appender you could just set its threshold to OFF then when you want
to enable it again you can set it back to DEBUG:

// untested
public static void SetThreshold(string appenderName, Level threshold)
{
    foreach (AppenderSkeleton appender in LogManager.GetRepository().GetAppenders())
    {
        if (appender.Name == appenderName)
        {
            appender.Threshold = threshold;
            appender.ActivateOptions();
            break;
        }
    }
}


----- Original Message ----

From: Radovan Raszka <raszka@hasam.cz>

To: log4net-user@logging.apache.org

Sent: Friday, July 27, 2007 2:23:05 AM

Subject: Disable/enable appender for all loggers



Disable/enable appender for all loggers           Hello,  

I am using three appenders in my log4net configuration (see bellow). But I need programatically
disable and reenable RollingFile appender at certain situations. Is there any simple solution?
I tried to use programatic configuration only.

  My config:  

<log4net>  

                <appender name="Console" type="log4net.Appender.ConsoleAppender">  

                        <layout type="log4net.Layout.PatternLayout">  

                                <!-- Pattern to output the caller's file name and line
number -->  

                                <conversionPattern value="%date{HH:mm:ss,fff} %5level [%thread]
%message%n" />  

                        </layout>  

                        <Threshold value="DEBUG" />  

                </appender> 

                  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
 

                        <appendToFile value="true" />  

                        <file value="Service.log" />  

                        <layout type="log4net.Layout.PatternLayout">  

                                <conversionPattern value="%date{dd-MM-yyyy HH:mm:ss,fff}
%5level [%2thread] %message (%logger{1}:%line)%n" />

                          </layout>  

                        <rollingStyle value="Date" />  

                        <Threshold value="DEBUG" />  

                </appender> 

                  <appender name="EvLog" type="log4net.Appender.EventLogAppender"> 


                        <ApplicationName value="IPserver" />  

                        <layout type="log4net.Layout.PatternLayout">  

                                <conversionPattern value="[%thread] %message (%logger{1})"
/>  

                        </layout>  

                        <Threshold value="INFO" />  

                </appender> 

                  <root>  

                        <level value="DEBUG" />  

                        <appender-ref ref="RollingFile" />  

                        <appender-ref ref="Console" />  

                        <appender-ref ref="EvLog" />  

                </root>  

        </log4net> 

  I tried to complete rewrite this config into C# code: 

  private readonly log4net.Appender.ConsoleAppender AppConsole;  

private readonly log4net.Appender.RollingFileAppender AppFile;  

private readonly log4net.Appender.EventLogAppender AppEvlog;  

private log4net.Repository.Hierarchy.RootLogger rootLog;  

//XmlConfigurator.Configure(new System.IO.FileInfo(IPserverCore.IPserver.GetAppPath() + "log4net.config"));
 

//xml configurator replaced by code  

AppConsole = new log4net.Appender.ConsoleAppender();  

AppConsole.Layout = new log4net.Layout.PatternLayout("%date{HH:mm:ss,fff} %5level [%thread]
%message%n");  

AppConsole.Threshold = log4net.Core.Level.Debug; 

  AppFile = new log4net.Appender.RollingFileAppender();  

AppFile.AppendToFile = true;  

AppFile.Layout = new log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level
[%2thread] %message (%logger{1}:%line)%n");

  AppFile.File = "Service.log";  

AppFile.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;  

AppFile.Threshold = log4net.Core.Level.Debug; 

  AppEvlog = new log4net.Appender.EventLogAppender();  

AppEvlog.Layout = new log4net.Layout.PatternLayout("[%thread] %message (%logger{1})");  

AppEvlog.ApplicationName = "IPserver";  

AppEvlog.Threshold = log4net.Core.Level.Info; 

  rootLog = new log4net.Repository.Hierarchy.RootLogger(log4net.Core.Level.Info);  

                          

rootLog.AddAppender(AppConsole);  

rootLog.AddAppender(AppFile);  

rootLog.AddAppender(AppEvlog);  

//----  

log = LogManager.GetLogger(typeof(IPservice));  

Log.Info("Start"); // no log produced here...  

rootLog.RemoveAppender(AppFile); // disable file logging  

...  

But after rewriting aplication doesn't create any logsā€¦.  

Did I miss some settings or am I complete wrong? 

  Thanks for any suggestions  

Radovan Raszka 

  











Mime
View raw message