Finding Out What s Going On


Finding Out What's Going On

Exceptions and defensive programming are all very well, but there are plenty of times during development that you'll have trouble finding out exactly what's going on in your code. Sometimes it's difficult to understand the flow of your program, and where things are happening. There are two ways to attack this problem. The first is by tracing your code, so you can add information to the page to let you see the page, plus some trace information. The second is by debugging your code, which is where you step through the code line by line as it is executing.

Tracing

Tracing in ASP.NET is extremely clever, and allows you to put statements throughout your code, which can then appear in a specific location at the bottom of the page. This allows the page to look correct, but still contain trace information. Let's give it a go.

Try It Out—Turning on Tracing

  1. Using the TryCatch.aspx example from above, switch to All view.

  2. Change the top line so it looks like this:

     <%@ Page Language="VB" Debug="true" Trace="true" %> 
  3. Save the page and run it:

    click to expand

Even without doing anything you'll see that a lot of information has been added to the end of the page. You don't need to worry about most of this at the moment – the important section is the Trace Information section. You see some default messages from ASP.NET, but you can also write into this section.

Try It Out—Writing to the Trace Log
  1. Switch to Code view, and add the following line as the first line of code in the Button1_Click event:

        Sub Button1_Click(sender As Object, e As EventArgs)  Trace.Write ("Click", "Start") 
  2. On the line between the Try and the conn.Open(), add the following:

          Try  Trace.Write("Opening connection")        Conn.Open()
  3. On the line after the Catch, add the following:

          Catch ex As Exception  Trace.Warn(ex.Message) 
  4. Save the program, run it, and press the button:

    click to expand

Notice that there are more details in the Trace Information section than before.

How It Works

The first point is the addition of the trace directive at the top of the page:

Trace="true"

This tells ASP.NET to output trace information. You can set this to false, and the trace information doesn't show. This means that you can switch tracing on and off without removing any of the trace statements from the code, which is a great time-saving technique.

Next are the lines that write your own information to the trace output. These are all variations on the same theme:

Trace.Write ("Click", "Start") Trace.Write("Opening connection") Trace.Warn(ex.Message)

The first of these has two arguments. You can see from the output that the first argument appears as the Category, and the second as the Message. When only one argument is used (as in the other cases), no Category is shown. The final example uses Warn instead of Write, in which case the output appears in red. This makes it easier to see (except in a black and white book of course!).




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net