System.Diagnostics.Debug.WriteLine in production code

I probably should know this already, but I'm not sure, and I don't see it documented.

I often use System.Diagnostics.Debug.WriteLine during the development process to track changes to variables or exceptions when I debug code. This is intended to develop and understand what happens faster only during development. I usually either comment on the code or delete it when I go to production.

I am wondering what will happen if I forget to comment on the code. Say, for example, that during the development cycle, I track error information that can register the connection binding to the output window using Debug.Write Line. Obviously, this is normal, but I think if I live, if there is a risk here. Can someone connect a debugger to my executable executable and catch this output? Or is this what only produces output in Visual Studio?

What about switching from debugging to release? Does this code mean compiler when compiling for release?

+56
debugging system.diagnostics
Oct 21 '09 at 13:55
source share
6 answers

All members of the Debug class are labeled ConditionalAttribute , so call sites will not be compiled into the Release assembly.

+65
Oct 21 '09 at 2:00
source share

System.Diagnostics.Debug method calls are present only when the conditional compilation symbol "DEBUG" is defined. By default, the symbol "DEBUG" is defined only for debug collections.

Compilers that support ConditionalAttribute ignore calls to these methods unless "DEBUG" is defined as a conditional compilation character.

+17
Oct 21 '09 at 13:59
source share

Since all Debug methods have the [Conditional ("DEBUG")] attribute, if you switch from Debug to Release, you don’t have to worry about this, since calls to these methods will be deleted (along with other Release build optimizations).

+11
Oct 21 '09 at 2:00
source share

Debug information is displayed only when working in Debug mode. In release mode, no debug statements will be visible (you can use Trace instead of Debug if you want these statements to be visible in release mode).

http://support.microsoft.com/kb/815788

+6
Oct 21 '09 at 2:06 p.m.
source share

By giving you compilation without the /d:DEBUG or #define DEBUG option, your WriteLine calls will not physically be present in your release code; there is no way for any third party to retrieve any information from these calls, as they are literally not present in the release.

Read more here: Debug Class (System.Diagnostics) on MSDN

+2
Oct 21 '09 at 14:03
source share

Almost all Debug members are marked with ConditionalAttribute. Compilers like C # will skip calls to these methods during release builds, so you are safe.

Information about the operating mode is here: http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.aspx

+1
Oct 21 '09 at 14:02
source share



All Articles