Click here to Skip to main content
15,884,838 members
Articles / Programming Languages / Visual Basic
Article

Eventlog Viewer

Rate me:
Please Sign up or sign in to vote.
4.50/5 (12 votes)
14 Jun 20062 min read 79.2K   4.3K   47   7
A control to view an event log.

Sample Image

Introduction

Ever wanted to view the contents of the Windows event log from within your application? This article might be for you!

Background

Many of the applications that I develop require some form of logging. I have found that logging to the Windows event log is simple and painless, but having to exit my application and go look in the Windows NT Event Viewer is tough as items get old very fast. One feature that I find lacking in the Windows Event Viewer is that you cannot search, filter, or sort the log entries. I created this component for just that purpose.

Using the code

The use of this component should be pretty straightforward. You need to add the component to the VS2005 toolbox, drag an EventLogViewer onto a form, and set the "Log" property to the log that you want to watch. There are a few other properties you might want to set (visibility of source, event ID, etc.), but you'll figure it out... If you are having a hard time using it, you must be doing something wrong.

In order to do the search/filter/sort, I used a DataSet in conjunction with a binding source. As I had to resort to the "Brute Force" method described by Christoph Wille's article "Displaying Event Log Entries the ASP.NET Way", the initial load time for the component was high; in my case, this gives me plenty of time to display a splash screen. :)

There isn't really code that is breath-taking here. One problem I had was getting the DataGridView to sort the logs by date/time properly. I found that both the DataGridView column and the underlying DataSet's column needed the DataType set in order for it to work.

VB
' Setup the dataset
' (Don't forget to set the DataType of the "Date/Time" column)
ds = New DataSet("EventLog Entries")
ds.Tables.Add("Events")
With ds.Tables("Events")
    .Columns.Add("Type")
    .Columns.Add("Date/Time")
    .Columns("Date/Time").DataType = GetType(System.DateTime)
    .Columns.Add("Message")
    .Columns.Add("Source")
    .Columns.Add("Category")
    .Columns.Add("EventID")
End With

...

' Set the column type of the DataGridView as well!
If col.Name = "Date/Time" Then
    col.ValueType = GetType(System.DateTime)
End If

Points of interest

The DataGridView is awesome! I learned several things about it while writing this code... If you have not had a chance to use the DataGridView, have a look at it. Some of the visual queues (icons, colors, highlight of selected item, etc.) were taken from Microsoft examples on how to use the DataGridView, but you can do just about anything you want with it.

History

  • Initial release.

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
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 19:38
professionalManoj Kumar Choubey26-Feb-12 19:38 
Generalquite helpful Pin
vegeta4ss10-Feb-10 6:08
vegeta4ss10-Feb-10 6:08 
GeneralHelps a lot Pin
rjknis28-May-07 23:10
rjknis28-May-07 23:10 
QuestionC/C++ version? Pin
minyang18-Jul-06 0:07
minyang18-Jul-06 0:07 
AnswerRe: C/C++ version? Pin
Benjamin Liedblad18-Jul-06 13:58
Benjamin Liedblad18-Jul-06 13:58 
GeneralRe: C/C++ version? Pin
minyang18-Jul-06 15:05
minyang18-Jul-06 15:05 
GeneralEvent Viewer Pin
AbuseByUnkindPeople18-Jun-06 13:34
AbuseByUnkindPeople18-Jun-06 13:34 

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.