Disable debug output

I would like to color some printing instructions in my code to show where I am and print important values ​​in the console window.

How can I do this, but then you can disable it for the version?

+4
source share
6 answers

All calls to System.Diagnostics.Debug.Print() will be deleted when switching to the release version.

+14
source

Use Log4net with logging at the debugging level in the release and a warning or error level during production.

The advantage of this is that you can enable registration in the release environment if you have problems that cannot be recreated during the development process.

[EDIT] FWIW, I found that now I am doing LOT less debug log using Test-Driven Development. Most of my logging is related to the warning / error option.

+4
source

Take a look at the System.Diagnostics.Debug and System.Diagnostics.Trace classes.

+3
source

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.

+1
source

I use: System.Console.WriteLine ()

 #if DEBUG System.Console.WriteLine("Message"); #endif 
0
source

You can also check out the Aspect Oriented Programming solutions. They do it really well. Look at PostSharp as a good implementation. Their sample code usually contains an example of what you are looking for.

0
source

All Articles