Use Trace.Write Instead of Response.Write for Debugging
It's probably a throw back to the days of classic ASP, but I still see a lot of
code that uses Response.Write to display debugging information. The concept
is that you simply comment out the debugging lines when you're done
with them and they'll be there waiting to be uncommented the next time you need them.
While the process actually works quite well, for those of you using
ASP.NET there's a better way.
For those of you who are new to Tracing, here's how it works. By setting Trace
equal to true in the Page directive at the top of your ASP.NET page like this:
<%@ Page Trace="true"%>
ASP.NET will add all sorts of cool debugging information to the bottom of the page
when it is run. It includes the following sections:
Request Details, Trace Information, Control Tree,
Session State, Application State, Request Cookies Collection
Response Cookies Collection, Headers Collection, Response Headers Collection,
Form Collection, Querystring Collection, and Server Variables.
If you look at that list, you'll probably find that quite a few of the things you've been
using Response.Write to display are already included by default. But the best part is
that it doesn't stop there.
For any debugging information that's not already included, all you have to do is use
Trace.Write wherever you would have previously used Response.Write, and
the results will be added to the Trace Information section. What's more, you can even
provide labels that will help categorize your debugging information if you want.
You want more? Well what if you could make your custom tracing information
stand out from all the rest? You can... simply use Trace.Warn and your entries will
appear in red so they're easy to spot.
Still not enough for you... well how about this? You don't have to comment out the
debugging lines when you're done with them! Simply set
<%@ Page Trace="false"%>
and all your debug statements no longer execute. Now you don't have to hunt through your code
looking for the Response.Write lines you added in order to comment them out so your visitors
don't see them.
So the next time you start to use Response.Write to debug a page, try Trace.Write instead.
It might take a little getting use to, but once you do I think you'll find it saves you quite
a bit of time and aggravation.
If you have a tip you would like to submit, please send it to:
webmaster@asp101.com.