System.Diagnostics.Debug.WriteLine in production code

.NetDebuggingsystem.diagnostics

.Net Problem Overview


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

I use System.Diagnostics.Debug.WriteLine quite often during the development process to be able to track changes to variables or exceptions as I'm debugging the code. This is meant to make development and understanding what's happening easier only during development. I normally either comment out the code or delete it when I go to production.

I'm wondering what happens if I forget to comment the code out. Say, for example, that during the development cycle, I'm tracking error information that may log a connection sting to the output window using Debug.Write Line. This is obviously OK while developing, but I'm wondering if when I go live, if there is a risk here. Can someone attach a debugger to my live executable and trap this output? Or is it something that only produces output in Visual Studio?

And what about when we switch from debug to release? Does this code get ignored by the compiler if we compile for release?

.Net Solutions


Solution 1 - .Net

All the members in the Debug class are marked with ConditionalAttribute, so the call sites won't be compiled into a Release build.

Solution 2 - .Net

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

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

Solution 3 - .Net

Since the Debug methods all have the [Conditional("DEBUG")] attribute on them, if you switch from Debug to Release you will have not have to worry about it as the calls to those methods will be removed (along with the other optimizations of a Release build).

Solution 4 - .Net

Debug information is only visible when you're running 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

Solution 5 - .Net

Providing you compile without the /d:DEBUG option or #define DEBUG, your WriteLine calls will not physically present in your release code; there is no way for any third party to recover any information from these calls, as they literally not there in the release version.

More details here: Debug Class (System.Diagnostics) on MSDN

Solution 6 - .Net

Almost all members of Debug are marked with ConditionalAttribute. Such compilers as C# will skip calls to those methods during Release build, so you are on the safe side.

Mode info here: http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.aspx

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - .NetPhil DevaneyView Answer on Stackoverflow
Solution 2 - .NetMitch WheatView Answer on Stackoverflow
Solution 3 - .NetAgent_9191View Answer on Stackoverflow
Solution 4 - .NetMarcView Answer on Stackoverflow
Solution 5 - .NetColin PickardView Answer on Stackoverflow
Solution 6 - .NetVitaliy LiptchinskyView Answer on Stackoverflow