简体   繁体   中英

How to check if an appender is disabled/enabled

Is it possible to determine if an appender is disabled (ie <level value="Off"> )?

I would like to have a global flag that determines if my appender is enabled:

public static bool isAppenderEnabled = /*...*/;

If the appender is enabled, then I would log stuff:

if(isAppenderEnabled)
{
    MyAppender.Warn("****************************************");
    MyAppender.Warn("Warning title!");
    MyAppender.Warn(String.Format("Some warning message! A:{0}, B:{1}, C:{2}, D:{3}", 0, 1, 2, 3));
    MyAppender.Warn("****************************************");
}

It's important to have the isAppenderEnabled flag, because I would be saving a lot of CPU time when the appender is disabled due to the fact that I'm not constructing all of those strings. What's the best/easiest way to check if an appender is turned off?

PS I've tried the MyAppender.Logger.IsEnabledFor(log4net.Core.Level.Off); , however, if I set the log level to Warn it always returns true for Off (basically, it always says the logger is disabled), so that doesn't work.

The recommended way of doing is, isn't to have a global flag to say whether logging is enabled, but rather to test the actual logging level of a per call basis, eg

if (MyAppender.IsWarnEnabled)
{
    MyAppender.Warn(String.Format("Some warning message! A:{0}, B:{1}, C:{2}, D:{3}", 0, 1, 2, 3));
}

...

if (MyAppender.IsDebugEnabled)
{
    MyAppender.Debug(String.Format("Some debug info! A:{0}, B:{1}, C:{2}, D:{3}", 0, 1, 2, 3));
}

...

if (MyAppender.IsErrorsEnabled)
{
    MyAppender.Error(String.Format("Some error message! A:{0}, B:{1}, C:{2}, D:{3}", 0, 1, 2, 3));
}

The advantage of this method is that you can set and leave your logging to Level.Error or Level.Fatal without constructing all the strings in your warn/debug and info messages.

Having said that, it is your choice. To answer your question, the reason MyAppender.Logger.IsEnabledFor(log4net.Core.Level.Off); doesn't work, is that MyAppender.Logger.IsEnabledFor(x) will return true if the level is set to x or higher . Hence I would expect this call to always return true.

The next level up is (AFAIK) log4net.Core.Level.Emergency so could check for that, but as that level isn't commonly used, you could also check for log4net.Core.Level.Fatal

MyAppender.IsFatalEnabled is just a property wrapper for MyAppender.Logger.IsEnabledFor(log4net.Core.Level.Fatal) ,

Based on my understanding of log4j which log4net is based on, you can check if the LOGGER is configured for the level that you are interested in by calling:

logger.isEnabledFor(Priority.WARN)  // or whatever log priority you want

There isn't anything that tells if an appender is enabled. You CAN however check to see if a given named appender is assigned to your logger. You would check:

if(logger.getAppender("MyAppender") == null)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM