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

Isenção de Responsabilidade para Conteúdo da KB Desativado

Este artigo foi escrito sobre produtos para os quais a Microsoft já não fornece suporte. Por conseguinte, este artigo é oferecido "tal como está" e não será mais atualizado.

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 "Debug", 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 ("MyVBScript.asp")

<%@ Language=VBScript %>

<% Option Explicit %>

<!-- #include file="VBScriptDebug.asp" -->

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

'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 "x before math", x
x = x + 50
Debug.Print "x after math", x

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

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


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

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

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

VBScript Debug Class Object ("VBScriptDebug.asp")

<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("Scripting.Dictionary")
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 "VARIABLE STORAGE", objStorage
PrintCollection "QUERYSTRING COLLECTION", Request.QueryString()
PrintCollection "FORM COLLECTION", Request.Form()
PrintCollection "COOKIES COLLECTION", Request.Cookies()
PrintCollection "SESSION CONTENTS COLLECTION", Session.Contents()
PrintCollection "SERVER VARIABLES COLLECTION", Request.ServerVariables()
PrintCollection "APPLICATION CONTENTS COLLECTION", Application.Contents()
PrintCollection "APPLICATION STATICOBJECTS COLLECTION", Application.StaticObjects()
PrintCollection "SESSION STATICOBJECTS COLLECTION", Session.StaticObjects()
End if
End Sub

Private Sub PrintSummaryInfo()
With Response
.Write("<hr>")
.Write("<b>SUMMARY INFO</b></br>")
.Write("Time of Request = " & dteRequestTime) & "<br>"
.Write("Time Finished = " & dteFinishTime) & "<br>"
.Write("Elapsed Time = " & DateDiff("s", dteRequestTime, dteFinishTime) & " seconds<br>")
.Write("Request Type = " & Request.ServerVariables("REQUEST_METHOD") & "<br>")
.Write("Status Code = " & Response.Status & "<br>")
End With
End Sub

Private Sub PrintCollection(Byval Name, Byval Collection)
Dim varItem
Response.Write("<br><b>" & Name & "</b><br>")
For Each varItem in Collection
Response.Write(varItem & "=" & Collection(varItem) & "<br>")
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 ("MyJScript.asp")

<%@ Language=JScript %>

<!-- #include file="JScriptDebug.asp" -->

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<%
Session("MySessionVar") = "Test variable"

//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("x before math", x)
x = x + 50
Debug.Print("x after math", x)

Response.Cookies("TestCookie") = "Hello World"

%>
</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<INPUT type="text" id=text1 name=text1>
<INPUT type="submit" value="Post Info" id=submit1 name=submit1>
</FORM>

<FORM action="" method=Get id=form2 name=form2>
<INPUT type="text" id=text2 name=text2>
<INPUT type="submit" value="Get Info" 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 ("JScriptDebug.asp")

<SCRIPT LANGUAGE=JScript RUNAT=Server>  

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

function StopDebug()
{
var Now = new Date();
Debug.FinishTime = Now;
if(Debug.Enabled == true)
{
PrintSummaryInfo()
PrintDictionary("VARIABLE STORAGE", Debug.Storage)
PrintCollection("QUERYSTRING COLLECTION", Request.QueryString())
PrintCollection("FORM COLLECTION", Request.Form())
PrintCollection("COOKIES COLLECTION", Request.Cookies())
PrintCollection("SESSION CONTENTS COLLECTION", Session.Contents())
PrintCollection("SERVER VARIABLES COLLECTION", Request.ServerVariables())
PrintCollection("APPLICATION CONTENTS COLLECTION", Application.Contents())
PrintCollection("APPLICATION STATICOBJECTS COLLECTION", Application.StaticObjects())
PrintCollection("SESSION STATICOBJECTS COLLECTION", 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("<hr>")
Response.Write("<b>SUMMARY INFO</b><br>")
Response.Write("Time of Request = " + Debug.RequestTime + "<br>")
Response.Write("Time Finished = " + Debug.FinishTime + "<br>")
Response.Write("Elapsed Time = " + TotalTime + " seconds<br>")
Response.Write("Request Type = " + Request.ServerVariables("REQUEST_METHOD") + "<br>")
Response.Write("Status Code = " + Response.Status + "<br>")
}

function PrintDictionary(theName, dict)
{
var i
var KeyArray = (new VBArray(dict.Keys()));
Response.Write("<br><b>" + theName + "</b><br>")
for(i=0;i < dict.Count;i++)
{
Response.Write(KeyArray.getItem(i) + " = " + dict(KeyArray.getItem(i)) + "<br>")
}
}

function PrintCollection(theName, theColl)
{
var x
Response.Write("<br><b>" + theName + "</b><br>")
for(x=1;x < (theColl.Count + 1); x++)
{
Response.Write(theColl.Key(x) + " = " + theColl.Item(x) + "<br>")
}
}

</SCRIPT>

References

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

192011 How To Configure Your Web Server for Visual InterDev 6.0 ASP Debugging
244272 INFO: Visual InterDev 6.0 Debugging Resources
For more information, see the Microsoft Scripting Technologies Web site:
Propriedades

ID do Artigo: 288965 - Última Revisão: 8 de ago de 2007 - Revisão: 1

Comentários