Search the Site

Sponsors

bottom corner

Logging Windows events with VBScript

Using Windows Scripting Host (WSH), we can insert entries into the Windows application event log for later debugging or other purposes.

This page is filed under keyword(s): windows, vbscript.

The VBScript code to perform the insertion into the event log is shown below. Please note that the "event_type" input variable will only take the following three values, in upper case letters:

  • "ERROR" - Use when logging a problem
  • "WARN" - Use when logging a minor issue
  • "INFO" - Use when logging miscellaneous information

When we run the example code further below, we will find the log as seen here.

Example Code

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Description: This sample code demonstrates how to log information 
'       to the Windows application event log
' Author: C. Peter Chen, http://dev-notes.com
'
' Note: Allowable event_type values:
'         "ERROR" - Use when logging a problem
'         "WARN"  - Use when logging a minor issue
'         "INFO"  - Use when logging miscellaneous information
'
' Revision History:
'	1.0	20090310	Original release
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
option explicit

sub logWindowsEventViewer(event_type, event_text)
	dim WshShell, tp
	
	select case event_type
	case "ERROR"
		tp = "1"
	case "WARN"
		tp = "2"
	case "INFO"
		tp = "4"
	case else
		tp = "0"
	end select

	set WshShell = CreateObject("WScript.Shell")
	wshshell.Logevent tp, event_text
	set wshshell=nothing
end sub

' Usage example:
dim event_type, event_text
event_type = "ERROR"
event_text = "Application error: GUI crashed!"

call logWindowsEventViewer(event_type, event_text)

Did you find this page useful? Please consider browsing other articles or subscribing to the RSS feed to keep up with latest.

This page is filed under keyword(s): windows, vbscript.
Author: C. Peter Chen
Last updated: 10 Mar 2009

bottom corner