03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Trac<strong>in</strong>g Your <strong>ASP</strong>.<strong>NET</strong> Web Pages x 723<br />

Add<strong>in</strong>g Your Own Information to the Trace<br />

Add<strong>in</strong>g your own data to the trace is useful if, for example, you want to see the value of a variable,<br />

or want to f<strong>in</strong>d out if a specific event fires, <strong>and</strong> when it fires exactly.<br />

You can add <strong>in</strong>formation to the trace by us<strong>in</strong>g the Trace class. This class exposes two useful methods:<br />

Write <strong>and</strong> Warn. Both do pretty much the same th<strong>in</strong>g: they add <strong>in</strong>formation to the trace that<br />

you can optionally put <strong>in</strong> a category you make up yourself. The only difference between Write <strong>and</strong><br />

Warn is that messages written by Warn appear <strong>in</strong> red. You could use the Warn method for unexpected<br />

situations because the message will draw more attention.<br />

In the follow<strong>in</strong>g exercise, you see how simple it is to add your own <strong>in</strong>formation to the trace us<strong>in</strong>g<br />

the Warn <strong>and</strong> Write methods.<br />

TRY IT OUT<br />

Add<strong>in</strong>g Trace Data to Your Pages<br />

In this Try It Out, you add some custom <strong>in</strong>formation to the <strong>ASP</strong>.<strong>NET</strong> trace. You use the Write method<br />

to write out trace <strong>in</strong>formation <strong>in</strong> a normal page execution, <strong>and</strong> use the Warn method for unexpected<br />

scenarios.<br />

1. Open CalculatorDemo.aspx, switch to its Code Beh<strong>in</strong>d, <strong>and</strong> locate the Click h<strong>and</strong>ler for the<br />

Calculate button.<br />

2. Right before the Select Case (<strong>VB</strong>.<strong>NET</strong>) or switch statement (C#), add the follow<strong>in</strong>g Trace<br />

.Write call:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Trace.Write(Str<strong>in</strong>g.Format("Perform<strong>in</strong>g the calculation with the {0} operator",<br />

OperatorList.SelectedValue))<br />

Select Case OperatorList.SelectedValue<br />

C#<br />

Trace.Write(str<strong>in</strong>g.Format("Perform<strong>in</strong>g the calculation with the {0} operator",<br />

OperatorList.SelectedValue));<br />

switch (OperatorList.SelectedValue)<br />

3. Near the bottom of the event h<strong>and</strong>ler, modify the Else statement for the check that ensures that<br />

both TextBox controls conta<strong>in</strong> a value:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Else<br />

Result.Text = Str<strong>in</strong>g.Empty<br />

Trace.Warn("Custom Category",<br />

"TextBox controls are empty; time to add Validation controls?")<br />

End If<br />

C#<br />

else<br />

{

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!