Tracking Events in Global.asa

Debugging Session and Application events can be tricky, because you can't use the Response.Write method to display messages to the client browser. One useful technique is to use the Scripting.FileSystemObject method to generate a simple log file with text messages indicating the success or failure of these events, as demonstrated in the following script:

<!--METADATA TYPE="TypeLib"
NAME="Scripting" FILE="C:\Winnt\System32\scrrun.dll"
UUID="420B2830-E718-11CF-893D-00A0C9054228" VERSION="1.0"
-->

<object ID="AppFileSystemObject" SCOPE=Application RUNAT=Server
   PROGID="Scripting.FileSystemObject">
</object>

<script LANGUAGE=VBScript RUNAT=Server>
Sub OutputDebugFile(ByRef strText)
   Dim ts
   Application.Lock
   Set ts = AppFileSystemObject.OpenTextFile(Application("DebugFile"),_
            ForAppending, True, TristateUseDefault)
   ts.WriteLine Now & ": " & strText
   ts.Close
   Application.Unlock
End Sub

Sub Application_OnStart
   '--- DebugFile must be defined before calling OutputDebugFile
   Application("DebugFile") = "c:\webs\appevnts.log"
   OutputDebugFile("Application Started")
End Sub

Sub Application_OnEnd
   OutputDebugFile("Application Ended")
End Sub

Sub Session_OnStart
   OutputDebugFile("Session Started: " & Session.SessionID)
   Response.Redirect "./end.asp"    ' End Session (this creates a loop!)
End Sub

Sub Session_OnEnd
   OutputDebugFile("Session Ended: " & Session.SessionID)
End Sub
</script>

The preceding script writes notifications for the Application start and end events, and notifications for every Session start and end event.