Log4net: How to enable debug logging at runtime?

こ雲淡風輕ζ 提交于 2020-01-07 02:51:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!