在.net企业库日志记录中设置"类别"(到事件日志)

Mat*_*att 6 c# logging enterprise-library

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

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

我错过了什么?


c#来源

LogEntry log = new LogEntry();
log.Message = "Test";
log.Categories.Add("Event");
Logger.Write(log);
Run Code Online (Sandbox Code Playgroud)

网络配置

<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>
Run Code Online (Sandbox Code Playgroud)

Ran*_*ica 5

EventLog类别与LogEntry类别是独立且不同的.所以我认为您不能使用LogEntry类别在EventLog类别字段中显示.

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

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

作为后备,您可以随时在EventLog Entry的描述中查找您的类别.