动态重新配置log4net的动态、log4net

2023-09-02 11:58:30 作者:思念有如万蚁噬心

我在寻找的动态重配置log4net的日志记录级别在我的ASP.NET应用程序的最佳方式提示。我一般用一个简单的配置,其中根记录器定义了默认的日志记录级别,如:

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.

我希望能够做的第一件事是让管理员连接到一个管理页面,使他们能够(一)查看当前等级为根记录器和(b)动态地改变它。我不想用ConfigureAndWatch,并写入到磁盘上的配置文件,因为我不希望这些改变持续存在的应用程序被回收的时候。

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.

接下来我想走得更远,和Admin页面上能够显示与存在于应用程序的所有当前的记录器,以及他们目前的日志记录级别一个TreeView。并允许管理员能够选择性地改变记录级别在层次结构的任何级别

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.

我们的想法是创建一个通用的管理页面,我可以投入我所有的应用程序,它允许管理员选择动态启用DEBUG级记录进行故障排除。

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.

我觉得log4net的API的有点混乱,任何人都可以指向样品或展示实现这一目标的最佳途径。

I find the Log4Net APIs a bit confusing, can anyone point to samples or show the best way to achieve this.

更新:

这两个答案都是一样的好,所以我已经接受了第一 - 谢谢。出演,我可以得到目前所有的记录器如下:

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是有效水平 - 同级别,如果后者不为空,否则继承自父

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.

更新2

再次感谢。我实现了一个ASP.NET服务器控件显示在复选框一个TreeView记录器的层次结构,并允许用户在层次结构中的任何节点动态地更改日志记录级别。伟大的作品,我会在我所有的ASP.NET Web和Web服务应用程序可以把它在管理页面!

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!

推荐答案

您是否正在寻找这样的事情(未经测试code):

Are you looking for something like this (untested code):

foreach (ILog logger in log4net.LogManager.GetCurrentLoggers())
{
  ((log4net.Repository.Hierarchy.Logger)logger).Level = 
      log4net.Core.Level.Error;
}

您可以显着拉出记录器名称等相同的方式。

You could obviously pull out the logger name, etc. in the same manner.