Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Diagnostics.Debug.WriteLine in production code

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?

like image 201
David Avatar asked Oct 21 '09 13:10

David


People also ask

What is System Diagnostics debug WriteLine?

WriteLine(String) Writes a message followed by a line terminator to the trace listeners in the Listeners collection. WriteLine(Object) Writes the value of the object's ToString() method to the trace listeners in the Listeners collection.

Where does System diagnostics debug WriteLine go?

Diagnostics. Debug. WriteLine will display in the output window ( Ctrl + Alt + O ), you can also add a TraceListener to the Debug.

Where does System diagnostics trace WriteLine write to?

By default, the output is written to an instance of DefaultTraceListener. This method calls the WriteLine method of the trace listener.

How do I trace debugging?

You can enable debugging or tracing by adding a #define DEBUG or #define TRACE line to the top of your code or using the /d:DEBUG or /d:TRACE compiler switch when you compile.


2 Answers

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

like image 100
Phil Devaney Avatar answered Sep 23 '22 14:09

Phil Devaney


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.

like image 35
Mitch Wheat Avatar answered Sep 20 '22 14:09

Mitch Wheat