C# Debugging


Debugging GUI applications for me mostlyconsists of printing out debug statements in the form of a dialog boxwith some text. While this technique was helpful for small to mediumsize apps I find writing large apps with a dialog box popping up afterevery other statement is counterproductive. With this in mind, I setout to find a better method for displaying debug statements duringruntime. Enter C#.

C# solves three problems I faced whendesigning the useful and scalable debugging system. These problemsexist either in Java, C/C++, or both (my main programming languages).

1. Not having very much meta-information (e.g. line number, method name, etc.)
2. Having to add and remove debug statements whenever the debug focus shifts
3. Having the debug statements compiled into the program affecting performance

Ok, discussing the solution for these problemsin order we’ll start with number one. Basically a few classes from theSystem.Reflection and System.Diagnostics namespaces solve this problem.Using the System.Diagnostics.StackFrame class the call stack can beexplored and stack details such as what is on the stack level above thecurrent one, what line is a function being called from, etc. And withthe System.Reflection classes the names of functions, namespaces,variable types, etc., can be ascertained. Applying all this to theproblem at hand here’s some code that retrieves the file name, linenumber, and method name of the function that called it.

// create the stack frame for the function that called this function
StackFrame sf = new StackFrame( 1, true );

// save the method name
string methodName = sf.GetMethod().ToString();

// save the file name
string fileName = sf.GetFileName();

// save the line number
int lineNumber = sf.GetFileLineNumber();

Moving right along to problem number two let’sdiscuss how to selectively debug different sections of a program duringruntime. Number two ties in with number one in that information fromnumber one will help us filter debug statements from being displayed.For this example we’ll filter by namespace. So say that you havefifteen namespaces in your program and right now you only want todisplay debug statements from one all you would have to do is tell thedebug class only allow that one namespace to display debug statements.Simple enough. Here’s some code.

// create the namespaces hashtable
namespaces = new Hashtable();

// get the assembly of this class
Assembly a = Assembly.GetAssembly( new Debug().GetType() );

// now cycle through each type and gather up all the namespaces
foreach( Type type in a.GetTypes() )
{
// check if the namespace is already in our table
if( ! namespaces.Contains( type.Namespace ) )
namespaces.Add( type.Namespace, true );
}

The above code will create a Hashtable objectcontaining all the namespaces in the same assembly as the Debug classof which this code is a part. Of course, this is only half of thesolution so here is the actual filtering code.
Continues…

Pages: 1 2

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!