Previous Section
 < Day Day Up > 
Next Section


Writing Custom Events to the Event Logs

Whenever you work with automated scripts, scheduled tasks, or custom applications, you might want those scripts, tasks, or applications to write custom events to the event logs. For example, if a script runs normally, you might want to write an informational event in the application log that specifies this so it is easier to determine that the script ran and completed normally. Similarly, if a script doesn’t run normally and generates errors, you might want to log an error or warning event in the application log so that you’ll know to examine the script and determine what happened.

Tip 

You can track errors that occur in scripts using %ErrorLevel%. This environment variable tracks the exit code of the most recently used command. If the command executes normally, the error level is zero (0). If an error occurs while executing the command, the error level is set to a nonzero value. To learn more about working with error levels, see the section of Chapter 3 titled, “Getting Acquainted with Variables.”

To create custom events, you’ll use the Eventcreate utility. Custom events can be logged in any available log except the security log, and can include the event source, ID and description you want to use. The syntax for Eventcreate is

eventcreate /l LogName /so EventSource /t EventType /id EventID /d 
EventDescr

where

To see how Eventcreate can be used, consider the following examples:

Create an information event in the application log with the source Event Tracker and event ID 209:

eventcreate /l "application" /t information /so "Event Tracker" /id 
209 /d "evs.bat script ran without errors."

Create a warning event in the system log with the source CustApp and event ID 511:

eventcreate /l "system" /t warning /so "CustApp" /id 511 /d 
"sysck.exe didn't complete successfully."

Create an error event in the system log on MAILER1 with the source “SysMon” and event ID 918:

eventcreate /s Mailer1 /l "system" /t error /so "SysMon" /id 918 /d 
"sysmon.exe was unable to verify write operation."


Previous Section
 < Day Day Up > 
Next Section