Easily write to the Windows Event Log

By | 2018-01-13

This module allows you to write text to the Windows Event Log with with a single method call. Includes options for Type, Source, Category, and Event.

Usage

LogEvent([Applicaiton Name], [EventLog Type], [Msg to be logged], [Optional Event ID], [Optional Category Number])

Example

Dim ret as Long
ret = LogEvent("TestApp1", EVENTLOG_SUCCESS, "Success event!!!!")

Code

Copy the entire code below into a VB Module. You can then add the module to a project and use a single function to write to the Event Viewer.

'******************************************************************
'Information compiled from MSDN Articles on Platform SDK: Debugging and Error Handling
'MSDN Home > MSDN Library > SDK Documentation > Event Logging > _
' Event Logging Reference > Event Logging Functions
'
'FUNCTIONS:
'Public Function LogEvent(strSource As String, dEventType As Long, _
'    strError As String, Optional iEventID As Integer = 26, _
'    Optional iCategory As Integer = 26) As Long
'
' returns nonzero for success otherwize zero for fail
'
'Module by Antonio Zavala, antonio.zavala@usaa.com
'United Services Automobile Association, 10/2002
'******************************************************************
Private Declare Function RegisterEventSource Lib "advapi32" Alias _
    "RegisterEventSourceA" (ByVal lpUNCServerName As String, ByVal lpSourceName As String) As Long

Private Declare Function DeregisterEventSource Lib "advapi32" _
    (ByVal hEventLog As Long) As Boolean

Private Declare Function ReportEvent _
    Lib "advapi32" Alias "ReportEventA" _
    (ByVal hEventLog As Long, _
    ByVal wType As Long, _
    ByVal wCategory As Long, _
    ByVal dwEventID As Long, _
    ByVal lpUserSid As Long, _
    ByVal wNumStrings As Long, _
    ByVal dwDataSize As Long, _
    lpStrings As Any, _
    lpRawData As Any) As Long

' Event Type Constants
Public Const EVENTLOG_SUCCESS = &H0            'Success event
Public Const EVENTLOG_ERROR_TYPE = &H1         'Error event
Public Const EVENTLOG_WARNING_TYPE = &H2     'Warning event
Public Const EVENTLOG_INFORMATION_TYPE = &H4 'Information event
Public Const EVENTLOG_AUDIT_SUCCESS = &H8     'Success audit event
Public Const EVENTLOG_AUDIT_FAILURE = &H10     'Failure audit event

Public Function LogEvent(strSource As String, dEventType As Long, strError As String, Optional iEventID As Integer = 26, Optional iCategory As Integer = 26) As Long
' returns nonzero for success otherwize zero for fail
Dim ret1, ret2, hEvt As Long
hEvt = RegisterEventSource(vbNullChar, strSource)
If hEvt Then
    ret1 = ReportEvent(hEvt, dEventType, iCategory, iEventID, 0, 1, 0, strError, vbNullChar)
    If Not ret1 Then
        LogEvent = 0
        Exit Function
    End If
End If
LogEvent = DeregisterEventSource(hEvt)
End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.