Microsoft KB Archive/288965

= How To Create a VBScript Class or JScript Object to Debug ASP Pages =

Article ID: 288965

Article Last Modified on 8/8/2007

-

APPLIES TO


 * Microsoft Active Server Pages 4.0
 * Microsoft JScript 5.0
 * Microsoft JScript 5.5
 * Microsoft Visual Basic, Scripting Edition 5.0
 * Microsoft Visual Basic, Scripting Edition 5.5

-



This article was previously published under Q288965



SUMMARY
When you use server-side Visual Basic Scripting Edition (VBScript) or JScript, you can build a reusable object for debugging that can be easily included in Active Server Pages (ASP) pages during development. Why use this approach rather than the Microsoft Visual InterDev debugger? If the Visual InterDev debugger is already set up, it is the best option for stepping through code to isolate all kinds of run-time errors because it has advanced features. However, the Visual InterDev debugger can be difficult to set up and use. It also consumes a lot of resources on the Web server, which can create a problem if you have multiple sites, users, or developers on that server while you are debugging.

If you simply need a lightweight debugging tool to track changing variable values, cookies, Session and Application values and collections as your ASP pages execute, you may consider using a VBScript class or JScript object. These objects offer the following advantages:
 * Can be encapsulated and included on each page with a simple server-side include file.
 * Can be enabled or disabled with a single command on the page.
 * Can be left in production pages (in a disabled state) in case you ever need to check the variables.

This object-based approach is better than sprinkling hundreds of Response.Write statements throughout your code and then going back to remove all of them for production. Also, this approach allows you to debug on Microsoft Windows 95 or Microsoft Windows 98 where the Visual InterDev debugger is not supported. You can also debug in situations where normally it is difficult or impossible to use the Visual InterDev debugger (such as across firewalls or on servers where you are not an Administrator).

NOTE: VBScript classes are only available in VBScript version 5.0 or later.



MORE INFORMATION
Many ASP developers write custom debug functions that loop through ASP collections, such as the Request.ServerVariables collection, and display the values. But usually such functions are hard-coded into the page, only display some of the critical values for debugging, and must be removed prior to production server deployment because they are not encapsulated. Jonathan Goodyear, President of ASPSoft.com, recently wrote an article in Visual Basic Programmer's Journal (February 2001, Vol. 11, No. 2, p. 68) in which he suggested a way to encapsulate a reusable VBScript debug object using a Class and a Dictionary object. The VBScript samples in this article are similar to his approach. In addition, this article provides a sample of a JScript debug object for those who use server-side JScript. Because JScript does not have classes, the sample uses a JScript constructor object.

Debugging Object for VBScript
The sample ASP page, which is coded in VBScript, uses the VBScript class for debugging. Follow these steps to use this VBScript debugging object:
 * 1) Paste the VBScript class code (VBScriptDebug.asp) into a new ASP page that has no other tags in it, and save the page.
 * 2) Paste the sample ASP page (MyVBScript.asp) that uses the debug object into another new ASP page, and save it.
 * 3) At the top of MyVBScript.asp, place an include command for VBScriptDebug.asp right after the default language declaration.
 * 4) Wherever you want to start checking variable values in the page, declare a variable called &quot;Debug&quot;, set it to a new instance of clsDebug, and enable it as the sample demonstrates.
 * 5) Wherever you want to check variable values throughout the page, use the Print method of the Debug object, into which you pass your own descriptive label and the variable itself.
 * 6) At the bottom of the page, outside any other code, call the Debug.End method (which prints all the variable values beneath your content), and set the Debug object to nothing.
 * 7) Where you do not want to debug but still want to leave the Debug object and its code in the page for future debugging, just set Debug.Enabled to false to disable debugging. You do not have to remove the debugging code from your page. For performance reasons, you may want to remove the debugging include files and code when you post the page to a production server.

VBScript Debug Object Sample Page (&quot;MyVBScript.asp&quot;)
<%@ Language=VBScript %>

<% Option Explicit %>

<%   'Create a session variable to test the debugger. Session(&quot;MySessionVar&quot;) = &quot;Test variable&quot; 'Declare your debug object and other variables. Dim Debug, x   'Instantiate the debug object. Set Debug = new clsDebug 'Enable the debug object. In the future, 'you can set it to false to disable it. 'The rest of your debug code can be left in the page. Debug.Enabled = true 'The following code shows how to use the Debug.Print method 'and provide a label to check your variables. x = 10 Debug.Print &quot;x before math&quot;, x   x = x + 50 Debug.Print &quot;x after math&quot;, x

'Create a cookie to test the debugger. Response.Cookies(&quot;TestCookie&quot;) = &quot;Hello World&quot;

%>     

   

    </BODY> </HTML>

<%   'Call the End method to output all the debug values. Debug.End Set Debug = nothing %>

VBScript Debug Class Object (&quot;VBScriptDebug.asp&quot;)
<SCRIPT RUNAT=Server Language=VBScript>

Class clsDebug Dim blnEnabled Dim dteRequestTime Dim dteFinishTime Dim objStorage

Public Property Get Enabled Enabled = blnEnabled End Property

Public Property Let Enabled(bNewValue) blnEnabled = bNewValue End Property

Private Sub Class_Initialize dteRequestTime = Now Set objStorage = Server.CreateObject(&quot;Scripting.Dictionary&quot;) End Sub

Public Sub Print(label, output) If Enabled then objStorage.Add label, output End if   End Sub

Public Sub [End] dteFinishTime = Now If Enabled then PrintSummaryInfo PrintCollection &quot;VARIABLE STORAGE&quot;, objStorage PrintCollection &quot;QUERYSTRING COLLECTION&quot;, Request.QueryString PrintCollection &quot;FORM COLLECTION&quot;, Request.Form PrintCollection &quot;COOKIES COLLECTION&quot;, Request.Cookies PrintCollection &quot;SESSION CONTENTS COLLECTION&quot;, Session.Contents PrintCollection &quot;SERVER VARIABLES COLLECTION&quot;, Request.ServerVariables PrintCollection &quot;APPLICATION CONTENTS COLLECTION&quot;, Application.Contents PrintCollection &quot;APPLICATION STATICOBJECTS COLLECTION&quot;, Application.StaticObjects PrintCollection &quot;SESSION STATICOBJECTS COLLECTION&quot;, Session.StaticObjects End if   End Sub

Private Sub PrintSummaryInfo With Response .Write(&quot; &quot;) .Write(&quot;SUMMARY INFO &quot;) .Write(&quot;Time of Request = &quot; & dteRequestTime) & &quot; &quot; .Write(&quot;Time Finished = &quot; & dteFinishTime) & &quot; &quot; .Write(&quot;Elapsed Time = &quot; & DateDiff(&quot;s&quot;, dteRequestTime, dteFinishTime) & &quot; seconds &quot;) .Write(&quot;Request Type = &quot; & Request.ServerVariables(&quot;REQUEST_METHOD&quot;) & &quot; &quot;) .Write(&quot;Status Code = &quot; & Response.Status & &quot; &quot;) End With End Sub

Private Sub PrintCollection(Byval Name, Byval Collection) Dim varItem Response.Write(&quot; &quot; & Name & &quot; &quot;) For Each varItem in Collection Response.Write(varItem & &quot;=&quot; & Collection(varItem) & &quot; &quot;) Next End Sub

Private Sub Class_Terminate Set objStorage = Nothing End Sub

End Class

</SCRIPT>

Debugging Object for JScript
If you use server-side JScript, the following ASP sample page provides the same functionality as the preceding VBScript sample. The steps to implement the JScript object on a page for debugging are essentially the same, so they are not repeated here. However, note a few minor syntax differences; without these differences, your JScript debug object does not work. These differences are noted in the sample below.

JScript Debug Object Sample Page (&quot;MyJScript.asp&quot;)
<%@ Language=JScript %>

<HTML>  <META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;> <%   Session(&quot;MySessionVar&quot;) = &quot;Test variable&quot;

//Note the difference in declaring the object variable. var Debug = new objDebug; var x   Debug.Enabled = true x = 10

//Notice that you have to include parentheses with the Print method. Debug.Print(&quot;x before math&quot;, x)   x = x + 50 Debug.Print(&quot;x after math&quot;, x)

Response.Cookies(&quot;TestCookie&quot;) = &quot;Hello World&quot;

%> </HEAD> <BODY>  <INPUT type=&quot;text&quot; id=text1 name=text1> <INPUT type=&quot;submit&quot; value=&quot;Post Info&quot; id=submit1 name=submit1> </FORM>

 <INPUT type=&quot;text&quot; id=text2 name=text2> <INPUT type=&quot;submit&quot; value=&quot;Get Info&quot; id=submit2 name=submit2> </FORM>

<P></P>

</BODY> </HTML>

<%   //Notice that you must use empty parentheses with the End method. Debug.End; %>

JScript Debug Constructor Object (&quot;JScriptDebug.asp&quot;)
<SCRIPT LANGUAGE=JScript RUNAT=Server>

function objDebug {   var Now = new Date; this.RequestTime = Now; this.FinishTime = null; this.Print = Print; this.Storage = Server.CreateObject(&quot;Scripting.Dictionary&quot;); this.Enabled = false; this.End = StopDebug; }

function StopDebug {   var Now = new Date; Debug.FinishTime = Now; if(Debug.Enabled == true) {       PrintSummaryInfo PrintDictionary(&quot;VARIABLE STORAGE&quot;, Debug.Storage) PrintCollection(&quot;QUERYSTRING COLLECTION&quot;, Request.QueryString) PrintCollection(&quot;FORM COLLECTION&quot;, Request.Form) PrintCollection(&quot;COOKIES COLLECTION&quot;, Request.Cookies) PrintCollection(&quot;SESSION CONTENTS COLLECTION&quot;, Session.Contents) PrintCollection(&quot;SERVER VARIABLES COLLECTION&quot;, Request.ServerVariables) PrintCollection(&quot;APPLICATION CONTENTS COLLECTION&quot;, Application.Contents) PrintCollection(&quot;APPLICATION STATICOBJECTS COLLECTION&quot;, Application.StaticObjects) PrintCollection(&quot;SESSION STATICOBJECTS COLLECTION&quot;, Session.StaticObjects)

}

}   function Print(label,output) {   if(Debug.Enabled == true) {       Debug.Storage.Add(label,output) }   }

function PrintSummaryInfo {   var TotalTime = new Date; TotalTime = (Debug.FinishTime - Debug.RequestTime) / 1000 Response.Write(&quot; &quot;) Response.Write(&quot;SUMMARY INFO &quot;) Response.Write(&quot;Time of Request = &quot; + Debug.RequestTime + &quot; &quot;) Response.Write(&quot;Time Finished = &quot; + Debug.FinishTime + &quot; &quot;) Response.Write(&quot;Elapsed Time = &quot; + TotalTime + &quot; seconds &quot;) Response.Write(&quot;Request Type = &quot; + Request.ServerVariables(&quot;REQUEST_METHOD&quot;) + &quot; &quot;) Response.Write(&quot;Status Code = &quot; + Response.Status + &quot; &quot;) }

function PrintDictionary(theName, dict) {   var i    var KeyArray = (new VBArray(dict.Keys)); Response.Write(&quot; &quot; + theName + &quot; &quot;) for(i=0;i < dict.Count;i++) {       Response.Write(KeyArray.getItem(i) + &quot; = &quot; + dict(KeyArray.getItem(i)) + &quot; &quot;) }   }

function PrintCollection(theName, theColl) {   var x    Response.Write(&quot; &quot; + theName + &quot; &quot;) for(x=1;x < (theColl.Count + 1); x++) {       Response.Write(theColl.Key(x) + &quot; = &quot; + theColl.Item(x) + &quot; &quot;) }   }

</SCRIPT>

<div class="references_section">