View Your Web Servers Log Files With ASP - The ShowLogEntries sub-routine
(Page 4 of 5 )
Once the user submits the details form generated by the GetDetails sub-routine, the page calls our ShowLogEntries sub-routine using the simple piece of code shown below:
if Request.Form("submitted") = "" then
GetDetails
else
ShowLogEntries Request.Form("txtLogFile"), Request.Form("txtType"), Request.Form("intRecs")
end ifThe value of the “submitted” form variable is set to “View Log” once the details form is submitted. This is because the submit button contains a “submitted” value, and ASP passes that variable and its value to our ASP page as well:
<input type="submit" name="submitted" value="View Log">The declaration of the ShowLogEntries sub-routine looks like this:
sub ShowLogEntries(logFile, logType, logNumRecs)Each of the compulsory arguments that need to be passed to the ShowLogEntries sub-routine are shown below:
- logFile: The physical path and filename of the log file to load. If it is empty, or doesn’t end in “.log”, then an error message will be displayed in the users web browser
- logType: The type of log file to load. Possible values are “W3C Extended Log File Format”, “NCSA Common Log File Format”, or “Microsoft IIS Log File Format”.
- logNumRecs: The number of log entries to return on one page.
The ShowLogEntries sub-routine starts out by dimensioning several variables that will be used to hold the details of each individual log entry as we loop through the log file:
dim objLog
dim clientIP
dim dateTime
dim url
dim serverPort
dim method
dim counterSecondly, we create two constant variables that will be used to set the access mode when we open the log file. We also set the counter variable to zero. The counter variable will be used to keep track of how many log entries we have retrieved so far:
const ForReading = 1
const ForWriting = 2
counter = 0Thirdly, because we can only pass variants to sub-routines and functions in ASP, we cast the logNumRecs argument to an integer:
logNumRecs = CInt(logNumRecs)Next, we create a new instance of the MSWC.IISLog class and call its OpenLogFile method. The OpenLogFile method accepts five arguments. The declaration and argument descriptions for the OpenLogFile method are shown below:
OpenLogFile( fileName, [IOMode], [serviceName], [serviceInstance], [outputLogFileFormat] )- fileName: Name of the log file to open
- IOMode: Optional parameter indicating whether the log file is opened for reading or writing. This parameter can be either ForReading (1), or ForWriting (2). If it is omitted, then the log file is opened for reading.
- serviceName: Optional parameter indicating the logging module should only return records matching this service.
- serviceInstance: Optional parameter indicating the logging module should only return records matching this server instance.
- OutputLogFileFormat: Optional parameter indicating the format for log files opened for writing.
In our “viewlog.asp” page, we use the following code to create an instance of the MSWC.IISLog class and open the log file:
set objLog = Server.CreateObject("MSWC.IISLog")
objLog.OpenLogFile logFile, ForReading, "W3SVC", ForReading, logTypeNext, we check the VBScript “err” class to see whether or not the log file was loaded successfully. We also make sure that the user has selected a file with the extension “.log”. If the log file was loaded successfully, then err.number will be zero. If not, it will be equal to 80070002 in hexadecimal:
if hex(err.number) = 80070002 OR logFile = "" OR Right(logFile, 4) <> ".log" then
Response.Write "<h1>File doesn't exist</h1>"
else
objLog.ReadLogRecordAs you can see from the code sample above, when the log file is loaded successfully, we call the log objects “ReadLogRecord” method. This method grabs the next log entry from the log file and extracts each of its values into the corresponding member variables of our log object.
For example, once we call the “ReadLogRecord” method, the next log entry is read. The ClientIP member of our log object will now contain the clients IP address, which was extracted from that log entry.
The ShowLogEntries sub-routine uses a HTML table to display each entry:
<h3><%=logFile%></h3>
<table width="95%" align="center" bgcolor="lightyellow" border="0">
<tr>
<td width="15%" bgcolor="yellow">
<b>Client IP</b>
</td>
<td width="15%" bgcolor="yellow">
<b>Date</b>
</td>
<td width="20%" bgcolor="yellow">
<b>URL</b>
</td>
<td width="15%" bgcolor="yellow">
<b>Port</b>
</td>
<td width="15%" bgcolor="yellow">
<b>Method</b>
</td>
</tr>The code above will create the initial row of headings for our table. It will also display the name of the log file we are currently viewing:

The ShowLogEntries sub-routine will display each log entry as a table row. To do this, our script loops through each log entry using the ReadLogRecord method of our log object. When we get to the end of the file, the AtEndOfLog method will return true.
Remember also, that our ShowLogEntries sub-routine accepts the maximum number of records to display in the table. We use this, as well as the AtEndOfLog method to create a while…wend loop, like this:
while (counter < logNumRecs) AND (not objLog.AtEndOfLog)
'Code to display the log entry
wendThe while loop starts by extracting some values for each log entry into our pre-dimensioned variables:
clientIP = objLog.ClientIP
dateTime = objLog.DateTime
url = objLog.URIStem
serverPort = objLog.ServerPort
method = objLog.MethodIt then adds a table row to the HTML output for the page, which displays the variables shown above. Lastly, the next log entry is fetched, and the counter variable is incremented by one:
<tr>
<td width="15%">
<%=clientIP%>
</td>
<td width="15%">
<%=dateTime%>
</td>
<td width="20%">
<a href="<%=url%>"><%=url%></a>
</td>
<td width="15%">
<%=serverPort%>
</td>
<td width="15%">
<%=method%>
</td>
</tr>
<%
objLog.ReadLogRecord
counter = counter + 1Once the while…wend loop is completed we have a nice looking table which contains each individual log entry:

Because the log file displayed above was taken from the devArticles.com development server, the entries are not very interesting. The client IP is always 127.0.0.1 (the local machine). The filename for each log entry is also a hyperlink directly to that file. Because all of our requests to the devArticles.com development server were to test web pages in our browser, the port is 80, and the method is “GET”, which simply means that we are retrieving a file from the web server.
Next: Conclusion >>
More ASP Articles
More By