RE: Programmatically setting the logger level
Nicko Cadell <[email protected]> Mon, 19 Jan 2004 23:35:40 -0000
| Newsgroups | gmane.comp.windows.dotnet.log4net.user |
|---|---|
| Message-ID | <[email protected]> |
Greg, In the example given: > log4net.Appender.RollingFileAppender fileAppender = > new log4net.Appender.RollingFileAppender( > new log4net.Layout.PatternLayout( > "%d [%t] %-5p %c - %m%n"), log4netFile); > fileAppender.MaxFileSize = 65536; > fileAppender.MaxSizeRollBackups = 3; > log4net.Config.BasicConfigurator.Configure(fileAppender); the fileAppender is created and then used to configure log4net (the call to BasicConfigurator.Configure), therefore this appender is attached to the logger. Because this is using the BasicConfigurator rather than the DOMConfigurator no config files are used. If you want to switch logging on and off for the whole of log4net then you should set the threshold on the logger repository rather than on the appenders. This is checked sooner so will improve performance and is easier to do programmatically. The logger repository is the object that manages the loggers. To disable all logging set the threshold level for the repository to OFF: LogManager.GetLoggerRepository().Threshold = LogManager.GetLoggerRepository().LevelMap["OFF"]; To enable logging set the threshold to ALL (this is the default value): LogManager.GetLoggerRepository().Threshold = LogManager.GetLoggerRepository().LevelMap["ALL"]; Nicko > -----Original Message----- > From: Ismay, Greg (CALBRIS) > [mailto:[email protected]] > Sent: 19 January 2004 23:02 > To: Nicko Cadell > Subject: RE: [Log4net-users] Programmatically setting the logger level > > > nicko, > > im pretty keen on getting logging to switch on > programatically without having to write to the config file > and i think this is what this thread was about.. > > when you write: > > fileAppender.Threshold = > log4net.LogManager.GetLoggerRepository().LevelMap["WARN"]; > > i take it your fileAppender is an instance of the > log4net.appender.fileAppender class as the fileAppender class > doesnt seem to have any shared methods. > if it is an instance, then what ties this back to the > instance that the loggers are using, as setting the threshold > on an instance ive just instantiated doesnt (and i guess > shouldnt) do anything! > > any ideas? > > thanks > > greg > > > -----Original Message----- > From: Nicko Cadell [mailto:[email protected]] > Sent: Tuesday, 20 January 2004 12:32 AM > To: 'Kevin Wittmer'; [email protected] > Subject: RE: [Log4net-users] Programmatically setting the logger level > > > Kevin, > > Only the BufferingAppenderSkeleton classes support the > Evaluator (i.e. the ADONetAppender, > BufferingForwardingAppender, RemotingAppender, SMTPAppender > and SmtpPickupDirAppender appenders). > > If what you want is to set the event threshold for the > appender then you do not need to use the Evaluator at all. > i.e. the following configuration would > work: > > <appender> > <param name="Threshold" value="WARN" /> > </appender> > > Or the equivalent: > > <appender> > <threshold value="WARN" /> > </appender> > > To set this in code you would use: > > fileAppender.Threshold = > log4net.LogManager.GetLoggerRepository().LevelMap["WARN"]; > > Hope this helps, > > Nicko > > > -----Original Message----- > > From: Kevin Wittmer [mailto:[email protected]] > > Sent: 16 January 2004 23:56 > > To: [email protected] > > Subject: [Log4net-users] Programmatically setting the logger level > > > > > > > > Hello, > > > > I am hoping that someone can rattle off a couple of lines of > > code that would > > show me how to programmatically set the logger level. > > > > If I want to accomplish the following: > > > > <appender> > > <evaluator type="log4net.spi.LevelEvaluator"> > > <param name="Threshold" value="WARN"/> > > </evaluator> > > </appender> > > > > In C# using the various log4net interface and classes, etc. > > How can this be > > done? > > > > My application currently includes the following: > > > > log4net.Appender.RollingFileAppender fileAppender = > > new log4net.Appender.RollingFileAppender( > > new log4net.Layout.PatternLayout( > > "%d [%t] %-5p %c - %m%n"), log4netFile); > > fileAppender.MaxFileSize = 65536; > > fileAppender.MaxSizeRollBackups = 3; > > log4net.Config.BasicConfigurator.Configure(fileAppender); > > > > Thanks! Maybe we could use the reply(s) from this posting to > > update the > > log4net documentation to include such an example. Or, maybe > > the solution to > > this could make its way into the log4net FAQ. > > > > Kevin > > > > _________________________________________________________________ > > Get a FREE online virus check for your PC here, from McAfee. > > http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963 > > > > > > > > ------------------------------------------------------- > > The SF.Net email is sponsored by EclipseCon 2004 > > Premiere Conference on Open Tools Development and Integration > > See the breadth of Eclipse activity. February 3-5 in Anaheim, > > CA. http://www.eclipsecon.org/osdn > > _______________________________________________ > > Log4net-users mailing list > > [email protected] > > https://lists.sourceforge.net/lists/listinfo/log4net-users > > > > > ------------------------------------------------------- > The SF.Net email is sponsored by EclipseCon 2004 > Premiere Conference on Open Tools Development and Integration > See the breadth of Eclipse activity. February 3-5 in Anaheim, CA. > http://www.eclipsecon.org/osdn > _______________________________________________ > Log4net-users mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/log4net-users > ------------------------------------------------------- The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA. http://www.eclipsecon.org/osdn