C# – Trace vs Debug in .NET BCL

cdebugginginstrumentationnet

It seems that the

are largely the same, with the notable exception that Debug usage is compiled out in a release configuration.

When would you use one and not the other? The only answer to this I've dug up so far is just that you use the Debug class to generate output that you only see in debug configuration, and Trace will remain in a release configuration, but that doesn't really answer the question in my head.

If you're going to instrument your code, why would you ever use Debug, since Trace can be turned off without a recompile?

Best Answer

The main difference is the one you indicate: Debug is not included in release, while Trace is.

The intended difference, as I understand it, is that development teams might use Debug to emit rich, descriptive messages that might prove too detailed (or revealing) for the consumer(s) of a product, while Trace is intended to emit the kinds of messages that are more specifically geared toward instrumenting an application.

To answer your last question, I can't think of a reason to use Debug to instrument a piece of code I intended to release.

Hope this helps.