Getting the Contents of an Event Log

Team-Fly    

 
Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 20.  Writing to the Event Log

Getting the Contents of an Event Log

Event log entries can be obtained individually for evaluation by your program. The EventLog.Entries property returns an array of EventLogEntry objects. You can iterate through the array to look for specific kinds of entries or just print all entries, as the code in Listing 20.1 demonstrates .

Listing 20.1 Iterating over EventLogEntry objects
  1:  Private Sub DisplayEntries(ByVal LogName As String)  2:   3:  If (Not EventLog.Exists(LogName)) Then Exit Sub  4:   5:  TextBox1.Clear()  6:  Dim Log As New EventLog(LogName)  7:  Try  8:   9:  Dim Entry As EventLogEntry  10:  For Each Entry In Log.Entries  11:  TextBox1.Text = TextBox1.Text & Entry.Message & _  12:  vbCrLf  13:  Next  14:   15:  Finally  16:  Log = Nothing  17:  End Try  18:   19:  End Sub 

The Entries array is an instance property. Line 6 creates an instance of an EventLog that refers to a specific log. The For Each constructor iterates over each EventLogEntry and adds the EventLogEntry.Message value to a TextBox control. The resource protection block Try..Finally sets the Log instance to Nothing. The EventLogEntry type properties are shown in Table 20.1.

Table 20.1. EventLogEntry Access Properties
Member Name Description
Category Returns the text associated with the CategoryNumber
CategoryNumber Returns the category number for this application
Container Returns the IContainer interface
Data Returns the binary entry data
EntryType Returns the EventLogEntryType
EventID Returns the event identifier
Index Returns the index for an entry
MachineName Returns the name of the machine that generated the entry
Message Returns the text for the entry
ReplacementStrings Returns the replacement string-values for an entry
Site Gets or sets the component site
Source The source (usually the application) that generated the entry
TimeGenerated Returns the time the event was generated
TimeWritten Returns the time the entry was written
UserName Returns the user originating the event

Team-Fly    
Top
 


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
ISBN: N/A
EAN: N/A
Year: 2001
Pages: 222

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net