Showing posts with label logging. Show all posts
Showing posts with label logging. Show all posts

27 March 2012

Creating a Comprehensive Event Logger

Would you rather try to decipher the meaning of an error in the Windows application event log, or ram a fork into the back of your hand?  I thought so.

The problem with the event logs is they are notoriously uninformative.  It seems that their purpose is to taunt IT personnel, by suggesting some helpful information is available. Yeah, right.  There are entire Web sites dedicated to deciphering event identification numbers mean.  The necessity for such sites is just plain sad.  Apparently, leaving what happened a mystery is a better alternative to including actual WORDS that describe the problem, in human language.

I implore you: do not be that kind of developer!  I always insist on making everything human readable, and easy to understand.  By leveraging the extensible nature of .NET, we can easily log useful and meaningful information to the event log, which will help the user or IT administrator fix the problem, and/or help the developer debug the issue.

Here's how you can use a single method, to alleviate the IT admin's urge to strangle you.

09 June 2010

Event Log Jam

While testing a service I wrote, I kept running into an exception in the area that creates a custom event log:
Only the first eight characters of a custom log name are significant, and there is already another log on the system using the first eight characters of the name given. Name given: 'MyNewService', name of existing log: 'MyNewServiceTEST'.

I made a test log, to write debug messages to the log, so i don't clutter up the production log. I have no idea why "Only the first eight characters of a custom log name are significant" in this age where 64-bit OS is the standard, but that's how it is.

The issue was resolved simply by deleting the log. Here's the syntax used to create a custom log, write an event, and delete the custom log. I placed the source and custom log names in string value types, because you'll likely want to use these throughout the application. I usually store these in a settings file, for ease of maintenance.

WARNING! Do not delete system logs! You can delete these, and will probably be displeased if you do.


Create Event Log Source:

string sourceName = "MyApp";
string logName = "MyAppLog";

// The event source must exist, before
// a custom log may be created for your application!
// This must be run using administrative privelages,
// otherwise an "access denied" exception is thrown.
if (!EventLog.SourceExists(sourceName))
{   
    EventSourceCreationData esData = 
        new EventSourceCreationData(sourceName, logName);
    EventLog.CreateEventSource(esData);
}

Write Event Log:

EventLog evt = new EventLog(logName, ".", sourceName);
evt.WriteEntry(
    String.Format(
        "Test log entry submitted at: {0} {1}",
        DateTime.Now.ToShortDateString(),
        DateTime.Now.ToShortTimeString()), 
    EventLogEntryType.Information);

Delete Custom Log:
Note, you must be running with administrative privelages, otherwise you receive an exception stating, "Requested registry access is not allowed."

EventLog.Delete("MyCustomLog");

Writing to the Event Log is simple and easy!  Keep the first 8 characters of your custom event log unique, and you'll be in great shape.