Friday, June 27, 2008

Working with event logs

The .Net Framework provides the EventLog class in the System.Configuration namespace that allows us to create and manage log entries. In this article, I will describe how this can be achieved in just a few lines of codes.

a) Creating an Event Log

The EventLog class allows us to create an event log. Here are a couple of overloaded constructors available, but here, I am going to use the simplest one by providing a name for the log only. Also, to use the event log, the Source property needs to be specified and a message needs to be written as well.

Next, to write an entry, I will use the WriteEntry method, by giving the message and the entry type.

If the code is executed, the new event log will be visible in the Windows Event Viewer.

b) Deleting an Event Log

c) Getting existing event logs

No comments: