Take the 2-minute tour ×

It seems that the System.Diagnostics.Debug and System.Diagnostics.Trace 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?

share|improve this question

6 Answers 6

up vote 26 down vote accepted

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.

share|improve this answer
I disagree totally with this answer. Trace is a lower level than Debug and should never be used in production. In my experience, trace is used at the start of methods or "tracing" a piece of work as it passes through a workflow and in some ways, shows the call stack. Everything above and including Info should be the only levels used in production. That is Info, Warning, Error, Fatal. –  Vince Panuccio Jan 9 '13 at 22:12
I think one other difference is that you can configure Trace Listeners in your application and route the output of the trace accordingly. The default trace listener in Visual Studio will direct the trace messages to Debug output stream. –  orad Jun 11 at 22:23

Debug is used to pure debugging purpose,it emits rich message in debug execution ( Debug mode) Trace helps in application debugging,bug fixing and profiling (After Release).

Debug class is no use in release mode.

share|improve this answer

The only difference between trace and debug is that trace statement are included by default in the program when it is compiled into a release build, whereas debug statement are not. Thus , the debug class is principally used for debugging in the development phase ,while trace can be used for testing and optimization after as application is compiled and released

share|improve this answer

For highly performance sensitive code blocks, leaving Trace compiled-in but disabled might make a performance difference.

share|improve this answer

You've answered your own question. If Debug messages stayed in, people could see them. For example, let's say you do:

Debug.WriteLine("Connecting to DB with username: blah and PW: pass");

Anyone who decompiles your code can see that. But that may be something vitally important for you to know during testing.

Trace is different. If you are going to do Trace, I'd likely just use log4net.

share|improve this answer

I'd look at using log4net for tracing as it's capabilities are much more flexible and robust.

But for true debug messages that I never intend for anyone other than me or an internal tester to see, I'd probably stick with Debug.

share|improve this answer

Your Answer


