How to Append a Diagnostic Log in Classic ASP

For a small legacy application, Scripting.FileSystemObject can append diagnostic text to a file. Keep the log outside the website's publicly served directory and give write permission only to the identity that actually performs the file access.

Prepare a private destination

On the test server, create C:\AppData\LegacyDemo. Inspect the application's authentication and application-pool settings to identify its effective file-access account. Grant that account the needed directory access. Do not grant broad write access to the website or to Everyone.

Save the following as a test ASP page. It uses a fixed path rather than a path supplied by the request.

Visual Basic
<%@ Language="VBScript" %>
<% Option Explicit %>
<%
Const ForAppending = 8
Const TristateTrue = -1
Dim fileSystem, logFile

Set fileSystem = Server.CreateObject("Scripting.FileSystemObject")
Set logFile = fileSystem.OpenTextFile( _
    "C:\AppData\LegacyDemo\diagnostic.log", _
    ForAppending, True, TristateTrue)

logFile.WriteLine CStr(Now()) & " | diagnostic page requested"
logFile.Close
Set logFile = Nothing
Set fileSystem = Nothing

Response.ContentType = "text/plain"
Response.Write "One diagnostic line appended."
%>

Request the page twice. The file should contain two lines. The second request must preserve the first. True permits file creation, while TristateTrue selects the API's Unicode text format. Use the same format for every writer to this file. This is not a UTF-8 writer.

Handle failures deliberately

An absent directory or insufficient permissions must be treated as a failed append. Do not claim success after suppressing the error. If logging is optional, isolate the failing call with a short error handler and record its failure through an independent channel.

This small example does not implement concurrent-writer coordination, retention or rotation. Use a suitable logging component for sustained traffic. Never log passwords, authentication headers or complete form bodies merely to diagnose an unrelated request. Remove the test endpoint after verification.

References

OpenTextFile method (Visual Basic for Applications)
VBA language reference
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/opentextfile-method
OpenTextFile method
Server Object (IIS)
https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms525541(v=vs.90)
Server object

Author: Herman Schoenfeld