Click here to Skip to main content
Click here to Skip to main content
Go to top

Simple EventLog

, 30 Jul 2002
Rate this:
Please Sign up or sign in to vote.
A simple event log class written in C#
<!-- Download Links --> <!-- Add the rest of your HTML here -->

Introduction

The SimpleEventLog class is designed to make using the event log, well, simpler in that it encapsulates all the types and information required to use the Windows Event Log without needing to add them to your own classes and can be used to quickly add custom event logs to the Event Viewer. It also provides the functionality to clear the data from the Event Log once it is finished with. The problem with the Event Log is that it either isn't used as much as it could be or when it is large amounts of data are constantly dropped into it and forgotten about, leaving the Event logs on the system to bloat. The class therefore provides the functionality to clear the event log and delete it completely.

One way in which I intend to use the class is to create custom Event Logs for applications that log required data when the app is running but delete the information when the application exits correctly. This means that the log only remains if there is a problem, thereby removing the bloat factor.

The sample code was developed and tested on Windows XP Home Edition

The SimpleEventLog Class

The SimpleEventLog class contains two constructors,

public SimpleEventLog( string eventLogName )
public SimpleEventLog( string eventLogName, bool clear )
                                    : this( eventLogName )

The First takes just the name of the EventLog which you set up and can be seen by viewing Control Panel\Administrative Tools\Event Viewer. When you run the sample program if you stop at the first breakpoint you can check that the Event Viewer reports not only the Event Logs of "Application", "Security" and "System" but also the TestLogOne Event Log. The second constructor is designed so that the extra parameter allows you to specify if you want to clear the Event Log or not by taking an extra boolean parameter which if set to true will clear the Event Log named.

The class attempts to catch all exceptions that the functions are reported to throw and saves the error message returning true or false from all it's functions. This is so that all the exception handling code is in one place and not scattered repeatedly through the code that is using the event log, allowing the users of the class to focus on writing their application and not on managing the Event Log.

The main function for writing to the Event Log is,

private bool WriteSimpleEntry( string entry, EventLogEntryType eventType )

This is a private function that is called by the public write functions which are

public bool WriteInformation( string information )
public bool WriteWarning( string warning )
public bool WriteError( string error )

I chose to do it this way so that the EventLogEntryType enumeration can be encapsulated within the class and doesn't need to be incorporated into the code that is using the class.

As mentioned above the class has a built in function for clearing the current Event Log and for Deleting the current Event Log.

public bool ClearSimpleEventLog()
public bool DeleteSimpleEventLog()

The sample project uses the SimpleEventLog class and has break points set to show what is happening at each point. the Sample creates a SimpleEventLog object and then writes to it before creating another that clears the information and then write to it again before deleting the new Event Log entirely.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Anthony Roach
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralIsError is overkill PinmemberManfred Lange25-Sep-03 21:15 
GeneralSingle-sign-on. Pinsussma7347msadict23-Sep-03 4:23 
Generallog4net PinmemberRoelof Blom27-Aug-02 4:44 
GeneralRe: log4net PinmemberAnthony Roach27-Aug-02 5:38