如何配置 Microsoft 企业库日志记录应用程序块来处理任何日志记录类别?

发布于 2024-10-24 04:06:33 字数 1905 浏览 1 评论 0原文

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

我遇到的问题是,当我尝试记录某些内容时,如下所示(在本示例中,我位于 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 的片段:

<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,但这只是使警告静音 - 它不会使日志记录工作。

我还尝试摆脱 特殊源,但这似乎也没有任何效果。

有人知道是否有办法让它发挥作用吗?谢谢!

I'm trying to use the Common Infrastructure Libraries for .NET (Common.Logging) with Enterprise Library 4.1's Logging Application Block. According to the docs, this is supported.

The problem I'm encountering is that when I try to log something, like so (in this example I'm in an ASP.NET MVC application):

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

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

    return View();
}

Instead of getting the log message, I'm getting an error in my event log:

Message: There is no explicit mapping for the categories 'TestApp.Controllers.HomeController'.

Well, there doesn't seem to be any option in Common.Logging to set the default category, and I can't figure out how to configure the LoggingConfiguration to accept any category, even if it isn't defined.

Here's a snippet of my LoggingConfiguration:

<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 & Warnings">
      <listeners>
        <add name="Formatted EventLog TraceListener" />
      </listeners>
    </errors>
  </specialSources>
</loggingConfiguration>

I've tried setting logWarningsWhenNoCategoriesMatch to false, but that just silences the warning -- it doesn't make the logging work.

I've also tried getting rid of the <notProcessed switchValue="All" .../> special source, but that doesn't seem to have any effect either.

Anyone know if there's a way to get this to work? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的那抹悲凉 2024-10-31 04:06:33

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

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

The 'notProcessed' special source will match any entries that do not match a category source - so you can use that to catch them. There's more information here: https://entlib.codeplex.com/discussions/215189

<notProcessed switchValue="All" name="Unprocessed Category"/>
    <listeners>
        <add name="Rolling Flat File Trace Listener Unprocessed"/>
    </listeners>
</notProcessed>
憧憬巴黎街头的黎明 2024-10-31 04:06:33

当您调用 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 方法,并使您的整个应用程序使用一个企业库类别。

When you call LogManager.GetCurrentClassLogger() Common.Logging is going to use the type of your calling class as the category:

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

If you want to use this approach (although the recommendation is not to use this method excessively since it inspects the StackFrame) then, yes, you will need to create a category for every class that you are logging from. This would recreate a kind of hierarchical loggers that are used in Log4Net. But this approach is not ideal since it is not maintainable.

You should be able to get around this by using the LogManager.GetLogger(string name) overload instead of GetCurrentClassLogger. It looks like the name that is passed in will be used as the category for logging in Enterprise Library. So you could define category name as a constant and pass that in to the GetLogger method and have your entire application use one Enterprise Library category.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文