tags:

views:

64

answers:

2

And what is the main difference between Trace.Write and Debug.Write?

Thanks!

+1  A: 

The difference is in Release mode.

Debug.Write will not be compiled into the code when the DEBUG symbol is not defined, i.e. compiling in Release mode.

However, Trace.Write will be compiled in both Debug mode and Release mode.

Stan R.
+4  A: 

Both are conditionally-compiled using the [Conditional] attribute.

If the TRACE flag is defined in the build, then calls to the Trace class will result in trace output being written. By default, TRACE is defined in both debug and release mode. If the flag is not defined, nothing will happen.

If the DEBUG flag is defined, then calls to the Debug class result in output being written to the debug stream. By default, DEBUG is only defined in debug mode.

The other major difference is that with tracing it's easy to customize the trace listeners and decide later on what you want to do with the trace output. It's more flexible than debug output, and generally better suited to logging in a production application.

Aaronaught
So if the TRACE flag is defined by default, is there a way to unset it in release or debug modes?
Carlo
@Carlo: Sure, it's in the project properties, Build tab. There are two checkboxes, one that says "Define DEBUG constant" and another that says "Define TRACE constant." Those correspond to how the `Debug` and `Trace` classes and their corresponding `Write` methods will behave.
Aaronaught
Great! Thanks, great answer!
Carlo
One more question. What about when you're not running it in Visual Studio? What happens to the output that Trace writes?
Carlo
@Carlo: The flag affects how the project is compiled. It doesn't matter where you run it from, it's the exact same program; the only difference is that in VS you have a debugger attached. Under normal circumstances, that won't alter the program's behaviour in any significant way.
Aaronaught
Makes sense. Thank you.
Carlo