Search code examples
vb.neteventsevent-log

read event log detail in vb.net


I'm writing a application at the moment, part of it will be scraping some information from the windows event log, it mostly works....

  Dim strValue As String
  Dim objLogs() As EventLog
  Dim Logname As String = "Application"
  Dim objEntry As EventLogEntry
  Dim objLogEntry As EventLogEntry
  Dim objLog As EventLog
  objLogs = EventLog.GetEventLogs()

  For Each objLog In objLogs
    If objLog.LogDisplayName = Logname Then
      For Each objLogEntry In objLog.Entries
        WriteLine("EventID")
        WriteLine("Machinename")
        WriteLine("message")
      Next
    Exit For
  End If
  Next

This will happily write out the EventID, machine name and event message. details tab in event viewer

What I can't figure out is how to output the "details" tab in event viewer ideal into strings or similar.

MSDN isn't being helpful, could anyone point me in the right direction please?

Thanks in advance,


Solution

  • Looks like the details in the EventLogEntry is represented by the Data property that is stored as a byte array. You would have you would have to then converted to something readable. But the format of the data seems to vary on the Windows OS version.

    Here an alternative way to do it. Code copied from answer to from the following question and converted to VB.Net.

    Serializing a .NET EventLogEntry instance to XML

    Imports System.Diagnostics.Eventing.Reader
    
    Sub Main()
        Dim query As New EventLogQuery("System", PathType.LogName)
        Dim watcher As New EventLogWatcher(query)
    
        AddHandler watcher.EventRecordWritten, AddressOf watcher_EventRecordWritten
        watcher.Enabled = True
        Console.ReadLine()
    End Sub
    
    Public Sub watcher_EventRecordWritten(sender As Object, e As EventRecordWrittenEventArgs)
        Console.WriteLine(e.EventRecord.ToXml())
    End Sub
    

    Put in the main module of a simple console application and an watches for system events. Writing out the event data converted to XML as one long string.

    Worked using 4.5 framework on a Windows 7 machine.