In preparation for the debugging and tracing tools available to you in ASP.NET, you’ll now create a debug object that works in previous versions of ASP. For this example, you will need to have VBScript 5.0 or higher running on your server. VBScript 5.0 introduced classes into the scripting language, and you will be taking advantage of that here.
First, take a look at the code of your object, shown in Listing 2.4.
<style type="text/css"> span.trace__ { background-color:white; color:black;font: 10pt verdana, arial; } span.trace__ table { font: 10pt verdana, arial; cellspacing:0; cellpadding:0; margin-bottom:25;} span.trace__ tr.subhead { background-color:cccccc;} span.trace__ th { padding:0,3,0,3 } span.trace__ th.alt { background-color:black; color:white; padding:3,3,2,3; } span.trace__ td { padding:0,3,0,3 } span.trace__ tr.alt { background-color:eeeeee } span.trace__ h1 { font: 24pt verdana, arial; margin:0,0,0,0} span.trace__ h2 { font: 18pt verdana, arial; margin:0,0,0,0} span.trace__ h3 { font: 12pt verdana, arial; margin:0,0,0,0} span.trace__ th a { color:darkblue; font: 8pt verdana, arial; } span.trace__ a { color:darkblue;text-decoration:none } span.trace__ a:hover { color:darkblue;text-decoration:underline; } span.trace__ div.outer { width:90%; margin:15,15,15,15} span.trace__ table.viewmenu td { background-color:006699; color:white; padding:0,5 ,0,5; } span.trace__ table.viewmenu td.end { padding:0,0,0,0; } span.trace__ table.viewmenu a {color:white; font: 8pt verdana,arial; } span.trace__ table.viewmenu a:hover {color:white; font: 8pt verdana, arial; } span.trace__ a.tinylink {color:darkblue; font: 8pt verdana, arial; text-decoration :underline;} span.trace__ a.link {color:darkblue; text-decoration:underline;} span.trace__ div.buffer {padding-top:7; padding-bottom:17;} span.trace__ .small { font: 8pt verdana, arial } span.trace__ table td { padding-right:20 } span.trace__ table td.nopad { padding-right:5 } </style> <% Class clsDebug Dim mb_Enabled Dim md_RequestTime Dim md_FinishTime Dim mo_Storage Public Default Property Get Enabled() Enabled = mb_Enabled End Property Public Property Let Enabled(bNewValue) mb_Enabled = bNewValue End Property Private Sub Class_Initialize() md_RequestTime = Now() Set mo_Storage = Server.CreateObject("Scripting.Dictionary") End Sub Public Sub Print(label, output) If Enabled Then Call mo_Storage.Add(label, output) End If End Sub Public Sub [End]() md_FinishTime = Now() If Enabled Then Response.Write "<p><span class='trace__'>" & vbCrLf Call PrintSummaryInfo() Call PrintCollection("VARIABLE STORAGE", mo_Storage) Call PrintCollection("QUERYSTRING COLLECTION", Request.QueryString()) Call PrintCollection("FORM COLLECTION", Request.Form()) Call PrintCollection("COOKIES COLLECTION", Request.Cookies()) Call PrintCollection("SERVER VARIABLES COLLECTION", Request.ServerVariables()) Response.Write "</span>" End If End Sub Private Sub PrintSummaryInfo() Dim i PrintTableHeader("SUMMARY INFO") Response.Write("<tr><td>Start Time of Request</td><td>" & md_RequestTime & "</td></tr>" & vbCrLf) Response.Write("<tr class='alt'><td>Finish Time of Request</td><td>" & md_FinishTime & "< /td></tr>" & vbCrLf) Response.Write("<tr><td>Elapsed Time</td><td>" & DateDiff("s", md_RequestTime, md_FinishTime) & "</td></tr>" & vbCrLf) Response.Write("<tr class='alt'><td>Request Type</td><td>" & Request.ServerVariables ("REQUEST_METHOD") & "</td></tr>" & vbCrLf) Response.Write("<tr><td>Status Code</td><td>" & Response.Status & "</td></tr<" & vbCrLf) Response.Write "</tr></table>" End Sub Private Sub PrintCollection(Byval Name, ByVal Collection) Dim vItem Dim i PrintTableHeader(Name) For Each vItem In Collection If i mod 2 = 0 Then Response.Write("<tr>") else Response.Write("<tr class='alt'>") end if Response.Write("<td>" & vItem & "</td><td>" & Collection(vItem) & ">/td></tr>" & vbCrLf) i = i + 1 Next Response.Write "</tr></table>" End Sub Private Sub Class_Terminate() Set mo_Storage = Nothing End Sub Private Sub PrintTableHeader(ByVal Name) Response.Write "<table cellpadding='0' width='100%' cellspacing='0'>" & vbCrLf Response.Write "<tr><th class='alt' colspan='10' align='left'><h3><b>" & Name & "</b></h3>< /th></tr>" & vbCrLf Response.Write "<tr class='subhead' align='left'><th width='10%'>Name</th><th width='10 %'>Value</th></tr>" & vbcrlf End Sub End Class %> |
Using this object for debugging and tracing is extremely simple. All you need to do is include the page at the top of the ASP page that you want to track, instantiate an instance of the object in your ASP page, enable it, and then call the Print method to output your own debugging information. When you’re finished, call the End method to display the collection information. Finally, set it equal to Nothing to destroy it. Another nice feature of this object is that it can be enabled and disabled at will. If you tossed in a few Debug.Print calls for testing and did not want them output for a demo, for example, you could simply disable the debug object on that page to stop the output from appearing instead of manually removing all the lines that reference it.
As an additional guide, take a look at Listing 2.5, which shows an example ASP page where the debug object that you just built is being used.
<%@ Language=VBScript %> <%Option Explicit%> <!—#include file="clsDebug.asp"—> <% Dim Debug Dim x Set Debug = New clsDebug ' Instantiate it Debug.Enabled = True ' Enable it ' Set a test cookie Response.Cookies("TestCookie") = "This is a test cookie!" %> <HTML> <HEAD> <TITLE>Test Page>/TITLE> </HEAD> <BODY> <% x = 10 ' Output a debug string Debug.Print "x before form", x %> <form method="POST" action="DebugTest.asp" name="frmForm1" id="frmForm1"> <input type="text" name="txtText1" id="txtText1"> <input type="submit" name="btnSubmit1" id="btnSubmit1"> </form> <form method="GET" action="DebugTest.asp" name="frmForm2" id="frmForm2"> <input type="text" name="txtText2" id="txtText2"> input type="submit" name="btnSubmit2" id="btnSubmit2"> </form> <% x = 20 Debug.Print "x after form", x ' Close it all up Debug.End Set Debug = Nothing %> </BODY> </HTML> |
After calling the End method, all information regarding form submissions, the query string, cookies, server variables, and your own variable-tracking statements is displayed. The output is almost identical to the trace output that is included as part of the ASP.NET functionality that you will be looking at in Chapter 6,“Tracing.” The output of this object can be seen in Figure 2.1.