Thursday, July 29, 2004

Another Tip Thanks to Dan - Making Debugging Easier

Dan (know as .NET Dan to his family and groupies) came up with another great idea that I thought was worth sharing. The project I'm working on at the moment is a validation framework that essentially involves a whole heap of plumbing code to allow strongly-typed rules written in compiled code to check an arbitrary data source. I inherited the project from Mitch, so if its a dumb idea, let him know.



I have been focussed on making the development-time friendliness of the framework better over the last month, and one of the problems we faced is that it can be difficult for a developer to track a validation failure back to a rule. When a rule fails, a particular method is called by the rule to signify the failure. Dan's idea was to output a message to the VS.NET Output window similar to a compilation error, allowing the developer to double-click the message and be instantly transported to the line where the validation failure is signalled. The code to do this is:


if (System.Diagnostics.Debugger.IsAttached){
  StackTrace st = new StackTrace(true);
  for(int ix = 0; ix < st.FrameCount; ++ix){
    StackFrame sf = st.GetFrame(ix);
    if (sf.GetMethod().Name == "Validate")
    {
      string sfMessage = String.Format("{0} ({1}, 0): Validation failed: {2}", sf.GetFileName(), sf.GetFileLineNumber(), message);

      Trace.WriteLine(sfMessage);
      break;
    }
  }
}


I'm obviously looking for the frame in the stack where a method called Validate is but you get the idea. Formatting a stack frame this way makes it automatically navigatable from the Output window.

1 Comments:

At March 16, 2006 at 5:22 AM, Blogger answer-man said...

ps I'm having a little trouble sending comments so if I do it twice please excuse me and I apologize.

 

Post a Comment

<< Home