Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ASP .NET Web Developer s Guide - Mesbah Ahmed, Chris Garrett

.pdf
Скачиваний:
38
Добавлен:
24.05.2014
Размер:
7.32 Mб
Скачать

430 Chapter 9 • Debugging ASP.NET

Trace.Write("value of factorial is" & factorial)

Next

Figure 9.5 Using the Warn() Method to Display Trace Information

NOTE

Turning tracing on and off is just a matter of modifying the value of the Trace attribute in the page directive.

Sorting the Trace Information

Inserting multiple Trace statements in an application can sometimes be messy. It is useful if the Trace information is classified into different categories to make tracing easier.The Trace class allows us to create different debugging categories and sort the Trace information based on these categories.The following example shows how to group the different categories of Trace information:

factorial = 1

Trace.TraceMode = TraceMode.SortByCategory

For i = 1 To 5

factorial *= i

Trace.Warn("counter", "value of i is " & i) Trace.Write("Factorial", "value of factorial is" & factorial)

Next

Trace.Write(factorial)

Response.Write("The factorial of 5 is " & factorial)

The output of the preceding code is shown in Figure 9.6.

Let’s dissect the preceding codes:

Trace.TraceMode = TraceMode.SortByCategory

www.syngress.com

Debugging ASP.NET • Chapter 9

431

Figure 9.6 Sorting by Category (All Lines in the “Counter” Category Are Displayed in Red)

The TraceMode property sets the modes supported by the trace:

SortByCategory Trace information is sorted by category.

SortByTime Trace information is displayed in the sequence of execution.

Since we are sorting the Trace mode by category, notice that Figure 9.6 shows the messages are sorted by category.

Trace.Warn("counter", "value of i is " & i)

The Warn method displays the message in red and notes that this method is overloaded. In this case, we pass in two arguments.The first goes into the Category and the second is for the Message.

Trace.Write("Factorial", "value of factorial is" & factorial)

The Write() method of the Trace object is also overloaded, just like the Warn() method.This time around, we write the message into the “Factorial” category.

Besides using the Trace class to set the Trace mode, you can also use the Page directive to set the Trace mode:

<%@ Page Language="vb" Trace="true" TraceMode="SortByCategory"

AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"

Inherits="WebApplication1.WebForm1" %>

www.syngress.com

432 Chapter 9 • Debugging ASP.NET

Writing the Trace Information to the Application Log

Although displaying the Trace information within the page is useful, sometimes you need to trace the page while users are utilizing your application. In such cases, the user should not see the Trace information. ASP.NET provides a mean for the Trace information to be written to a log file.The following example shows how the Trace information is written to the application log:

...

Response.Write("The factorial of 5 is " & factorial)

Dim appLog As New System.Diagnostics.EventLog()

appLog.WriteEntry("Factorial ASP.NET application", "The factorial of 5

is " & factorial)

The System.Diagnostics namespace provides the class to debug our application. In particular, we used the EventLog component to help us write messages to the application log.To view the message, use the Event Viewer. Our message is shown in Figure 9.7.

Figure 9.7 Writing to the Application Log

To see the message details, double-click the event item.The detailed message is shown in Figure 9.8.

Application Tracing

This last section discusses page tracing which maps the flow within a page. ASP.NET also supports tracing at the application level. Application-level tracing is set in the web.config file, under the trace section:

<trace enabled="false"

requestLimit="10"

pageOutput="false"

traceMode="SortByTime"

www.syngress.com

Debugging ASP.NET • Chapter 9

433

localOnly="true"

/>

Figure 9.8 Details of the Message

To enable an application-level trace, set the following values shown in Table 9.5.

Table 9.5 Attributes of the Trace Element

Attribute

Value

Description

 

 

 

enabled

true

Enables or disables application-level tracing.

requestLimit

10

Sets the maximum number of requests to trace.

pageOutput

false

Displays the trace at the end of the page.

traceMode

SortByTime

Trace information sort order.

localOnly

true

Sets the ability to see trace viewer on a nonlocal

 

 

machine.

When the application is loaded, the Trace information does not appear on the page.To view the Trace information, we need to use the Trace viewer (trace.axd) shown in Figure 9.9.

Figure 9.9 shows the Trace information of the last ten requests to the application.To view the detailed information of each request, click the View Details link of each row.

www.syngress.com

434 Chapter 9 • Debugging ASP.NET

Figure 9.9 Application Level Tracing

NOTE

If the trace is set to “true” in the web.config file and set to “false” in the page directive, tracing is disabled.

Using Visual Studio .NET

Debugging Tools

Visual Studio .NET contains a rich set of debugging tools to help developers debug their applications. In this section, we look at some of the tools available.

Setting Breakpoints

Besides using the Trace class to trace the value of variables in your application, another method is to set breakpoints in your application.Visual Studio .NET allows you to do this so you can examine and trace the flow of your application during runtime. Figure 9.10 shows a breakpoint (indicated by a dot, which shows up as red on the screen).

www.syngress.com

Debugging ASP.NET • Chapter 9

435

Figure 9.10 Setting a Breakpoint (Designated by Red Dot)

NOTE

Visual Studio 6 developers should be familiar with setting breakpoints in the IDE.

When the application is run, the execution would stop at the breakpoint. Three options are available:

Step Into The execution would then move into the function named Factorial. Each step would execute a line (by pressing F11).

Step Over The execution would execute the function (without stepping through the codes within the function) and treat the function as a single line.This is achieved by pressing F10.

Step Out This option is available if the current execution point is in the function and you want to execute the rest of the codes in the function without stepping through them. It then returns to the calling function.

Besides tracing the flow, you can also examine the values of variables during a breakpoint.There are two ways to examine the values of variables:

Tool tip help Position the cursor over the variable you want to examine.The value will be displayed in a tool tip dialog box.

Watch window Examine the value of variables by using the Watch window (activated by choosing Debug | Windows | Watch).

Enabling and Disabling Debug Mode

By default, your ASP.NET application is in debug mode.The <compilation> element in the web.config file controls this:

<compilation defaultLanguage="vb" debug="true" />

www.syngress.com

436 Chapter 9 • Debugging ASP.NET

During compilation, debugging symbols (.pdb information) are inserted into the compiled page. As a result, the application will run slower than without the debugging symbols. As such, remember to set the debug attribute to false when you deploy your application.

Viewing Definitions Using the Object Browser

One of the key aspects of successful .NET programming is the ability to use the appropriate class libraries provided by the framework.While the MSDN documentation is a good place to find out about the class libraries, a better option would be to use the Object Browser provided by the .NET SDK.To launch the Object Browser in Visual Studio .NET, press Ctrl+Alt+J.

Figure 9.11 shows the Object Browser with the System assembly and its associated namespaces exposed. Members of the class UriFormationException are shown on the right window, while the bottom window shows the description of the selected member.

Figure 9.11 Using the Object Browser

Members

Assemblies

Descriptions

Namespaces

Using the Class Viewer

Besides employing the Object Browser to view the various class libraries available, you can also use the Class Viewer.To launch the Class Viewer, type WinCV at the command prompt. Figure 9.12 shows the Class Viewer.

www.syngress.com

Debugging ASP.NET • Chapter 9

437

Figure 9.12 Using the Class Viewer

The Class Viewer allows you to type in the keyword to search and display all matching instances of the search word. For example, Figure 9.12 shows the search result for “overflowexception.” It also displays the corresponding namespace and the members of the selected class.

www.syngress.com

438 Chapter 9 • Debugging ASP.NET

Summary

Error handling is an important aspect of software development. Good robust applications anticipate various errors and take an active role in resolving them without crashing the program. In this chapter, we have seen two distinctive methods of error handling—structured and unstructured.While the unstructured error handling mechanism continues to be supported in .NET, it is recommended that programmers make the switch to the structured error handling mechanism using the Try-Catch-Finally statement. Besides handling errors, the new tracing capability found in .NET makes the life of a programmer much easier. No longer do you have to insert Response.Write statements into your application, you can now trace your application using the Trace class. Removing the Trace statements during deployment is simply a matter of setting an attribute. Finally,Visual Studio .NET allows you to set breakpoints in your application so that the flow of variables and codes can be examined during runtime.

Solutions Fast Track

Handling Errors

;There are four main categories of programming errors: syntax, compilation, runtime, and logic errors.

;Visual Studio .NET IDE provides help for detecting syntax errors.

;Runtime errors can be handled using structured and unstructured error handling mechanisms.

;Structured handling using the Try-Catch-Finally statement is the recommended mode for handling runtime errors in .NET.

Page Tracing

;The Trace class provides tracing capability.

;Turning tracing on and off is easy.

;Trace information can be grouped into multiple categories for easier viewing and it can be written into log files, viewable using the Event Viewer.

;Tracing can be done at the page level or at the application level.

www.syngress.com

Debugging ASP.NET • Chapter 9

439

Using Visual Studio .NET Debugging Tools

;Programmers can use the Visual Studio .NET IDE to set breakpoints in their application.

;Breakpoints allow you to examine variables and trace the execution flow of your application.

;The Object Browser and Class Viewer provide quick reference to the various class libraries.

Frequently Asked Questions

The following Frequently Asked Questions, answered by the authors of this book, are designed to both measure your understanding of the concepts presented in this chapter and to assist you with real-life implementation of these concepts. To have your questions about this chapter answered by the author, browse to www.syngress.com/solutions and click on the “Ask the Author” form.

Q: Is the Try-Catch-Finally block available in C# as well?

A: Yes, the Try-Catch-Finally block is available in both VB.NET and C#.

Q:Can I use both structured and unstructured error handling within a function/subroutine?

A:No, you cannot use both error handling mechanisms at the same time. It is recommended you use structured error handling in .NET.

Q:When I try to run my ASP.NET application in VS.NET, I encounter this error message “Error while trying to run project: Unable to start debugging on the Web server.The project is not configured to be debugged.”Why does this occur?

A:This is caused by the setting of the debug attribute within the <compilation> element. During development stage, set the value of the debug attribute to “true.” Remember, however, to set this attribute to “false” when you are ready to deploy your application.

www.syngress.com