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.
The 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:
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:
Secondly, 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 = 0
Thirdly, 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:
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:
Next, 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>"
As 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:
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
The 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.Method
It 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:
counter = counter + 1
Once 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.