【问题标题】:How to configure Microsoft Enterprise Library Logging Application Block to handle any logging category?如何配置 Microsoft 企业库日志记录应用程序块来处理任何日志记录类别?
【发布时间】:2011-07-19 00:07:07
【问题描述】:

我正在尝试将Common Infrastructure Libraries for .NET (Common.Logging) 与 Enterprise Library 4.1 的日志记录应用程序块一起使用。根据docs,这是支持的。

我遇到的问题是,当我尝试记录一些东西时,就像这样(在这个例子中,我在一个 ASP.NET MVC 应用程序中):

public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    ILog log = LogManager.GetCurrentClassLogger();
    log.Info("Hello world!");

    return View();
}

我没有收到日志消息,而是在我的事件日志中收到错误:

消息:“TestApp.Controllers.HomeController”类别没有显式映射。

嗯,Common.Logging 中似乎没有任何选项来设置默认类别,我不知道如何配置 LoggingConfiguration 以接受任何类别,即使它没有定义。

这是我的 LoggingConfiguration 的 sn-p:

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
...
  <categorySources>
    <add switchValue="All" name="General">
      <listeners>
        <add name="Formatted EventLog TraceListener" />
        <add name="Email TraceListener" />
      </listeners>
    </add>
  </categorySources>
  <specialSources>
    <allEvents switchValue="All" name="All Events" />
    <notProcessed switchValue="All" name="Unprocessed Category" />
    <errors switchValue="All" name="Logging Errors &amp; Warnings">
      <listeners>
        <add name="Formatted EventLog TraceListener" />
      </listeners>
    </errors>
  </specialSources>
</loggingConfiguration>

我尝试将 logWarningsWhenNoCategoriesMatch 设置为 false,但这只会使警告静音 - 它不会使日志记录工作。

我也尝试过删除 &lt;notProcessed switchValue="All" .../&gt; 特殊来源,但这似乎也没有任何效果。

有人知道是否有办法让它工作吗?谢谢!

【问题讨论】:

    标签: .net logging web-config app-config enterprise-library


    【解决方案1】:

    “notProcessed”特殊源将匹配任何与类别源不匹配的条目 - 因此您可以使用它来捕获它们。这里有更多信息:https://entlib.codeplex.com/discussions/215189

    <notProcessed switchValue="All" name="Unprocessed Category"/>
        <listeners>
            <add name="Rolling Flat File Trace Listener Unprocessed"/>
        </listeners>
    </notProcessed>
    

    【讨论】:

    • 这是适合我的解决方案,因为我不必更改源代码。谢谢!
    【解决方案2】:

    当你调用LogManager.GetCurrentClassLogger()Common.Logging 时会使用你调用类的类型作为类别:

    public static ILog GetCurrentClassLogger()
    {
        StackFrame frame = new StackFrame(1, false);
        return Adapter.GetLogger(frame.GetMethod().DeclaringType);
    }
    

    如果你想使用这种方法(虽然建议不要过度使用这种方法,因为它会检查 StackFrame),那么,是的,你需要为你正在记录的每个类创建一个类别。这将重新创建一种在 Log4Net 中使用的分层记录器。但这种方法并不理想,因为它不可维护。

    您应该能够通过使用LogManager.GetLogger(string name) 重载而不是GetCurrentClassLogger 来解决此问题。看起来传入的名称将用作登录企业库的类别。因此,您可以将类别名称定义为常量并将其传递给 GetLogger 方法,让您的整个应用程序使用一个企业库类别。

    【讨论】:

    • @Tuxo - 太棒了!我没有意识到GetLogger 会以这种方式工作——我的假设(d'oh)是GetLogger 会返回一个匹配给定类型的记录器,有点像ServiceLocator 模式。现在命名对我来说更有意义。谢谢!!!
    • @Pandincus - 有一个 GetLogger 重载,它采用您可能想到的类型。
    猜你喜欢
    • 2010-12-30
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    相关资源
    最近更新 更多