【问题标题】:Setting a 'category' in the .net enterprise library logging (to event log)在 .net 企业库日志记录中设置“类别”(到事件日志)
【发布时间】:2010-12-17 11:56:51
【问题描述】:

我正在使用 Microsoft 企业库将一些日志写入事件日志

它可以很好地写入日志,但似乎没有在事件日志中设置类别。该类别在日志的消息正文中显示正常(如果我选择设置该类别),但 事件查看器 没有选择该类别。

我错过了什么?


c#源码

LogEntry log = new LogEntry();
log.Message = "Test";
log.Categories.Add("Event");
Logger.Write(log);

网络配置

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
<listeners>
  <add source="TestLogSource" formatter="Text Formatter" log="TestLog"
    machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    name="Formatted EventLog TraceListener" />
</listeners>
<formatters>
  <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Severity: {severity}"
    type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    name="Text Formatter" />
</formatters>
<categorySources>
  <add switchValue="All" name="Events">
    <listeners>
      <add name="Formatted EventLog TraceListener" />
    </listeners>
  </add>
  <add switchValue="All" name="General">
    <listeners>
      <add name="Formatted EventLog 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>

【问题讨论】:

  • log.Categories.Add("Event**s**");?
  • 您的意思是您希望您记录的事件在事件查看器中属于您自己的类别?还是在事件查看器中查看后,在详细信息中看不到类别?

标签: c# logging enterprise-library


【解决方案1】:

EventLog 类别与 LogEntry 类别是分开且不同的。所以我认为你不能使用 LogEntry 类别来显示在 EventLog 类别字段中。

从数据的角度来看,这些类型是不兼容的:EventLog 类别很短,而 LogEntry 类别是字符串。是的,在事件查看器中它会显示一个字符串,但该值是通过注册表中定义的 CategoryMessageFile 查找的。​​p>

如果您希望能够在事件查看器中进行一些过滤,您可以使用 LogEntry.EventId 属性。您可以使用任何您希望的约定来填充它。例如每个日志记录点的唯一事件 ID、每个层的事件 ID、每个类的事件 ID 或其他约定。

作为后备,您始终可以在 EventLog 条目的描述中为您的类别进行查找。

【讨论】:

    猜你喜欢
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多