The trace Section

One of the problems developers experienced with ASP was difficulty obtaining detailed debugging information. Exactly what was the page doing when an error occurred? What portions of the code had been run? ASP.NET has much improved debugging information, and the trace section of the Web.config file allows you to specify settings for the trace service. Figure 4-15 shows a page that has been run with tracing enabled and pageOutput set to true.

U LJI Ï»- 'i-flrh

a

1 ^ J J ^ xi'f'Jt—

» > ir j :3

- tJft.

Irt» -

Login Pag*

PjSimOKO

dflTEiaienfl est**: r

|Hi!(|LI|-M lift. lik 1

"mil»!' WlHtl*' -mimrnifiin: VLZ/;KL irirn PU

Hui|iiml 1* vi

MJI i^jao: Patpnn» t*40dlpg ■

ne

■ fraCï 11 il ■ 1 1

f null rWM

E-v ■ Ml

■r.-r." Vi)t tf'l

0 0001«

ussu*

«V* j/^ tvfi prtCfW CtbJ F-b Jt"

UIWl

D.lbMJT

^u}i>)

J! »jd Sr.! 1 v-f ' f » ■H ]MJj hfn rwihi

O 2-S1ÏW

igfWli ttlHH ¡jWI»

J

Figure 4-15 : The output of the Login.aspx page shown in Listing 4-6 when tracing is enabled and pageOutput is set to true The <trace> tag supports five attributes, as described in Table 4-13.

Table 4-13 : Attributes of the <trace> Tag

Attribute

Option

Description

enabled

Specifies whether tracing is enabled.

true

Specifies that tracing is enabled.

false

Specifies that tracing is disabled.

Table 4-13 : Attributes of the <trace> Tag

Attribute

Option

Description

This is the default.

requestLimit

Specifies the number of trace requests to save on the server. The default is 10.

pageOutput

Specifies whether trace information should be displayed at the end of each page.

true

Specifies that trace output is appended to each page.

-1

false

Specifies that trace output is not appended to each page. This is the default.

traceMode

Sets the order of trace output.

SortByTime

Specifies that output is sorted by time (thus, displayed in the order the events being traced occurred). This is the default.

SortByCategory

Specifies that output is displayed alphabeticall y by category. See the text in this

Table 4-13 : Attributes of the <trace> Tag

Attribute

Option

Description

section for information about user-defined categories.

localOnly

Specifies whether the trace viewer is available only on the host Web server.

true

Specifies that trace output is available only on the server console. This is the default.

false

Specifies that trace output is available on any client, not just the Web server.

In Figure 4-15, all trace output has a category of aspx.page, and it is generated automatically by the .NET Framework, with no explicit trace code in the page source. However, this is only half the power of ASP.NET tracing. Suppose, for example, that the login page from Listing 4-6 wasn't responding the way you expected. Perhaps the following code seemed not to be working correctly:

if ((UserEmail.Value == "[email protected]") &&

(UserPass.Value == "password"))

FormsAuthentication.RedirectFromLoginPage( UserEmail.Value, false);

Msg.Text = "Invalid Credentials: Please try again";

You can use the Trace class to add user-defined trace statements in the trace output, like this:

if ((UserEmail.Value == "[email protected]") && (UserPass.Value == "password"))

Trace.Write("MyCategory", "Authenticated");

FormsAuthentication.RedirectFromLoginPage( UserEmail.Value,false);

Msg.Text = "Invalid Credentials: Please try again"; Trace.Write("MyCategory", "Invalid Credentials");

If you ran the page shown in Listing 4-6 with the modifications shown above and entered an invalid user name or password, the trace would have one "MyCategory" trace line inserted into the output, as shown in Figure 4-16.

w rlai rsr

mlfyuam hhpmu tccadilf r r-KP [niiirm.jiKr"

«pi ie#7i t-vt iifn m-i" >"35«siir-,i»c'*»i 5-"rrrrj !>y up1 iwji 1>>d xttw-d Inr lip1

Hyiiiwgfl-Y Inrv: Cmitfr:11

up- r>v (tu IUh taiiisUiwc

a amen DWKH

0 woup

[race QJipJl

Figure 4-16 : Trace output with explicit trace category added In addition to the Web.config sections described in this chapter, there are a couple other sections that are not generally modified (such as the globalization section).

0 0

Post a comment