Debugging the Business Layer

I now try and separate out my code into distinct layers.  One problem with this is debugging the Business Layer through the web Presentation Layer.  I though I had solved this by adding a reference to System.Web in my Business Layer and then grabbing a handle to System.Web.HttpContext.Current.Trace.  I could then happily write debug information to my web page. 

As I strive to achieve Zen Layer Separation (ZLS) I thought that this tied my Business Layer to my Presentation Layer too closely.  So I took a look at System.Diagnostics.  This Namespace has lovely built in tools for debugging your application, but I always thought it was more suited for desktop development.  M$ was kind enough to make the Debug and Trace functions fully extensible through custom TraceListeners.  So, in five minutes, I created the WebTraceListner. 

public class WebTraceListener : System.Diagnostics.TraceListener {

private static System.Web.TraceContext Trace = null;

public WebTraceListener() {

try { Trace = System.Web.HttpContext.Current.Trace; }

catch( Exception e ) {

throw new Exception( "WebTraceListener may only be used as part of an ASP.Net Web Application", e ); }

}

public override void WriteLine( string Message ) {

Trace.Warn( Message );

}

//...Abbreviated for brevity

}

Now, debugging the Business Layer is as easy as System.Diagnostics.Debug("My debug messgage") and those messages are merrily routed to the ASP TraceContext for display on your page.  The beauty of this is I could easily debug my Business Layer in a WinForm app by using the built-in TextWriterTraceListener instead of the WebTraceListner.  I don't need to change any code at all, just a small change to the configuration file App.exe.config/Web.config.  Instead of adding WebTraceListener in the section, you can add a listener that writes debug information to the console or a text file.

<system.diagnostics>

<switches>

<add name="WebSwitch" value="4" />

--

0 = Off Output no tracing and debugging messages.

1 = Error Output error-handling messages.

2 = Warning Output warnings and error-handling messages.

3 = Info Output informational messages, warnings, and error-handling messages.

4 = Verbose Output all debugging and tracing messages.

-->

switches>

<trace autoflush="false" indentsize="4">

<listeners>

<clear/>

<add name="WebTraceListener" type="Development.WebTraceListener,Development" />

listeners>

trace>

system.diagnostics>

Just one more step on the road to ZLS.

posted @ Friday, January 21, 2005 11:01 AM


Print

Comments on this entry:

# re: Debugging the Business Layer

Left by Willie Tilton at 1/25/2005 7:37 AM

Would it be possible to have a Trace or debug layer that just mirrored whatever type of app you are running. For example: myProj.Trace instead of HttpContext.Current.Trace.

In your Trace wrapper class static method you'd just reference the Console.Writeline or Trace.Warn method that you'd want your app to point to. I do similar things in my security layer. As for writing to an error log...I'd say use what you got, but for simple debug...

# re: Debugging the Business Layer

Left by Bill at 1/26/2005 8:47 AM

Yeah, that is the idea with custom listeners. Your code, no matter what project, will look like this:

using System.Diagnostics
...
Trace.Write( "My Trace Output");

That code will work in a web app, desktop app, you name it. Then you just pick the TraceListner you want to use by editing your config file.

Your comment:



 (will not be displayed)


 
 
 
Please add 4 and 5 and type the answer here:
 

Live Comment Preview:

 
«August»
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456