Saturday, June 18, 2011

Windows Event Log by Visual Basic 2008

We've been experiencing Delayed Write Errors, which result in a loss of data.  In an effort to provide enough data points for our IT section to determine the problem, I needed to extract from the Window's Event Log all logged events.


For those who may not know, the Event Log is a system managed place in which the system logs certain events that users may use to diagnose problems.  It only took a few minutes on Google to pick up enough pieces to do what I needed.


Everything we need is found in the System.Diagnostics namespace.


Imports System.Diagnostics


Module Module1


    Sub Main()
        'Here we create an EventLog object
        Dim a As New EventLog
        Dim z As Long
        'This creates a streamwriter through which we will
        'write the log data.
        Dim myOut As New System.IO.StreamWriter("delayed.txt")
        'This is which of the EventLogs we are accessing 
        a.Log = "System"
        'We have to specify the machine name
        a.MachineName = My.Computer.Name

       '----------------------------------------
       '       the good stuff
       '       We iterate through each event log entry,
       '       looking for the word "delayed".  If we
       '       find it, we write out the entry.
       '------------------------------------------
        For Each entry As EventLogEntry In a.Entries
            z += 1
           
            If entry.Message.ToLower.Contains("delayed") Then
                myOut.WriteLine(entry.TimeGenerated & "," & entry.Message & "," & entry.MachineName)
                Console.WriteLine(entry.Message)
            Else
                If z Mod 100 = 0 Then
                    Console.Write(".")
                End If
            End If


        Next
        myOut.Close()


    End Sub


End Module

No comments: