ASP.NET 2.0 Trace Listeners

I have been spending a lot of time exploring Visual Studio’s debugging options because I deal with a lot of large projects and web applications that require me to search through thousands of lines of code. Web applications are especially difficult to debug. For example, Storefront 6.0 has hundreds of classes which are not associated with web pages so I cannot determine the value of a variable just by enabling tracing.

ASP.NET 2.0 has added trace listeners which can redirect debug and trace statement output to a log file or the Windows Event Log. This has proven to be very useful for web services and web applications that cannot be debugged in Visual Studio. Now I can insert System.Diagnostics.Debug.WriteLine statements in a web service that is giving me problems and have the information I need written out to a log file while my client application consumes the web service. Since I cannot run the web service directly from Visual Studio this is the only way I can get any debug output while the web service is executing.

Web applications can be run locally in debug mode but it is very difficult to do remote debugging without a lot of cooperation from the web hosting company. Fortunately, trace listeners would allow me to write the debug output to a log file which I could then review.

To add a trace listener you just add a system.diagnostics section to your application config file or web.config file which will provide all the configuration information for the trace listener. It can also be specified in code. To disable the trace listener you just comment out that section of the web.config file. You definitely want to do that or the log file will continue to grow and grow with each application run.

This entry was posted in ASP.NET. Bookmark the permalink.

Leave a Reply

Your email address will not be published.