The Log Parser scriptable COM components offer numerous
advantages and more flexibility than the command-line executable
binary.
For example, with the Log Parser scriptable COM components we can
execute a query without providing an output format, retrieve the
result output records, and process the output records
ourselves.
The Log Parser scriptable COM components are implemented as Automation objects, which means that they can be used from any programming environment supporting automation, including C++, C#, Visual Basic, JScript and VBScript.
Tip: Before
using the Log Parser scriptable COM components on a computer, the
"LogParser.dll" binary should be registered with the
computer's COM infrastructure by executing the following command in
the directory containing the "LogParser.dll" binary:
C:\LogParser>regsvr32
LogParser.dll
The Log Parser scriptable COM components architecture is made up
of the following objects:
When writing an application that uses the Log Parser scriptable
COM components, the very first step should be the instantiation of
the MSUtil.LogQuery COM object.
The following JScript example shows how the MSUtil.LogQuery object
is instantiated by a JScript application:
var oLogQuery = new ActiveXObject("MSUtil.LogQuery");The following VBScript example shows how the MSUtil.LogQuery object is instantiated by a VBScript application:
Dim oLogQuery Set oLogQuery = CreateObject("MSUtil.LogQuery")Once the MSUtil.LogQuery COM object has been instantiated, an application would usually proceed by executing a query in either batch mode or interactive mode, depending on the task that needs to be accomplished.
A query executed in batch mode will have its output records
written directly to an output format.
Batch mode works in the same way as the commands used with the Log
Parser command-line executable, and it is useful when we want to
execute a query and have its results sent to an output format, with
no application intervention on the query output records.
A query is executed in batch mode by calling the ExecuteBatch method of
the MSUtil.LogQuery object. This method takes three
arguments:
The basic steps of an application using batch mode resemble the
commands used with the Log Parser command-line executable:
The following examples show a simple application that creates a
CSV file containing selected records from the event log.
After instantiating the main MSUtil.LogQuery object, the
application instantiates the MSUtil.EVTInputFormat input format
object, which implements the EVT input
format, and sets its direction
property to "BW", in order to read events from the latest to the
earliest.
Then, the application instantiates the MSUtil.CSVOutputFormat output
format object, which implements the CSV
output format, and sets its tabs
property to "ON", in order to improve readability of the CSV
file.
Finally, the application calls the ExecuteBatch method of
the MSUtil.LogQuery object, specifying the query, the input
format object, and the output format object; the method will
execute the query, reading from the event log and writing to the
specified CSV file, and will return when the query execution is
complete.
JScript example:
var oLogQuery = new ActiveXObject("MSUtil.LogQuery"); // Create Input Format object var oEVTInputFormat = new ActiveXObject("MSUtil.LogQuery.EventLogInputFormat"); oEVTInputFormat.direction = "BW"; // Create Output Format object var oCSVOutputFormat = new ActiveXObject("MSUtil.LogQuery.CSVOutputFormat"); oCSVOutputFormat.tabs = true; // Create query text var strQuery = "SELECT TimeGenerated, EventID INTO C:\\output.csv FROM System"; strQuery += " WHERE SourceName = 'Application Popup'"; // Execute query oLogQuery.ExecuteBatch( strQuery, oEVTInputFormat, oCSVOutputFormat );VBScript example:
Dim oLogQuery Dim oEVTInputFormat Dim oCSVOutputFormat Dim strQuery Set oLogQuery = CreateObject("MSUtil.LogQuery") ' Create Input Format object Set oEVTInputFormat = CreateObject("MSUtil.LogQuery.EventLogInputFormat") oEVTInputFormat.direction = "BW" ' Create Output Format object Set oCSVOutputFormat = CreateObject("MSUtil.LogQuery.CSVOutputFormat") oCSVOutputFormat.tabs = TRUE ' Create query text strQuery = "SELECT TimeGenerated, EventID INTO C:\output.csv FROM System" strQuery = strQuery & " WHERE SourceName = 'Application Popup'" ' Execute query oLogQuery.ExecuteBatch strQuery, oEVTInputFormat, oCSVOutputFormat
Queries executed in interactive mode do not use output formats,
but rather return their output records directly to the
application.
Interactive mode is useful when we want to execute a query and
receive the output records for custom processing.
A query is executed in interactive mode by calling the Execute method of the
MSUtil.LogQuery object. This method takes two
arguments:
The basic steps of an application using interactive mode
are:
The following examples show a simple application parsing an IIS
web site's logs and printing the output records to the console
output.
After instantiating the main MSUtil.LogQuery object, the
application instantiates the MSUtil.IISW3CInputFormat input
format object, which implements the IISW3C
input format.
Then, the application calls the Execute method of the
MSUtil.LogQuery object, specifying the query and the input
format object, and receiving the resulting LogRecordSet
object.
The LogRecordSet object is used in a loop to enumerate the
LogRecord objects implementing the query output records; the
application retrieves the first field from each LogRecord
object and prints it to the console output.
Finally, the application disposes of the LogRecordSet object
by calling its close
method.
JScript example:
var oLogQuery = new ActiveXObject("MSUtil.LogQuery"); // Create Input Format object var oIISW3CInputFormat = new ActiveXObject("MSUtil.LogQuery.IISW3CInputFormat"); // Create query text var strQuery = "SELECT c-ip FROM <1> WHERE cs-uri-stem LIKE '%hitcount.asp'"; // Execute query and receive a LogRecordSet var oRecordSet = oLogQuery.Execute( strQuery, oIISW3CInputFormat ); // Visit all records while( !oRecordSet.atEnd() ) { // Get a record var oRecord = oRecordSet.getRecord(); // Get first field value var strClientIp = oRecord.getValue( 0 ); // Print field value WScript.Echo( "Client IP Address: " + strClientIp ); // Advance LogRecordSet to next record oRecordSet.moveNext(); } // Close LogRecordSet oRecordSet.close();VBScript example:
Dim oLogQuery Dim oIISW3CInputFormat Dim strQuery Dim oRecordSet Dim oRecord Dim strClientIp Set oLogQuery = CreateObject("MSUtil.LogQuery") ' Create Input Format object Set oIISW3CInputFormat = CreateObject("MSUtil.LogQuery.IISW3CInputFormat") ' Create query text strQuery = "SELECT c-ip FROM <1> WHERE cs-uri-stem LIKE '%hitcount.asp'" ' Execute query and receive a LogRecordSet Set oRecordSet = oLogQuery.Execute ( strQuery, oIISW3CInputFormat ) ' Visit all records DO WHILE NOT oRecordSet.atEnd ' Get a record Set oRecord = oRecordSet.getRecord ' Get first field value strClientIp = oRecord.getValue ( 0 ) ' Print field value WScript.Echo "Client IP Address: " & strClientIp ' Advance LogRecordSet to next record oRecordSet.moveNext LOOP ' Close RecordSet oRecordSet.close
© 2004 Microsoft Corporation. All rights reserved.