.LogEvent - VBScript


.LogEvent

The .LogEvent command logs a custom event to the Windows Event Log. It is primarily used to record significant events or errors during script execution for diagnostic and troubleshooting purposes.

Syntax

.LogEvent [Source] [Category] [EventID] [Type] [Message]

Options/Flags

  • Source: The source of the event (default: script filename without extension).
  • Category: The event category (default: “Application”).
  • EventID: A unique identifier for the event (default: 1).
  • Type: The event type (default: “Error”). Valid types are:
    • Error
    • Warning
    • Information
    • SuccessAudit
    • FailureAudit
  • Message: The event message. Supports string interpolation using %x (e.g., %0, %1).

Examples

Simple logging:

.LogEvent "MyScript" "General" 1 "Information" "Script execution started."

Logging with string interpolation:

.LogEvent "MyScript" "Error" 1 "Error" "Failed to open file '%0'."

Common Issues

  • Access denied: Ensure the script has sufficient permissions to write to the Event Log.

Integration

The .LogEvent command can be integrated with other VB Script commands to provide additional logging capabilities. For example:

' Loop through files and log errors

For Each file In FilesCollection
  If Not FileExists(file) Then
    .LogEvent "MyScript" "File I/O" 1 "Error" "Failed to find file '%0'."
  End If
Next
  • .GetObject
  • .WScript.Echo