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