Click here to Skip to main content
15,887,746 members
Articles / Programming Languages / C#
Article

Simple EventLog

Rate me:
Please Sign up or sign in to vote.
3.00/5 (8 votes)
30 Jul 20023 min read 100.4K   1.6K   28   6
A simple event log class written in C#

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,

C#
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,

C#
private bool WriteSimpleEntry( string entry, EventLogEntryType eventType )

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

C#
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.

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


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIsError is overkill Pin
Manfred Lange25-Sep-03 21:15
Manfred Lange25-Sep-03 21:15 
GeneralSingle-sign-on. Pin
ma7347msadict23-Sep-03 4:23
sussma7347msadict23-Sep-03 4:23 
Generallog4net Pin
Roelof Blom27-Aug-02 4:44
Roelof Blom27-Aug-02 4:44 
GeneralRe: log4net Pin
Anthony Roach27-Aug-02 5:38
Anthony Roach27-Aug-02 5:38 
GeneralNice begin for creating Wrapper Objects Pin
Juergen Posny15-Aug-02 6:55
Juergen Posny15-Aug-02 6:55 
GeneralRe: Nice begin for creating Wrapper Objects Pin
Anthony Roach15-Aug-02 7:49
Anthony Roach15-Aug-02 7:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.