3

I am using log4net which is being configured via a config file. There is one case where I would like everything to be logged in debug (some code used during an upgrade). Is there anyway I can get the current logging level, set it to debug, run the specified code, then back to whatever it was before?

Kyle
  • 17,317
  • 32
  • 140
  • 246

3 Answers3

3

You can add a section in the config file specifically for DEBUG using

 <logger name="MyApp.DebugLogging" additivity="false">
        <level value="DEBUG"/>
        <appender-ref ref="MainLog"/>
    </logger

Then in your code, you can access this logger using:

 private static readonly ILog debuglog = log4net.LogManager.GetLogger("MyApp.DebugLogging");

And use the variable in your code block as

 debuglog .Debug(ex);

The idea here is you should be able to create different loggers in your config file, then have flexibility of choosing which logger to use within your code blocks

Julius A
  • 38,062
  • 26
  • 74
  • 96
  • That's a fine solution. However, I'd rather use the existing loggers instead of creating a new logger just for the process described. Why? Because if you properly use the logging infrastructure, you should already have the `Debug` method in each logger. Change the logging level (or filter, whatever) **only in the config file**, and you'll have the benefit of viewing the `Debug` level without any new code to the system. New code is more maintenance and more potential bugs. – Ron Klein Jan 17 '11 at 19:17
1

Here's an idea:

Use log4net configuration file only. Set the "watch" flag to true, in order to watch it.

Inside this configuration file, set a logging filter (in each appender). Set its min level to, say, Info or whatever. See the Filters section in http://logging.apache.org/log4net/release/manual/configuration.html

Then, before the upgrade phase, edit the configuration file and change the filter's min level to, say, Debug.

Once the upgrade is done, set the filter's min level back to its original value.

Ron Klein
  • 9,178
  • 9
  • 55
  • 88
1

This question discussed dynamic reconfiguration of Log4Net loggers, which should give you the info you need.

Community
  • 1
  • 1
Joe
  • 122,218
  • 32
  • 205
  • 338