加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

.net – 如何配置Microsoft Enterprise Library日志记录应用程序

发布时间:2020-12-14 03:57:29 所属栏目:Windows 来源:网络整理
导读:我正在尝试将 Common Infrastructure Libraries for .NET(Common.Logging)与Enterprise Library 4.1的Logging Application Block一起使用.根据 docs,这是支持的. 我遇到的问题是,当我尝试记录某些内容时,就像这样(在本例中我是在ASP.NET MVC应用程序中): pu
我正在尝试将 Common Infrastructure Libraries for .NET(Common.Logging)与Enterprise Library 4.1的Logging Application Block一起使用.根据 docs,这是支持的.

我遇到的问题是,当我尝试记录某些内容时,就像这样(在本例中我是在ASP.NET MVC应用程序中):

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

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

    return View();
}

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

Message: There is no explicit mapping for the categories ‘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,但这只会使警告静音 – 它不会使日志记录工作.

我也尝试摆脱< notProcessed switchValue =“All”... />特殊来源,但似乎也没有任何影响.

任何人都知道是否有办法让这个工作?谢谢!

解决方法

当您调用LogManager.GetCurrentClassLogger()时,Common.Logging将使用您的调用类的类型作为类别:

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读