Enterprise Library 5 Logging Block: How to write to event viewer?

Pompair picture Pompair · Sep 22, 2011 · Viewed 7k times · Source

I'm using Logging Application Block from Enterprise Library 5 trying to log into flat file and event log. My configuration looks like this:

enter image description here

I'm using this code to write to logs:

Logger.Write("message", "General1");
Logger.Write("message", "General2");

The problem is, I get the flat file logging ok, but event log never shows the log entries. I've made there a custom view to filter for "Enterprise Library Logging".

How can one produce entries in the event viewer log with Ent lib 5?

Thanks.

Pom.

Answer

Randy supports Monica picture Randy supports Monica · Sep 23, 2011

I think there are two things that could be happening here:

  1. Logging is working but your custom filter is not correct
  2. Logging is not working

Let's be optimistic and assume that logging is working.

Logging is Working

You've configured the EventLogTraceListener to log to an Event Log called MyLog with a source of "Enterprise Library Logging". Perhaps the custom filter is configured to filter by log and is not searching MyLog? Double check your filter and also check that there actually is a log under Applications and Services Logs called MyLog.

If I were to guess, though, that's probably not the issue.

Logging Is Not Working

Let's assume that logging is not working. The most likely reason that it is not working is that you don't have permission to create an event log. I also notice that the Logging Errors & Warnings section is not configured with a listener. It's always a good idea to setup the Logging Errors & Warnings to use a trace listener (I prefer the flat file trace listener since it's one of the simplest so less can go wrong).
So, in your case, I would set it to use the "Flat File Trace Listener" that you have already set up:

 <specialSources>
   <allEvents switchValue="All" name="All Events" />
   <notProcessed switchValue="All" name="Unprocessed Category" />
   <errors switchValue="All" name="Logging Errors &amp; Warnings">
    <listeners>
     <add name="Flat File Trace Listener" />
    </listeners>
   </errors>
  </specialSources>

Now if you re-run your program you should probably see an error message in the consoletrace.log.

If permissions are the problem the usual solution is to create the event logs and their sources during installation with an account that has the proper permissions. E.g. logging a message as administrator to setup your log and category.