<p>For years I have been using the DEBUG compiler constant in VB.NET to write messages to the console.  I've also been using System.Diagnostics.Debug.Write in similar fashion.  It was always my understanding that when RELEASE was used as the build option, that all of these statements were left out by the compiler, freeing your production code of the overhead of debug statements.  Recently when working with Silverlight 2 Beta 2, I noticed that Visual Studio actually attached to a RELEASE build that I was running off of a public website and displayed DEBUG statements which I assumed weren't even compiled!  Now, my first inclination is to assume that that there is something wrong with my environment, but I also want to ask anyone with deep knowledge on System.Diagnostics.Debug and the DEBUG build option in general what I may be misunderstanding here.</p>

[{'answer_id': 28907, 'author': 'Vincent', 'author_id': 1508, 'author_profile': 'https://Stackoverflow.com/users/1508', 'pm_score': -1, 'selected': False, 'text': '<p>In my experience choosing between Debug and Release in VB.NET makes no difference. You may add custom actions to both configuration, but by default I think they are the same.</p>\n\n<p>Using Release will certainly not remove the System.Diagnostics.Debug.Write statements.</p>\n'}, {'answer_id': 28913, 'author': 'juan', 'author_id': 1782, 'author_profile': 'https://Stackoverflow.com/users/1782', 'pm_score': 1, 'selected': False, 'text': '<p>What I do is encapsulate the call to Debug in my own class and add a precompiler directive</p>\n\n<pre><code>public void Debug(string s)\n{\n#if DEBUG\n    System.Diagnostics.Debug(...);\n#endif\n}\n</code></pre>\n'}, {'answer_id': 28934, 'author': 'Chris Karcher', 'author_id': 2773, 'author_profile': 'https://Stackoverflow.com/users/2773', 'pm_score': 1, 'selected': False, 'text': '<p>Using the DEBUG compiler symbol will, like you said, actually omit the code from the assembly.</p>\n\n<p>I believe that System.Diagnostics.Debug.Write will always output to an attached debugger, even if you\'ve built in Release mode.  Per the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.write.aspx" rel="nofollow noreferrer">MSDN article</a>:</p>\n\n<blockquote>\n  <p>Writes information about the debug to the trace listeners in the Listeners collection. </p>\n</blockquote>\n\n<p>If you don\'t want <em>any</em> output, you\'ll need to wrap your call to Debug.Write with the DEBUG constant like Juan said:</p>\n\n<pre><code>#if DEBUG\n    System.Diagnostics.Debug.Write(...);\n#endif\n</code></pre>\n'}, {'answer_id': 28947, 'author': 'Mike', 'author_id': 2848, 'author_profile': 'https://Stackoverflow.com/users/2848', 'pm_score': 6, 'selected': True, 'text': '<p>The preferred method is to actually use the conditional attribute to wrap your debug calls, not use the compiler directives.  #ifs can get tricky and can lead to weird build problems.</p>\n\n<p>An example of using a conditional attribute is as follows (in C#, but works in VB.NET too):</p>\n\n<pre><code>[ Conditional("Debug") ]\nprivate void WriteDebug(string debugString)\n{\n  // do stuff\n}\n</code></pre>\n\n<p>When you compile without the DEBUG flag set, any call to WriteDebug will be removed as was assumed was happening with Debug.Write().</p>\n'}, {'answer_id': 28962, 'author': 'Pete', 'author_id': 3059, 'author_profile': 'https://Stackoverflow.com/users/3059', 'pm_score': 2, 'selected': False, 'text': '<p>I read the article too, and it led me to believe that when DEBUG was not defined, that the ConditionalAttribute declared on System.Debug functions would cause the compiler to leave out this code completely.  I assume the same thing to be true for TRACE.  That is, the System.Diagnostics.Debug functions must have ConditionalAttributes for DEBUG and for TRACE.  I was wrong in that assumption.  The separate Trace class has the same functions, and these define ConditionalAttribute dependent on the TRACE constant.</p>\n\n<p>From System.Diagnostics.Debug:\n _\nPublic Shared Sub Write ( _\n    message As String _\n)</p>\n\n<p>From System.Diagnostics.Trace:\n _\nPublic Shared Sub WriteLine ( _\n    message As String _\n)</p>\n\n<p>It seems then that my original assumption was correct, that System.Diagnostics.Debug (or system.Diagnostics.Trace) statements are actually not included in compilation as if they were included in #IF DEBUG (or #IF TRACE) regions.  </p>\n\n<p>But I\'ve also learned here from you guys, and verified, that the RELEASE build does not in itself take care of this.  At least with Silverlight projects, which are still a little flaky, you need to get into the "Advanced Compile Options..." and make sure DEBUG is not defined.  </p>\n\n<p>We jumped from .NET 1.1/VS2003 to .NET 3.5/VS2008 so I think some of this used to work differently, but perhaps it changed in 2.0/VS2005.</p>\n'}, {'answer_id': 29021, 'author': 'Bjorn Reppen', 'author_id': 1324220, 'author_profile': 'https://Stackoverflow.com/users/1324220', 'pm_score': 3, 'selected': False, 'text': '<p>Examine the <a href="http://msdn.microsoft.com/en-us/library/54ffa64k.aspx" rel="noreferrer">Debug.Write</a> method.  It is marked with the </p>\n\n<pre><code>[Conditional("DEBUG")]\n</code></pre>\n\n<p>attribute.</p>\n\n<p>The MSDN help for <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx" rel="noreferrer">ConditionalAttribute</a> states:</p>\n\n<blockquote>\n  <p>Indicates to compilers that a method\n  call or attribute should be ignored \n  unless a specified <strong>conditional\n  compilation symbol is defined</strong>.</p>\n</blockquote>\n\n<p>Whether the build configuration has a label of release or debug doesn\'t matter, what matters is whether the DEBUG symbol is defined in it.</p>\n'}, {'answer_id': 2455663, 'author': 'Elad A', 'author_id': 294880, 'author_profile': 'https://Stackoverflow.com/users/294880', 'pm_score': 1, 'selected': False, 'text': '<p>To select whether you want the debug information to be compiled or to be removed,</p>\n\n<p>enter the "Build" tab in the project\'s properties window.</p>\n\n<p>Choose the right configuration (Active/Release/Debug/All) and make sure you \ncheck the "DEBUG Constant" if you want the info, \nor uncheck it if you don\'t.</p>\n\n<p>Apply changes and rebuild</p>\n'}]
['https://Stackoverflow.com/questions/28894', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3059/']