Commands relating to reading and writing the Windows event log
This module provides commands for reading and writing the Windows event log. The following commands, although described here, are available in the twapi_base package itself and do not require this module to be loaded: eventlog_open, eventlog_log, eventlog_write and eventlog_close. This allows applications to write to the event log without this module being loaded.
The Windows event log provides a common application-independent mechanism for applications to log messages for diagnostic and informational purposes. The infrastructure and API's supporting this mechanism changed starting with Windows Vista. Although the older Windows API's will also work on Vista and later versions, they are limited in their support for events logged by applications using the newer API's.
TWAPI follows the Windows model by providing two sets of commands corresponding to the two Windows API sets and in addition also provides a set of unified commands:
For reading the event log, use of the winlog set of commands is recommended as those will use the underlying API that is most appropriate for that OS version. Alternatively, if platform-specific features not available through those commands are required, applications can call the eventlog or evt commands as appropriate after checking the operating system version.
This section describes the use of the older pre-Vista event log commands. Most access to the event log using this API requires a handle returned by the eventlog_open command. A handle allows either read or write access, but never both. Returned handles must be closed when no longer required by calling eventlog_close.
Events can be written using the eventlog_write command. The eventlog_log command provides an alternate, simpler but less flexible, interface for writing events.
Events can be read from an event log through the eventlog_read command. The returned event record can be formatted using eventlog_format_message and eventlog_format_category.
The command eventlog_count returns the number of records in the event log. eventlog_oldest returns the record number of the oldest record. The command eventlog_is_full indicates whether the event log is full.
To monitor the event log in real time, an application can poll at regular intervals. However, a more efficient way is to set up notifications through the eventlog_subscribe command.
Event logs can be backed up using eventlog_backup or as a side effect of clearing the log through eventlog_clear. The backup file can be read using the standard eventlog_open and eventlog_read commands.
This section describes the use of the newer event log commands available in Windows Vista and later Windows version. The corresponding Windows API functions are prefixed with Evt in the Windows SDK. TWAPI does not support writing events using the new API. Only reading of events is supported.
The new event logging subsystem in Windows Vista is considerably more flexible (and complex) than what was available in earlier Windows versions. The description below assumes the reader is familiar with the terms and concepts described in the Windows SDK.
In the Vista event logging system, channels implement a event stream to which event publishers can write events. The commands evt_channels and evt_publishers return the names of the channels and publishers on a system respectively.
Event consumers can consume events from channels in real time or from a saved event file using the the evt_query command. This command returns a handle to a result set containing matching events. These can then be retrieved and processed using the evt_next and evt_decode_events or evt_decode_event.
All handles returned by the evt_* commands, such as those returned from evt_next and evt_query must be closed by passing them to evt_close.
To monitor the event log in real time, use the evt_subscribe command.
This section describes the use of the TWAPI's common interface to the old and new versions of Windows event log APIs. This is the recommended interface for consuming events but does not provide a command to write events.
The common access commands require a handle to the event log which can be obtained via the winlog_open command. The returned handle must be closed when no longer required by calling winlog_close.
Events can be read from an event log through the winlog_read command which returns a list of dictionaries containing the fields of each event. winlog_event_count returns the number of events in an event log.
Events can be written to a backup file using winlog_backup and cleared with winlog_clear.
To monitor the event log in real time, use the winlog_subscribe command.
-langid LANGID | Passed to the format_message command for formatting. See the description of that function for details. |
-width MAXLINEWIDTH | Passed to the format_message command for formatting. See the description of that function for details. |
-langid LANGID | Passed to the format_message command for formatting. See the description of that function for details. |
-width MAXLINEWIDTH | Passed to the format_message command for formatting. See the description of that function for details. |
-system SYSTEMNAME | Specifies the system on which the event log resides. By default, this is the local system. |
-source SOURCENAME | Specifies a event log source. The returned handle will refer to the event log to which events from SOURCENAME are logged. This defaults to the filename portion (without the path or extension) of the executable file. |
-type EVENTTYPE | Indicates the type or severity of the event. EVENTTYPE should be one of success, error, warning, information (default), auditsuccess or auditfailure. |
-category CATEGORY | This should be a integer corresponding to a category id in the event source's category or message file. This value defaults to 0. |
-file EVENTLOGBACKUPFILE | Specifies the name of a backed up event log file. This option may not be used with the -source or -system options. |
-source SOURCENAME | Specifies a event log source. The returned handle will refer to the event log to which events from SOURCENAME are logged. This defaults to the filename portion (without the path or extension) of the executable file. This option may not be used with the -file option. |
-system SYSTEMNAME | Specifies the system on which the event log resides. By default, this is the local system. This option cannot be used with the -file option. |
-write | Returns a handle used for writing to the event log. By default, a read handle is returned. Note that event log handles are never read-write. Applications wishing to do both need to open two separate handles. This option may not be used with the -file option as event log backup files cannot be written to. |
-seek RECORDNUMBER | Normally the command returns event log records starting from current read pointer. If this option is specified, the command will return records starting with the one with record number RECORDNUMBER. |
-direction backward|forward | Controls the direction in which the read pointer moves. By default, the commands will return records reading forwards from the current read pointer. |
-category | This is an integer corresponding to a category id in the event source's category or message file. |
-data | Raw binary data stored as part of the event record. |
-eventid | The event id that identifies the event type. See eventlog_write for how this may be used. |
-params | A list of strings corresponding to the positional %N format specifiers in the format string for the event in the message file. |
-sid | The SID of the user account of the thread that logged the event. This may be a null string if this information was not included in the record. |
-recordnum | The record number for this record. This may be used with the -seek option to locate a record. |
-source | The event source that logged the event. |
-system | The name of the system on which the event was generated. |
-timegenerated | Contains the time that the event was generated (UTC) expressed as an offset in seconds since 00:00:00 Jan 1, 1970. This can be passed to the clock format command to convert to an absolute time. |
-timewritten | Contains the time (UTC) that the event was written into the event log expressed as an offset in seconds since 00:00:00 Jan 1, 1970. This can be passed to the clock format command to convert to an absolute time. |
-type | Indicates the type or severity of the event. The value is one of success, error, warning, information, auditsuccess or auditfailure. |
-type EVENTTYPE | Indicates the type or severity of the event. EVENTTYPE should be one of success, error, warning, information (default), auditsuccess or auditfailure. Note that the last two may only be used when writing to the Security event log. |
-category CATEGORY | This should be a integer corresponding to a category id in the event source's category or message file. This value defaults to 1. |
-loguser | Specifying this will cause the SID of the user account for this thread to be included in the event record. |
-params PARAMLIST | Specifies a list of strings corresponding to the positional %N format specifiers in the format string for the event in the message file. If the source has not been configured with a message file in the registry, event viewers will generally display this as a list of strings. |
-data DATA | Raw binary data that should be stored as part of the event record. |
-hpublisher HPUBLISHER | A handle to the event publisher metadata. If not specified, the publisher metadata for the event is extracted from the event itself. |
-values HVALUES | By default, the command will pick any values to be inserted into the formatted event string from the event itself. If this option is specified, the values will instead be picked from HVALUES which must be a handle to a render values block. |
-activityid | GUID containing the activity id associated with the event. |
-channel | The channel to which the event was logged. |
-computer | The name of the system on which the event was logged. |
-eventid | The event id for the event's definition. |
-eventrecordid | The id of this event record. |
-keywordmask | An integer mask with bits corresponding to the event's keywords. |
-level | The numeric level for the event. |
-opcode | The numeric opcode for the event. |
-pid | The process id of the process that logged the event. |
-providerguid | The GUID of the logging event provider. |
-providername | The name of the logging event provider. |
-qualifiers | |
-relatedactivityid | GUID containing the relative activity id associated with the event. |
-task | The numeric task for the event. |
-tid | The thread id of the logging thread. |
-timecreated | The time the event was logged in 100ns units since January 1, 1601 UTC. |
-sid | The SID of the account under which the logging thread was running. |
-version | The version of the event definition. |
-keywords | Contains a list of keywords with which the event is tagged. |
-levelname | Contains the string associated by the publisher with the numeric level. |
-message | Contains the formatted message corresponding to the event. |
-opcodename | Contains the string associated by the publisher with the numeric opcode field. |
-taskname | Contains the string associated by the publisher with the numeric task field. |
-xml | Contains an XML fragment describing the event. |
-ignorestring STRING | If specified, STRING is returned for any field that cannot be obtained. Without this option, errors retrieving fields will generate a Tcl exception. |
-lcid LCID | Specifies the language in which the message is to be formatted. Defaults to the locale of the calling thread. This option is ignored if the -hpublisher option is specified. |
-logfile PATH | Specifies the path to the logfile containing archived publisher metadata for the event. This option is ignored if the -hpublisher option is specified. |
-session HSESSION | Specifies the session to the event logging subsystem for the event. By default the local event logging subsystem is used. |
-channel CHANNELNAME | Specifies the event channel from which events are to be read. Cannot be used with the -file option. |
-file PATH | Specifies the event file from which events are to be read. Cannot be used with the -channel option. |
-ignorequeryerrors | Normally, if retrieving a specific event results in an error, Windows will fail the query and a Tcl exception will be raised. If -ignorequeryerrors is specified, such events are simply skipped and any further events are returned. |
-query QUERYSTRING | Specifies the selection criteria for events that should be included in the returned result set. QUERYSTRING should be * (for all events, default), an XPATH expression or a structured XML query. See the Windows SDK for details. |
-count COUNT | Returns COUNT events from the result set. The actual number of events returned may be less if the end of the result set is reached. If unspecified, the number of returned events depends on various internal tuning factors and applications must not rely on a specific number being returned. |
-status VARNAME | The name of a variable in which to return status. If there is an error when reading the next event, the command will normally generate an Tcl exception. If this option is specified, the command will instead return an empty list and store the error code in the specified variable. |
-timeout MILLISECONDS | Specifies the number of milliseconds to wait for events. A value of -1 (default) indicates call should not time out. |
-channel CHANNELNAME | Specifies the event channel from which events are to be read. Cannot be used with the -file option. |
-direction DIRECTION | If DIRECTION is forward events are returned oldest first. If DIRECTION is backward, events are returned newest first. |
-file PATH | Specifies the event file from which events are to be read. Cannot be used with the -channel option. |
-ignorequeryerrors | Normally, if retrieving a specific event results in an error, Windows will fail the query and a Tcl exception will be raised. If -ignorequeryerrors is specified, such events are simply skipped and any further events are returned. |
-query QUERYSTRING | Specifies the selection criteria for events that should be included in the returned result set. QUERYSTRING should be * (for all events, default), an XPATH expression or a structured XML query. See the Windows SDK for details. |
-ignorequeryerrors | Normally, if retrieving a specific event results in an error, Windows will fail the query and a Tcl exception will be raised. If this option is specified, such events are simply skipped and any further events are returned. |
-includeexisting | By default the command will only subscribe to new events. If this option is specified, notification is sent for existing events as well. |
-query QUERYSTRING | Specifies the selection criteria for events that should be included in the returned result set. QUERYSTRING should be * (for all events, default), an XPATH expression or a structured XML query. See the Windows SDK for details. |
-channel NAME | Specifies a event log source (pre-Vista) or channel (Vista and later) from which events are to be read. This option may not be used with the -file option. |
-file EVENTLOGBACKUPFILE | Specifies the name of a backed up event log file. This option may not be used with the -channel or -system options. |
-system SYSTEMNAME | Specifies the system on which the event log resides. By default, this is the local system. This option cannot be used with the -file option. |
-channel NAME | Specifies a event log source (pre-Vista) or channel (Vista and later) from which events are to be read. This option may not be used with the -file option. |
-direction DIRECTION | If DIRECTION is forward events are returned oldest first. If DIRECTION is backward, events are returned newest first. |
-file EVENTLOGBACKUPFILE | Specifies the name of a backed up event log file. This option may not be used with the -channel or -system options. |
-system SYSTEMNAME | Specifies the system on which the event log resides. By default, this is the local system. This option cannot be used with the -file option. |
Copyright © 2004-2014, Ashok P. Nadkarni