As far as I understand, System.Console
will write to STDOUT by default, but what about System.Diagnostics.Trace
and System.Diagnostics.Debug
? What are the default behaviors, and are they configurable in any way?
It also seems that different people use different things (on the internet), but I'm assuming that most of what I've found is wrong, since there should be specific semantics for each of these, right? And if so, are there any frameworks (like ASP.NET or WPF) that make special use of these?
Also one last question, what are the rules of thumb for picking which one of these to use?
Debug
and Trace
both write out to the same location, the Listeners
collection. By default it is routed to Visual Studio's Debug window, however you can put code in your app.config
file to redirect it to other locations when you are not debugging.
The difference between Debug
and Trace
is all of the methods in Debug
only write out when the DEBUG
compilation symbol is set (default on for debug, off for release) when the symbol is not set the methods are never called in your code. Trace looks for the TRACE symbol (default on for both debug and release). Other that that, the two classes are identical. In fact if you modify Debug.Listeners
to add a new listener it will also modify Trace.Listeners
as both just point to the internal static property TraceInternal.Listeners
As for picking which one to use, Do you want diagnostic information to show up in release and debug mode? use Trace, Debug only? use Debug. Do you want it to be visible to a end user without a debugger attached? use Console or add a console trace listener.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With