Microsoft KB Archive/299986

= HOW TO: Use an ASP Debug Object to Debug ASP Pages =

Article ID: 299986

Article Last Modified on 8/8/2007

-

APPLIES TO


 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q299986



SUMMARY
This step-by-step guide provides Active Server Pages (ASP) code that Web developers can use to debug ASP applications. This article demonstrates how to build your own custom &quot;debug object,&quot; which is an ASP page that is written in JScript or VBScript. This is a simple, lightweight alternative to using the Microsoft Visual InterDev debugger. It requires no server configuration and enables you to check the value of changing variables in your code, as well as display the values of all ASP collections and variables.

Although the Visual InterDev debugger is the most powerful tool for debugging ASP pages, there are a number of scenarios in which it cannot be used. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

288965 HOWTO: Create a VBScript Class or JScript Object to Debug ASP Pages

Requirements

 * Microsoft Windows 2000 Professional, Windows 2000 Server, or Windows 2000 Advanced Server
 * Microsoft Internet Information Server (IIS) 5.0

NOTE: If you using an earlier version of Windows (prior to Windows 2000), make sure you have Microsoft Windows Script version 5.1 or later installed. Although the features that are described in this article also work on Windows NT with IIS 4.0 installed and configured, this sample assumes that you are using Windows 2000.

Prepare the Web Site
 Right-click the Windows Start menu, and then click Explore. In Windows Explorer, click to select your Web server's root folder, which is typically C:\Inetpub\Wwwroot\. From the File menu, point to New, and then click Folder. Type DebugTest to name the new folder. Right-click the new DebugTest folder, and then click Properties. On the Security tab, add the Everyone group, and select the Read and Write check boxes to allow these permissions to the Everyone group. Click Apply and then click OK to accept the changes.

NOTE: Use these permissions for this development sample only. Consult your Network administrator to determine your specific security settings. On the Start menu, point to Programs, point to Administrative Tools, and then click Internet Services Manager. Under Internet Information Services, double-click to expand the entry for the local server.</li> Right-click the Default Web Site, point to New, and then click Virtual Directory. In the wizard, follow these steps: <ol style="list-style-type: lower-alpha;"> When prompted, type DebugTest in the Virtual Directory Alias text box, and then click Next.</li> When you are prompted to type the Web Site Content Directory, click Browse, select the newly created DebugTest directory, and then click Next.</li> When you are prompted to select Access Permissions, select Read and Run scripts (such as ASP). No other access permissions are necessary for this example. Click Next to complete the wizard.</li></ol> </li> Double-click to expand the Default Web Site, right-click DebugTest, and then click Properties.</li> On the Directory tab, verify that the Web name (as entered in step 8a) is listed in the Application Name text box under Application Settings. If it is not, click Create to create the application.</li> Close the Properties dialog box and Internet Information Server.</li></ol>

Create a VBScript Debug Object
This section demonstrates how to create the Debug ASP page, named VBScriptDebug.asp. Furthermore, a sample ASP page named MyVBScript.asp demonstrates how to incorporate the Debug object into any ASP page through a server-side include file.

VBScriptDebug.asp
<ol> On the Start menu, point to Programs, point to Accessories, and then click Notepad.</li>  Highlight the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file: <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> </li> From the File menu, click Save.</li> In the Save in drop-down list box, browse to the DebugTest folder that you created earlier. In the File name text box, type VBScriptDebug.asp, and click All Files in the Save as Type drop-down list box. Finally, click Save to save the file.</li> Leave Notepad open.</li></ol>

MyVBScript.asp
<ol> In Notepad, from the File menu, click New to create a new text document.</li>  Highlight the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file: <%@ Language=VBScript %>

<% Option Explicit %>

<% 'Create a session variable to test the debugger. Session(&quot;MySessionVar&quot;) = &quot;Test variable&quot;

'Declare variables. Dim x

'Declare your Debug object. Dim Debug

'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 how to 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;

%> <HTML> <HEAD> <META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;> </HEAD> <BODY>

<FORM action=&quot;&quot; method=POST id=form1 name=form1> <INPUT type=&quot;text&quot; id=text1 name=text1> <INPUT type=&quot;submit&quot; value=&quot;Post Info&quot; id=submit1 name=submit1> </FORM>

<FORM action=&quot;&quot; method=Get id=form2 name=form2> <INPUT type=&quot;text&quot; id=text2 name=text2> <INPUT type=&quot;submit&quot; value=&quot;Get Info&quot; id=submit2 name=submit2> </FORM> </BODY> </HTML>

<%  'Call the End method to display all of the debug values. Debug.End Set Debug = nothing %>                   </li> From the File menu, click Save.</li> In the Save in drop-down list box, browse to the DebugTest folder that you created earlier. In the File name text box, type MyVBscript.asp, and click All Files in the Save as Type drop-down list box. Finally, click Save to save the file.</li></ol>

To view the page, start your Web browser, and type the HTTP location of the page in the Address bar. If you saved the file in the above-mentioned location, type http://<servername>/DebugTest/MyVBScript.asp in the Address bar.

How to Use the VBScript Debug Object in an ASP Page
The MyVBScript.asp file demonstrates how to enable the VBScript Debug object in an ASP page. To understand how to use the Debug object, remember the following key points: <ol> <li> To see how include files work, use Notepad to reopen MyVBScript.asp on the Web server. Approximately five lines into the code, notice that the following include code appears: This line of code demonstrates how to include the Debug object (which is located in VBScriptDebug.asp) in your own ASP page. </li> <li> After the VBScriptDebug.asp page has been referenced through the server-side include file, the Debug object must be instantiated as follows: 'Declare your Debug object. Dim Debug

'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 Debug object is declared and then instantiated by setting the object to a new instance of the Debug class. After it is instantiated, the Debug class is enabled by setting Debug.Enabled to true. </li> <li> The following portion of code '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 demonstrates how to use the Debug object to check variable values. You call the Print method of the object, pass it a string that describes what the variable is, and then pass in the variable itself. As the sample shows, you can use the Print method repeatedly on the same variable to track how its value changes through the code. When you browse to this ASP page in a browser, the Print method causes your variable values to be displayed so that you can check them. </li> <li> At the bottom of MyVbscript.asp, notice the following code: <%  'Call the End method to display all of the debug values. Debug.End Set Debug = nothing %> This code tells the Debug object to proceed and print all of the intrinsic ASP variables (Session, Application, Server, and so on), as well as any variables that you passed in. The Debug object allows your ASP page to run as normal. When the ASP page is enabled with Debug.Enabled = True, it displays all of the different variable values at the bottom of your page. </li> <li>When you are finished debugging, if you still want to leave the Debug object and Print statements in the code for future debugging, set the line that currently enables debugging to Debug.Enabled = False. This disables debugging but allows you to leave all the code in your page and simply set Debug.Enabled = True again when you want to debug.</li></ol>

Create a JScript Debug Object
If you use server-side JScript, the following ASP sample pages provide the same functionality as the preceding VBScript sample.

JScriptDebug.asp
<ol> <li>On the Start menu, point to Programs, point to Accessories, and then click Notepad.</li> <li> Highlight the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file: <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> </li> <li>From the File menu, click Save.</li> <li>In the Save in drop-down list box, browse to the DebugTest folder that you created earlier. In the File name text box, type JScriptDebug.asp, and click All Files in the Save as Type drop-down list box. Finally, click Save to save the file.</li> <li>Leave Notepad open.</li></ol>

MyJScript.asp
<ol> <li>In Notepad, click New on the File menu to create a new text document.</li> <li> Highlight the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file: <%@ Language=JScript %>

<HTML> <HEAD> <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> <FORM action=&quot;&quot; method=POST id=form1 name=form1> <INPUT type=&quot;text&quot; id=text1 name=text1> <INPUT type=&quot;submit&quot; value=&quot;Post Info&quot; id=submit1 name=submit1> </FORM>

<FORM action=&quot;&quot; method=Get id=form2 name=form2> <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; %>                   </li> <li>From the File menu, click Save.</li> <li>In the Save in drop-down list box, browse to the DebugTest folder that you created earlier. In the File name text box, type MyJScript.asp, and click All Files in the Save as Type drop-down list box. Finally, click Save to save the file.</li></ol>

How to Use the JScript Debug Object in an ASP Page
The MyJScript.asp file demonstrates how to enable the JScript Debug object in an ASP page, which is very similar to how the VBScript Debug object is used. To understand how to use the Debug object, remember the following key points: <ul> <li> The JScriptDebug.asp page is included in the MyJScript.asp file by using the same method as above, a server-side include directive: This line of code demonstrates how to include the Debug object (which is located in JScriptDebug.asp) in your own ASP page. </li> <li> Instantiation of the Debug object is slightly different through JScript than through VBScript: //Note the difference in declaring the object variable. var Debug = new objDebug; </li> <li> Closing the Debug object in JScript is also different. Note the use of parentheses to close the Debug object: //Notice that you must use empty parentheses with the End method. Debug.End; </li></ul>

Pitfalls
The only significant drawback to using these Debug objects is that they cannot provide as many debugging features as the Visual InterDev Debugger. However, in cases where you cannot use the Visual InterDev debugger, these objects may be a useful option.