Programming | Advanced 6.2.11 Logging Utility components
The Logging utility component, a new component of ASP 3.0, provides a way to access the log file that is created by the server-supported IIS for Web and FTP Web sites.
1. Members of the Logging utility component
The Logging utility component provides six ways to read the contents of log files and write new records into existing log files. These methods are similar to the FileSystemObject objects discussed in the previous chapter, as shown in table 6-9:
Table 6-9 method and description of Logging utility component
Method
Description
Atendoflog ()
The return value is true if all records in the file are read out when you traverse the record with the ReadLogRecord method
Closelogfiles (Io_mode)
Closes the open log file, the Io_mode value specifies the file to be closed, and the constant ForReading (1) indicates that all files opened for reading are closed, and ForWriting (2) Closes all files opened for writing, Allopenfiles (32) Indicates that all open files are closed
OpenLogFile (Filename,io_mode,
Service_name,service_instance,
Output_format)
Open the log file or set of log files specified by filename, optionally io_mode parameters can be set to ForReading (default value, 1) or ForWriting (2). Optional service_name and Service_instance parameters limit the records returned from a file when fetching a particular IIS service (such as "W3SVC" and "1"). The optional Output_format parameter indicates the format to use when writing to the file
ReadFilter (Start,end)
When reading a file within a specified time and date range, limit the records returned, two parameters are optional, and if omitted, start is the first record of the file, and end is the last record
ReadLogRecord ()
Reading the next record from the current log file opened with the OpenLogFile method
Writelogrecord (Logging_object)
Writes a record to a file opened for writing from a file open for reading, parameter logging_object is an object variable that references the Logging Utility component instance that holds the source record
The Logging utility component provides 20 properties that correspond to IIS logging, as shown in table 6-10:
Table 6-10 Properties and descriptions of Logging utility components
Property
Description
Bytesreceived
The number of bytes received from the browser at request
BytesSent
Number of bytes sent to the browser when responding
ClientIP
IP address of the client or its host (that is, the proxy server)
Cookies
The content of the cookie sent in the request
Custumfields
An array of custom headers added to the request
Datatime
Date and time of request (GMT)
Method
Type of action, such as "Get" and "Post"
Protocolstatus
Returns the status message to the client, that is, "OK"
ProtocolVersion
Protocol version string, such as "http/1.1"
Referer
The URL of the Web page that contains the initialization request link
ServerIP
IP address of the IIS machine
ServerName
The machine name of the IIS server
ServerPort
The port number on which the request arrived, such as "80"
ServiceName
Server name, such as "msftpsvc" or "w3svc"
Timetaken
Total processing time for retrieving and creating Web pages
URIQuery
All parameters in the query string that are added to the URL in the request
Uristem
Target URL for instructions
UserAgent
User agent string sent by the client
UserName
If you are not accessing the server anonymously, username is the user's login name
Win32Status
WIN32 status code returned after processing of the request
2. Using the Logging Utility component
The most common application of Logging utility components is the custom query for log files. The ability to select records to write new files from an existing file means that certain types of entries can be aggregated or selectively selected for future use.
In order to use the ForReading, ForWriting, and Allopenfiles constants, the metadata directives must be included in the <HEAD> section of the Web page.
<!--METADATA type= "typelib" file= "C:\WINNT\system32\inetsrv\logscrpt.dll"-->
In order to traverse the record, only the file must be opened and ReadLogRecord will be called repeatedly until the Atendoflog return value is true. In the following example, the records are filtered and only recorded within the last 24 hours.
' Create the component instance
Set objlogutil = Server.CreateObject ("MSWC. IISLog ")
' Open ' log file for reading, for the W3SVC instance number 1
Objlogutil.openlogfile "Extend#.log", ForReading, "w3svc", 1, 0
' Set a filter for the ' Records ' s
Objlogutil.readfilter DateAdd ("D",-1, now), now
' Loop through the records
Do as not Objlogutil.atendoflog
Objlogutil.readlogrecord ' Read the next record
Response.Write ' Request received for page ' & Objlogutil.urlstem & ' on ' _
& Objlogutil.datetime & "From IP Address" _
& Objlogutil.clientip & ".<br>"
Loop
Objlogutil.closelogfiles (ForReading) ' Close the ' file (s)
This book provides a sample Web page that demonstrates the use of the logging utility component, which can be run from the ASP Installable Components main menu.
Make sure that you do not allow anonymous access to the directory or loggingutility.asp files that contain the sample files in Internet Services Manager, otherwise you will not be able to access the log files. Open the Directory Security tab of the Loggingutility.asp file or the Properties dialog box for the directory containing the file, and click Anonymous access and Authentication The edit button of the control section opens the Authentication Method dialog box and closes the anonymous access option.
You must change the file name of the log file to fit your computer. In the Default Web