Error Trapping and Capturing Third-Party Output in WSH - Logging to the Event Log
(Page 3 of 4 )
Logging to a simple log file may not always be enough. If you were performing system processes or automating applications you may wish to keep an error record in the actual Windows Event Log.
Value | Event Type |
0 | SUCCESS |
1 | ERROR |
2 | WARNING |
4 | INFORMATION |
8 | AUDIT_SUCCESS |
16 | AUDIT_FAILURE |
Luckily, WSH provides an easy way for us to do that as well. The WshShell object has a LogEvent method that allows us to record our events in the Application Event Log.
The LogEvent method can log to either the local computer or to a remote system. This is nice if you are monitoring several different machines at the same time from one location such as a server. Keep in mind also that event logging is always enabled on any Microsoft Windows system.
object.LogEvent value, string[, computername]
The LogEvent method accepts two required attributes. The first is the numerical value for the Event Type. You can find the list of possible values in the table above. The second attribute is the text string that contains the description for the event. A third optional attribute is used to specify the name of a remote computer to log the event to.
Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.LogEvent 0,"Test Success Event"
objShell.LogEvent 1,"Test Error Event"
objShell.LogEvent 2,"Test Warning Event"
objShell.LogEvent 4, "Test Information Event"
objShell.LogEvent 8, "Test Success Audit Event"
objShell.LogEvent 16, "Test Failure Audit Event"
The above code will log one of each of the event types to the Applications Event Log. You can verify this by clicking start, Run…, and typing eventvwr.msc in the Run dialog box to start the system's Event Viewer. Select Application in the left pane to view the Application log.
All events logged in this manner will have the Source “WSH” for Windows Script Host. The Event ID will match the Event Type provided.
Next: Capturing Third-Party Output >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer