Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between System.Diagnostics.Trace, System.Diagnostics.Debug and System.Console?

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?

like image 626
Jakub Arnold Avatar asked Sep 30 '14 17:09

Jakub Arnold


1 Answers

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.

like image 139
Scott Chamberlain Avatar answered Oct 12 '22 04:10

Scott Chamberlain