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:
Methods- 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.
Properties- 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:
Sub GetDetails()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:
sub GetDetails()
'This sub-routine will display a simple HTML form
'into which the user will enter the location of
'the log file that we will be viewing.
%>
<form name="frmGetFile" action="<%=Request.ServerVariables("SCRIPT_NAME")%>" method="post">
<h1>Log File Details</h1>
Please enter the name and full path to the log file
that you would like to view:<br>
<input type="text" name="txtLogFile">
<br><br>
What type of log file will you be viewing?<br>
<select name="txtType">
<option SELECTED>W3C Extended Log File Format</option>
<option>NCSA Common Log File Format</option>
<option>Microsoft IIS Log File Format</option>
</select>
<br><br>
How many records do you want returned?<br>
<select name="intRecs">
<option>10</option>
<option>20</option>
<option>30</option>
<option>40</option>
<option>50</option>
<option>100</option>
<option>150</option>
<option>200</option>
<option>500</option>
<option>1000</option>
<option value="10000" SELECTED>ALL</option>
</select>
<br><br>
<input type="submit" name="submitted" value="View Log">
</form>
<%
end sub
The HTML output from the GetDetails sub-routine looks like this:

Once the user completes the details form and clicks on the “View Log” button, the ShowLogEntries sub-routine is called. I will describe this sub-routine on the next page.
Next: The ShowLogEntries sub-routine >>
More ASP Articles
More By