Tracing in ASP.NET
page 2 of 4
by Phil Winstanley
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 36806/ 89

Use Tracing to Debug a page

Visual Studio .NET and ASP.NET offer the developer some really cool debugging tools, but if you don’t have access to them, then you are stuck with the good old ways of doing things, writing out your variables until you find the problem.

Let’s just quickly throw together some business logic that has a variable changing it’s value through out the page, in real life this could be anything from the content of a shopping basket right up to some high level mathematics. For now it’s just me playing around with a number.

private void Page_Load(object sender, System.EventArgs e)

{

          //Some fake business logic

 

          //Take a variable, any variable

          int i = 0;

 

          //Perform some addition

          i += 10;

 

          //Perform some more addition

          i += 30;

 

          //Do something slightly more complex than addition

          i = (i * (i + -2));

 

          //Pass it to a method

          i = DoSomething(i);  

 

}

 

public int DoSomething(int aNumber)

{

         

          //Blow up, just to prove a point

          if (aNumber > 0)

          {

                   throw new Exception("aNumber is more than 0!");

          }

          else

          {

                   //Set it to 0

                   return 0;

          }

 

}

In the Page_Load event we can see how we’re changing the value of i quite a bit. If something is going wrong like this: -

Trace Information

Category

Message

From First(s)

From Last(s)

 

 

 

 

 

 

aspx.page

Begin Init

 

 

 

 

 

 

 

 

aspx.page

End Init

0.001180

0.001180

 

 

 

 

 

 

Unhandled Execution Error


aNumber is more than 0!
  at intranet._default.DoSomething(Int32 aNumber) in c:\inetpub\wwwroot\intranet\default.aspx.cs:line 56
  at intranet._default.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\intranet\default.aspx.cs:line 44
  at System.Web.UI.Control.OnLoad(EventArgs e)
  at System.Web.UI.Control.LoadRecursive()
  at System.Web.UI.Page.ProcessRequestMain()

0.002194

0.001014

 

 

 

 

 

 

This gives us no information as to where our sums are going wrong, so we can use the Trace commands to output the variables value throughout it’s life time.

So here is the same method above with some Trace calls: -

private void Page_Load(object sender, System.EventArgs e)

{

          //Some fake business logic

 

          //Take a variable, any variable

          int i = 0;

 

          Trace.Write("i","Variable after it's been created is " + i.ToString());

 

          //Perform some addition

          i += 10;

 

          Trace.Write("i","Variable after it's been added to is " + i.ToString());

 

          //Perform some more addition

          i += 30;

 

          Trace.Write("i","Variable after it's been added to again is " + i.ToString());

 

          //Do something slightly more complex than addition

          i = (i * (i + -2));

 

          Trace.Write("i","Variable after it's been messed around with is " + i.ToString());

 

          //Pass it to a method

          i = DoSomething(i);  

 

          Trace.Write("i","Variable after it's been processed is " + i.ToString());

 

}

 

public int DoSomething(int aNumber)

{

         

          //Blow up, just to prove a point

          if (aNumber > 0)

          {

                   throw new Exception("aNumber is more than 0!");

          }

          else

          {

                   //Set it to 0

                   return 0;

          }

 

}

When we run our page with the Trace information included we get it all placed in to the Trace log: -

Trace Information

Category

Message

From First(s)

From Last(s)

 

 

 

 

 

 

aspx.page

Begin Init

 

 

 

 

 

 

 

 

aspx.page

End Init

0.001043

0.001043

 

 

 

 

 

 

i

Variable after it's been created is 0

0.002366

0.001323

 

 

 

 

 

 

i

Variable after it's been added to is 10

0.002413

0.000047

 

 

 

 

 

 

i

Variable after it's been added to again is 40

0.002442

0.000030

 

 

 

 

 

 

i

Variable after it's been messed around with is 1520

0.002472

0.000030

 

 

 

 

 

 

Unhandled Execution Error


aNumber is more than 0!
  at intranet._default.DoSomething(Int32 aNumber) in c:\inetpub\wwwroot\intranet\default.aspx.cs:line 56
  at intranet._default.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\intranet\default.aspx.cs:line 44
  at System.Web.UI.Control.OnLoad(EventArgs e)
  at System.Web.UI.Control.LoadRecursive()
  at System.Web.UI.Page.ProcessRequestMain()

0.003193

0.000720

 

 

 

 

 

 

Now we can see by looking at the trace log, what’s happening to the number, and we can modify our business logic accordingly.

private void Page_Load(object sender, System.EventArgs e)

{

          //Some fake business logic

 

          //Take a variable, any variable

          int i = 0;

 

          Trace.Write("i","Variable after it's been created is " + i.ToString());

 

          //Perform some addition

          i += 10;

 

          Trace.Write("i","Variable after it's been added to is " + i.ToString());

 

          //Perform some more addition

          i += 30;

 

          Trace.Write("i","Variable after it's been added to again is " + i.ToString());

 

          //Do something slightly more complex than addition

          i = (i * -(i + -2));

 

          Trace.Write("i","Variable after it's been messed around with is " + i.ToString());

 

          //Pass it to a method

          i = DoSomething(i);  

 

          Trace.Write("i","Variable after it's been processed is " + i.ToString());

}

Now when we look at the page the exception does not occur and we can see why when we look at the Trace information: -

Trace Information

Category

Message

From First(s)

From Last(s)

 

 

 

 

 

 

aspx.page

Begin Init

 

 

 

 

 

 

 

 

aspx.page

End Init

0.003425

0.003425

 

 

 

 

 

 

i

Variable after it's been created is 0

0.006264

0.002838

 

 

 

 

 

 

i

Variable after it's been added to is 10

0.006327

0.000063

 

 

 

 

 

 

i

Variable after it's been added to again is 40

0.006370

0.000044

 

 

 

 

 

 

i

Variable after it's been messed around with is -1520

0.006414

0.000044

 

 

 

 

 

 

i

Variable after it's been processed is 0

0.007609

0.001195

 

 

 

 

 

 

aspx.page

Begin PreRender

0.007684

0.000074

 

 

 

 

 

 

aspx.page

End PreRender

0.009410

0.001727

 

 

 

 

 

 

aspx.page

Begin SaveViewState

0.034088

0.024677

 

 

 

 

 

 

aspx.page

End SaveViewState

0.085753

0.051665

 

 

 

 

 

 

aspx.page

Begin Render

0.085834

0.000081

 

 

 

 

 

 

aspx.page

End Render

0.288975

0.203141

 

 

 

 

 

 

Now we have fixed our code, we don’t need to do anything to the Trace commands as they do not interfear with the visual side of our page. All we need to do is set the Trace property to false in the page directive Tag.

<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="intranet._default" Trace="False" %>



View Entire Article

User Comments

Title: ateeq   
Name: helo
Date: 2012-12-18 4:43:34 AM
Comment:
flksd fsdjlk fjds djs sda s sadlj falk asldfj as
Title: Programmer   
Name: Terence
Date: 2004-07-16 12:42:52 PM
Comment:
It looks like the last post cut some of the code off... but you get the idea. Terence.
Title: Programmer   
Name: Terence
Date: 2004-07-16 12:40:45 PM
Comment:
Nice job Phil. I tranlated your C# code to vb.net for those who are interested in that. Cheers. Terence
'Translated from C# to VB.net.
'July 16, 2004
'Terence J Wehle
'We use shared Methods in this translation so that creating an instance
'of the object is not necessary. This is appropriate since
'each methods is acting only on passed in parameters.

'In this article, Phil Winstanley proves that contrary to the
'NET SDK documentation that “Trace statements are processed and displayed
'only when tracing is enabled.”, the statements are in fact processed,
'although not displayed. If you have many such statements, particularly
'in a loop, this could slow things down for the page even thought the
'Trace is set to "False". This solution checks first whether tracing
'is enable before making the call and thus save resources. It is a
'wrapper for the trace calls.
'---------------------
Imports System
Namespace TJW


Public Class Trace

Public Sub Write(ByVal Message As String)

If (System.Web.HttpContext.Current.Trace.IsEnabled) Then

Try
System.Web.HttpContext.Current.Trace.Write(Message.ToString())
Catch e As Exception
System.Web.HttpContext.Current.Trace.Warn("Trace", "Write", e)
End Try

End If

End Sub

Shared Sub Write(ByVal Category As String, ByVal Message As String)

If (System.Web.HttpContext.Current.Trace.IsEnabled) Then

Try
Throw New Exception("This is a bogus error")
System.Web.HttpContext.Current.Trace.Write(Category.ToString(), Message.ToString())
Catch e As Exception
System.Web.HttpContext.Current.Trace.Warn("Trace", "Write", e)
End Try

End If

End Sub

Public Sub Write(ByVal Category As String, ByVal Message As Stri






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-11-14 8:51:50 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search