Page

9.4.2- Conditional Tracing

Created by Brendan Doss.
Last Updated by Jim Minatel.  

PublicCategorized as 09. Error Handling.

Not yet tagged
<< 9.4.1- Use Response.WriteChapter99.4.3- The Microsoft Script Debugger >>

Conditional Tracing

Conditional tracing can be quite useful during the development of an ASP application, especially if it is quite large. The process of tracing is the process of running a program and returning information at each separate step of the program. This simply involves having a variable that tells you whether or not tracing is in action. So our tracing routine could look like this:

 

Sub Trace (strString)

 

If blnTracing Then

Response.Write "Debug: " & strString & "<BR>"

End If

 

End Sub

 

You can now scatter Trace statements amongst your code, and you can simply turn tracing on or off by setting blnTracing to True or False.

 

You might think that these techniques are a bit cumbersome, but let's have a look at combining some of them into a set of tracing routines that can be included into any ASP script.

Try It Out – Doing a Trace

1.    Open up your ASP editor and add the following code. This will be our HTML form.

 

<HTML>

<HEAD>

<TITLE>Tracing Form</TITLE>

</HEAD>

Enter your name and email address here:

 

<FORM NAME=Tracing ACTION="DoTracing.asp" METHOD="POST">

Name: <INPUT TYPE=TEXT NAME="Name"><BR>

Email: <INPUT TYPE=TEXT NAME="Email"><BR>

<INPUT TYPE=SUBMIT VALUE="Process">

<INPUT TYPE=RESET VALUE="Clear">

</FORM>

 

</HTML>

 

2.    Save this file as Tracing.html, close it and create a new one. This will be the ASP file into which we have put the tracing statements.

3.    Add the following code to this new file.

 

<% Option Explicit %>

<HTML>

<!-- #INCLUDE FILE="Trace.asp" -->

<HEAD>


<TITLE>Tracing Example</TITLE>

</HEAD>

<BODY>

 

Tracing is on for this bit<BR>

<%

  Dim strName

  Dim strEmail

 

  TraceStart

 

  Trace "Getting form details"

 

  strName = Request.Form("Name")

  strEmail = Request.Form("Email")

 

  ProcessFormDetails strName, strEmail

 

  TraceStop

 

  ProcessFormDetails strName, strEmail

 

Sub ProcessFormDetails (strName, strEmail)

 

  TraceProcedureStart "ProcessFormDetails"

 

  ' some form of processing goes here

   Response.Write "We are doing some processing here<BR>"

 

  Trace "Name=" & strName

  Trace "Email=" & strEmail

 

  TraceProcedureEnd "ProcessFormDetails"

 

End Sub

%>

 

</BODY>

</HTML>

 

4.    Save this file as DoTracing.asp, and create a new one. This will be our include file.

An include file is a way to include the contents of one file within another. Thus we can define a routine or connection string etc. in one place and use it from other files. They will be discussed in more detail in Chapter 10 .

Add the following code:

 

<%

 

Dim blnTracing

 

Sub TraceStart()

 

  blnTracing = True

  Trace "Tracing started"

 

End Sub

 

Sub TraceStop()

 

  Trace "Tracing stopped"

  blnTracing = False

 

End Sub

 

Sub Trace (strString)

 

   If blnTracing Then

     Response.Write "Debug: " & strString & "<BR>"

   End If

 

End Sub

 

Sub TraceProcedureStart (strProcedure)

 

  Trace strProcedure & " started"

 

End Sub

 

Sub TraceProcedureEnd (strProcedure)

 

  Trace strProcedure & " ended"

 

End Sub

%>

 

5.    Save this file as Trace.asp, and close down your ASP editor.

6.    Open the first file, Tracing.html, in your browser:

 

Chapter9_image007

 

7.    Enter some details and press the Process button.

Chapter9_image008

You can see that all of our trace lines start with Debug. You can also see that the processing procedure that outputs We are doing some processing here is run twice, but that the second time it is run tracing has been turned off.

How it Works

We don't need to look at the HTML file – it's just a standard file with a form on it, whose purpose is to call the ASP page. Before we look at this ASP page, let's first look at the actual tracing code in Trace.asp

 

The first thing to notice is that we have a variable to determine whether tracing is enabled or not.

 

Dim blnTracing

 

We then have two procedures to set this flag. We also have a trace message telling us that tracing has been turned on and off – this stops you worrying whether you have set it or not.

 

Sub TraceStart()

 

  blnTracing = True

  Trace "Tracing started"

 

End Sub

 

Sub TraceStop()

 

  Trace "Tracing stopped"

  blnTracing = False

 

End Sub

 

We then have the actual trace procedure. This accepts a single string as an argument, and it writes this to the output stream if the trace flag is True. This way you can still call the Trace statement even if tracing is disabled:

 

Sub Trace (strString)

 

   If blnTracing Then

   Response.Write "Debug: " & strString & "<BR>"

End If

 

End Sub

 

Lastly we have two procedures that can be called at the start and end of procedures, just so you can see when procedures are called.

 

Sub TraceProcedureStart (strProcedure)

 

  Trace strProcedure & " started"

 

End Sub


Sub TraceProcedureEnd (strProcedure)

 

  Trace strProcedure & " ended"

 

End Sub

 

This gives you five procedures:

 

  • TraceStart to turn on tracing.
  • TraceEnd to turn off tracing.
  • TraceProcedureStart to indicate the start of a procedure. The argument should be the procedure name.
  • TraceProcedureEnd to indicate the end of a procedure. The argument should be the procedure name.
  • Trace to indicate a trace message. The argument should be the string to be displayed.

 

Let's now see how they were used in the ASP script in DoTracing.asp. After declaring the script variables, the first thing to do is turn on tracing:

 

<%

  Dim strName

  Dim strEmail

 

  TraceStart

 

Now tracing is enabled, we write a message indicating that we are about to get the form details:

 

  Trace "Getting form details"

 

  strName = Request.Form("Name")

  strEmail = Request.Form("Email")

 

Once we have the form details in variables we pass them to our processing routine:

 

  ProcessFormDetails strName, strEmail

 

We then turn off tracing, and call our processing routine again:

 

  TraceStop

 

  ProcessFormDetails strName, strEmail

 

Now let's consider the actual processing routine. It doesn't actually do any processing in this case, but simply displays a string. The first thing we do is to trace the start of the procedure:

 

Sub ProcessFormDetails (strName, strEmail)

 

  TraceProcedureStart "ProcessFormDetails"

We then display our processing string, and display the name and email address using the Trace statement:

 

  ' some form of processing goes here

   Response.Write "We are doing some processing here<BR>"

 

  Trace "Name=" & strName

  Trace "Email=" & strEmail

 

  Lastly, we display the end of the procedure:

 

  TraceProcedureEnd "ProcessFormDetails"

 

End Sub

%>

 

So all we have done is used a few simple routines to see exactly where in the ASP code we are. You can see that we can include tracing in routines, and then turn off tracing without worrying about it, because the tracing routine detects whether tracing is in action or not. In other words, you can turn on tracing to see what's happening and then turn it off with a single statement, without having to remove the tracing code.

 

So you can see that we have achieved our objectives here:

 

  • We have reusable subroutines.
  • We have those reusable routines in an include file, which can be used in other ASP scripts.
  • We have conditional tracing so it can be turned on and off at will.

 

Although this include file won't stop you getting errors, it will allow you to start debugging them quicker. In large ASP projects you might like to include this file in all of your ASP scripts and place tracing statements throughout your code. Should you encounter problems you can simply turn tracing on. Once the ASP page is ready for the real world you can strip the tracing statements out so that execution isn't slowed down at all.

<< 9.4.1- Use Response.WriteChapter99.4.3- The Microsoft Script Debugger >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |