动态重新配置 Log4Net [英] Dynamically reconfigure Log4Net

查看:45
本文介绍了动态重新配置 Log4Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关在我的 ASP.NET 应用程序中动态重新配置 Log4Net 日志记录级别的最佳方法的提示.我通常使用一个简单的配置,其中根记录器定义默认的日志记录级别,例如

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

并且可能有多个 appender,每个 appender 都有过滤器来定义它们使用的日志记录级别.

and there may be several appenders, each with filters to define the logging levels they use.

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

  1. 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.

接下来我想更进一步,在管理页面上能够显示一个 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.

我们的想法是创建一个通用管理页面,我可以将其放入我的所有应用程序中,允许管理员有选择地动态启用调试级别的日志记录以进行故障排除.

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 是有效级别 - 如果后者不为 null,则与 Level 相同,否则从父级继承.

    • 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!

      推荐答案

      您是否正在寻找这样的东西(未经测试的代码):

      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.

      这篇关于动态重新配置 Log4Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆