I am debugging a Windows service (by hitting F5
in Visual Studio 2010) using the following code:
In Program.cs file:
Navigate to Project Properties -> Build and be sure that "Define DEBUG Constant" and "Define TRACE Constant" checkboxes are checked.
Environment.UserInteractive
is not the same thing as Debug mode -- that just means that you are running it in the console instead of as a service. Make sure that your build configuration is set to Debug.
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.
Seems the Debug symbols are missing.
Happened to me and this seemed to help.
I had the same random problem.
I was able to just fix it by:
Project Properties -> Build -> Define DEBUG constant
'Define DEBUG constant'
option againAfter doing this it hit the #if DEBUG
line as expected.