I am debugging a Windows service (by hitting F5
in Visual Studio 2010) using the following code:
In Program.cs file:
If you aren't using a debug build, the code is excluded by the compiler. The signature of the method has something similar to:
[Conditional("DEBUG")]
public static void WriteLine(string message) { }
That attribute tells the compiler to only include calls to the method in question, when you're doing debug builds. Therefore, if you aren't compiling with the DEBUG flag set (say, a release build) then the compiler drops the call altogether.
Same is true of Debug.Assert
.
You can use this attribute in your own code too, with any label you like.
Another way of thinking about this attribute is that it forces all calls to the method having the attribute to be wrapped in directives -- something like:
DoSomething();
#if DEBUG
Debug.WriteLine("I'm a debug build");
#endif
DoSomethingElse();
What is it you are trying to achieve via Environment.UserInteractive
? Perhaps you mean Debugger.IsAttached
? If you're wanting to check if it's a debug build, then you can use the ConditionalAttribute
as above, or #if #endif
directives.