Mike knows the pain and frustration of having a web server crash unexpectedly... it happens when we least expect it. In this article, he's going to describe a simple way to create an ASP page that will allow us to view our web servers log files in a standard web page across the Internet.
View Your Web Servers Log Files With ASP - The MSWC logging utility (Page 3 of 5 )
When Windows 2000 server is installed for the first time, a DLL named “logscrpt.dll” is also installed and registered. This DLL contains the MSWC.IISLog class, which allows us to load, read, write and close IIS log files on our server. To create an instance of the MSWC.IISLog class in ASP, we use the “MSWC.IISLog” ProgId, like this:
set objLog = Server.CreateObject("MSWC.IISLog")
The MSWC.IISLog class contains several methods and properties that can be used to either retrieve log entries, or write log entries to a log file. These methods and properties are described below:
AtEndOfLog: Indicates whether all records have been read from the log file.
CloseLogFiles: Closes all open log files.
OpenLogFile: Opens a log file for reading or writing.
ReadFilter: Filters records from the log file by date and time.
ReadLogRecord: Reads the next available log record from the current log file.
WriteLogRecord: Writes a log record to the current log file.
BytesReceived: Indicates the number of bytes received.
BytesSent: Indicates the number of bytes sent.
ClientIP: Indicates the client’s host name.
Cookie: Indicates the client’s cookie.
CustomFields: Indicates an array of custom headers.
DateTime: Indicates the date and time, in GMT.
Method: Indicates the operation type.
ProtocolStatus: Indicates the protocol status.
ProtocolVersion: Indicates the version string.
Referer: Indicates the referrer page.
ServerIP: Indicates the server’s IP address.
ServerName: Indicates the server name.
ServerPort: Indicates the port number.
ServiceName: Indicates the service name.
TimeTaken: Indicates the total processing time.
URIQuery: Indicates any parameters passed with the request.
URIStem: Indicates the target URL.
UserAgent: Indicates the user agent string.
UserName: Indicates the user’s name.
Win32Status: Indicates the Win32 status code.
We will use some of these methods and properties now, to create our log viewer script, viewlog.asp. This file is available for download as part of the support material for this article. It will contain two sub-routines: GetDetails and ShowLogEntries.
The GetDetails sub-routine will be called automatically whenever the viewlog.asp file is loaded for the first time. Its declaration looks like this:
It will display a HTML form with one text box, two drop-down lists, and a submit button. The text box will be where the user enters the file name for the log file they wish to view. This file name should contain a physical path, such as “c:\winnt\system32\logfiles\w3svc1\ex012808.log”. The first drop-down list will allow the user to select which type of log file they are viewing. The second drop-down list will allow the user to choose how many records to view.
The code for the GetDetails sub-routine is shown below: