Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a 'category' in the .net enterprise library logging (to event log)

I am writing some logs to the event log using the Microsoft enterprise library

Its writes logs away fine but doesnt seem to set the category in the event log. The category appears okay in the message body of the log (if I choose to set that) but the event viewer doesnt pick up the category.

What am i missing?


c# source

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

web config

<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>

like image 910
Matt Avatar asked Nov 17 '09 15:11

Matt


1 Answers

The EventLog Category is separate and distinct from the LogEntry categories. So I don't think you can use the LogEntry categories to display in the EventLog category field.

From a data perspective the types are incompatible: the EventLog category is a short while the LogEntry categories are a string. Yes, in the event viewer it shows a string but this value is looked up via a CategoryMessageFile defined in the registry.

If you want to be able to do some filtering in the event viewer, you could use the LogEntry.EventId property. You can populate this using any convention you wish. e.g. unique Event IDs for every logging point, an Event ID per layer, an Event ID per class or some other convention.

As a fallback you can always do a Find for your category within the description of the EventLog Entry.

like image 193
Randy supports Monica Avatar answered Oct 06 '22 00:10

Randy supports Monica