Debugging and tracing are related to each other, but differ from each other. The System.Diagnostics namespace supports both of them.
Use Debug.Write/WriteLine to write debug messages to the log file. (And to the output window.) Use Debug.Assert , well, almost everywhere. All code that you write using the Debug class is deleted before compilation, if you do not define the DEBUG symbol in your code - this is done by default in the Debug and Release configuration settings.
Use Trace.Write/WriteLine to write trace messages. These methods are functionally equivalent to the methods of the Debug class; the difference is that they are deleted if you do not define the TRACE character in your code.
The debug and trace classes also have a small cumbersome infrastructure of other classes, such as TextWriterTraceListener , that allow you to do things like change the output of the trace log from a local file on disk to a web service (or nothing) by simply editing the application configuration file. My favorite trick is to implement TextWriter , which can write text in a TextBox , and use it to redirect all debugging and tracing data to the user interface.
You can also set flags in the application configuration file that respect the WriteIf and WriteLineIf methods, but in my experience they become quite cumbersome.
I find that in general, I am happier if I create my own static class to wrap Trace methods. This allows me to do things like turning tracing on and off in the user interface by simply setting the property of my Trace class.
Finally: if you need to attach a tool to doing something like tracing, when each method starts and ends, the tool for this is the wonderful PostSharp.
source share