Click here to Skip to main content
13,249,813 members (83,592 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 30 Jul 2002

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


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.


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


About the Author

Anthony Roach
Web Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralIsError is overkill Pin
Manfred Lange25-Sep-03 22:15
memberManfred Lange25-Sep-03 22:15 
GeneralSingle-sign-on. Pin
ma7347msadict23-Sep-03 5:23
sussma7347msadict23-Sep-03 5:23 
Generallog4net Pin
Roelof Blom27-Aug-02 5:44
memberRoelof Blom27-Aug-02 5:44 
GeneralRe: log4net Pin
Anthony Roach27-Aug-02 6:38
memberAnthony Roach27-Aug-02 6:38 
GeneralNice begin for creating Wrapper Objects Pin
Juergen Posny15-Aug-02 7:55
memberJuergen Posny15-Aug-02 7:55 
GeneralRe: Nice begin for creating Wrapper Objects Pin
Anthony Roach15-Aug-02 8:49
memberAnthony Roach15-Aug-02 8: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 31 Jul 2002
Article Copyright 2002 by Anthony Roach
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid