|I l @ ve RuBoard|
ASP.NET includes several new features to aid debuggers that weren't included with Classic ASP. The first of these is the Trace object, which allows programmers to include debug output in their pages that can be controlled using page-level or application-level directives. Trace is a new intrinsic object that, like Response , Request , Server , and so on, is directly accessible in your ASP.NET code. When deactivated, these statements are not included in the compiled version of the page. As a result, there is no performance hit as there would have been in Classic ASP code, like that shown in Listing 13.1. Using this kind of code resulted in extra overhead from the function call involved and the If-Then statements that had to be executed every time the page was executed. If these kinds of constructions are used throughout an application, this overhead can quickly add up and impede scalability.
Listing 13.1 Source code for classicdebug.asp.
Dim sql Dim book_id book_id = Request("book_id") Call Debug("Book ID: " & book_id) sql = "SELECT * FROM mytable WHERE mycolumn = " & book_id Call Debug(sql) 'Insert database code here Function Debug(myString) If Session("debug") = "" Then Session("debug") = 0 If Session("debug") = 1 Then Response.Write myString & "<br>" End If End Function
The Trace object in ASP.NET provides you with the same debugging functionality and much more, without impeding the performance of our application (at least, not when tracing is turned off!). By simply turning Tracing on for a page, you get a great deal of information about the execution of the page, including elapsed time information. Figure 13.1 shows a simple ASP.NET trace example, for the HelloWorldVB.aspx example used in Chapter 2, "An Introduction to ASP.NET." The source code is listed in Listing 13.2, and is identical to the source in Chapter 2 except for the addition of the Trace="True" statement in the Page directive.
Figure 13.1. An ASP.NET trace example.
Listing 13.2 Source code for HelloVB.aspx
<%@ Page Language="VB" Trace="True"%> <html> <head> </head> <body> <% Response.Write("Hello World") %> </body> </html>
With tracing enabled, you get a wealth of information about the execution of your code. There are eight main sections of the trace output page: Request Details, Trace Information, Control Tree, Cookies Collection, Headers Collection, Form Collection, Querystring Collection, and Server Variables. The different collections are only displayed if they have values to display, so frequently some will be omitted from trace output.
This section of the trace output contains simple metadata about the page execution, such as when it occurred and what kind of request ( GET or POST ) it was. The session ID and HTTP status code are also included.
The Trace Information section is the area where your Trace.Write() or Trace.Warn() statements will appear (we'll cover these methods in detail shortly). It lists messages in a table view, including the category of the message and the message text. Also included is the time since the previous message (From Last) and a running tally of the time since the page began execution (From First).
ASP.NET controls are covered in Chapters 5 through 9. Any controls that are used on the page are listed in this section of the trace output, along with their memory size in bytes (because controls can include other controls, the size is listed both with and without child controls). The ASP.pagename_aspx control (where pagename is the name of your ASP.NET page) will be listed here even if no other controls are used on the page, because the Page object is used to render the trace output and is the base class for all ASP.NET pages.
Any cookies sent by the client in the request header are displayed in this section.
The Headers collection displays the HTTP headers that were passed to the server with this request.
The Form collection displays any form values that were POSTed to the page.
The Querystring collection displays any querystring values that were appended to the URL or the result of a GET form submission.
For classic ASP developers, this is the Request. ServerVariables collection that you are familiar with. If you're unfamiliar with this collection, it includes many name-value pairs, such as the server name, IP address, client IP address, page path , and much more. This collection often has information that is useful when debugging.
|I l @ ve RuBoard|