Articles > Development
Printer Friendly Version
Views: 9742

Event Logs: Convert InstanceID to EventID in VB.net

Last Updated: 10/24/08

 Function EventInstanceToID(ByRef lInstance As Long) As String
        Dim lEventID As Int32

        'Take just the integer part 

        lEventID = CInt(lInstance And CLng(Int32.MaxValue))

        'Remove Top Two Bits

        lEventID = (lEventID And &H3FFFFFFF)

        'Return the evenid if event is recognized,
        ' if event id is specific to app only return last digit

        If lEventID.ToString.Length > 5 Then
            Return (lEventID.ToString.Substring(lEventID.ToString.Length - 1, 1))
        Else
            Return (lEventID.ToString)
        End If


    End Function
MSDN: EventLogEntry.EventID Property


You might find this code helpful too:
 ' Define the WMI query
        '
        myQuery = "SELECT * FROM Win32_NTLogEvent WHERE " & _
                  "(Logfile='Application' OR LogFile='System') AND " & _
                  "timegenerated > '" & strLastRun & "'"

        '
        ' Execute the query and create the objInstance object
        '
        objInstance = objservice.ExecQuery(myQuery)

        For Each item In objInstance
            dDate = EventDate(item.timegenerated)
            If DateDiff(DateInterval.Day, dDate, Now()) < 4 Then
                iCounter = iCounter + 1
                ReDim Preserve myEvents(iCounter)
                myEvents(iCounter - 1).Source = item.sourcename
                myEvents(iCounter - 1).EventID = CStr(item.eventcode)
                myEvents(iCounter - 1).LogType = item.LogFile
                Try
                    myEvents(iCounter - 1).Message = CStr(item.Message)
                Catch ex As Exception
                    myEvents(iCounter - 1).Message = ""
                End Try
            End If
        Next 




Keywords: vb.net 2005 vs visual studio event logs .eventid depreciated