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" %>