动态重新配置 Log4Net
我正在寻找有关在 ASP.NET 应用程序中动态重新配置 Log4Net 日志记录级别的最佳方法的提示。 我通常使用一个简单的配置,其中根记录器定义默认日志记录级别,例如,
<log4net>
<root>
<level value="INFO" />
<appender-ref ref="..." />
<appender-ref ref="..." />
... etc ...
</root>
... etc
可能有多个附加程序,每个附加程序都带有过滤器来定义它们使用的日志记录级别。
我想做的第一件事是允许管理员连接到管理页面,使他们能够(a)查看根记录器的当前级别并(b)动态更改它。 我不想使用“ConfigureAndWatch”并写入磁盘上的配置文件,因为我不希望这些更改在应用程序回收时持续存在。
接下来我想更进一步,在管理页面上能够显示一个 TreeView,其中包含应用程序中存在的所有当前记录器及其当前日志记录级别。 并允许管理员能够在层次结构的任何级别有选择地更改日志记录级别。
我们的想法是创建一个通用管理页面,我可以将其放入所有应用程序中,允许管理员有选择地动态启用调试级别日志记录以进行故障排除。
我发现 Log4Net API 有点令人困惑,任何人都可以指出示例或展示实现此目的的最佳方法。
更新:
两个答案都同样好,所以我接受了第一个 - 谢谢。 重复一下,我可以按如下方式获取所有当前记录器:
foreach (log4net.ILog log in log4net.LogManager.GetCurrentLoggers())
{
log4net.Repository.Hierarchy.Logger logger =
(log4net.Repository.Hierarchy.Logger)log.Logger;
Debug.WriteLine(
String.Format("{0} Parent {1} Level {2} EffectiveLevel {3}<br>",
logger.Name,
logger.Parent.Name,
logger.Level == null ? "<null>" : logger.Level.Name,
logger.EffectiveLevel
)
);
}
EffectiveLevel 是有效级别 - 如果后者不为空,则与 Level 相同,否则从父级继承。
上面返回的至少一个记录器将根记录器作为父记录器,这使我能够获取对根记录器的引用。
上面返回的至少一个记录
通过上述内容应该可以重建记录器层次结构。
更新2
再次感谢。 我实现了一个 ASP.NET 服务器控件,它在带有复选框的 TreeView 中显示记录器层次结构,并允许用户动态更改层次结构中任何节点的日志记录级别。 效果很好,我将把它放在我所有 ASP.NET Web 和 Web 服务应用程序的管理页面上!
I'm looking for tips on the best way to reconfigure the Log4Net logging level dynamically in my ASP.NET apps. I generally use a simple configuration where the root logger defines the default logging level, e.g.
<log4net>
<root>
<level value="INFO" />
<appender-ref ref="..." />
<appender-ref ref="..." />
... etc ...
</root>
... etc
and there may be several appenders, each with filters to define the logging levels they use.
The first thing I'd like to be able to do would be to allow Administrators to connect to an admin page that enables them to (a) view the current level for the root logger and (b) dynamically change it. I don't want to use "ConfigureAndWatch" and write to the configuration file on disk because I don't want these changes to persist when the application is recycled.
Next I'd like to go further, and on an Admin page be able to display a TreeView with all current Loggers that exist in the application, and their current logging level. And allow the administrator to be able to change the logging level selectively at any level of the hierarchy.
The idea is to to create a generic admin page that I can put into all my apps that allows administrators to selectively enable DEBUG-level logging dynamically for troubleshooting purposes.
I find the Log4Net APIs a bit confusing, can anyone point to samples or show the best way to achieve this.
Update:
Both answers are equally good so I've accepted the first - thanks. To reprise, I can get all current loggers as follows:
foreach (log4net.ILog log in log4net.LogManager.GetCurrentLoggers())
{
log4net.Repository.Hierarchy.Logger logger =
(log4net.Repository.Hierarchy.Logger)log.Logger;
Debug.WriteLine(
String.Format("{0} Parent {1} Level {2} EffectiveLevel {3}<br>",
logger.Name,
logger.Parent.Name,
logger.Level == null ? "<null>" : logger.Level.Name,
logger.EffectiveLevel
)
);
}
EffectiveLevel is the effective level - same as Level if the latter is not null, otherwise inherited from the parent.
At least one of the loggers returned above will have the root logger as parent, which enables me to get a reference to the root logger.
With the above it should be possible to reconstruct the logger hierarchy.
Update 2
Thanks again. I've implemented an ASP.NET server control that displays the logger hierarchy in a TreeView with checkboxes, and allows the user to dynamically change the logging level at any node in the hierarchy. Works great and I'll be putting it on Admin page in all my ASP.NET Web and Web Service apps!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否正在寻找类似的东西(未经测试的代码):
您显然可以以相同的方式提取记录器名称等。
Are you looking for something like this (untested code):
You could obviously pull out the logger name, etc. in the same manner.
我已经成功地以编程方式更改了 log4net 记录器的日志记录级别,但如何从公共 API 执行此操作并不明显。 给定这个记录器:
您必须执行以下花哨的步骤才能将其设置为调试:
对于某些情况 - 我不知道是什么导致了这个更复杂的要求 - 您可能需要遵循文章中显示的额外步骤log4net 和更改记录器级别。
I have successfully programmatically changed the Logging level of a log4net logger, but it's not obvious how to do so from the public API. Given this Logger:
You have to do the following fancy footwork to set it to Debug:
For some situations -- I don't know what causes this more complicated requirement -- you may need to follow the extra steps shown in the article log4net and changing the logger levels.
可能不太完全是你想要的,但是:
May be not quite exactly what you want , yet: