问题
In the test environment, the debug log is enabled, so I can see detailed debug log messages. In production environment, I want to disable debug log by default, but still want to enable/disable it if needed.
Can I enable debug log at runtime without restarting my application? My logging code looks like below:
if(logger.IsDebugEnabled) Logger.Debug("debug message");
回答1:
You can change to loglevel by setting it and then apply the new settings:
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level = Level.Debug;
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).RaiseConfigurationChanged(EventArgs.Empty);
回答2:
Normal way of doing this is to get log4net to Watch for changes to the app.config or configuration file.
eg use the attribute
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
or
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(logConfigFilename));
and the default config can be configured to set logging off.
Then any changes to your config file will take effect immediately without you need to restart your program.
来源:https://stackoverflow.com/questions/40176935/log4net-how-to-enable-debug-logging-at-runtime