Debug.WriteLine() is not hit

后端 未结 5 1930
时光取名叫无心
时光取名叫无心 2021-01-07 09:01

I am debugging a Windows service (by hitting F5 in Visual Studio 2010) using the following code:

In Program.cs file:

         


        
相关标签:
5条回答
  • 2021-01-07 09:17

    Navigate to Project Properties -> Build and be sure that "Define DEBUG Constant" and "Define TRACE Constant" checkboxes are checked.

    0 讨论(0)
  • 2021-01-07 09:24

    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.

    0 讨论(0)
  • 2021-01-07 09:25

    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();
    

    EDIT

    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.

    0 讨论(0)
  • 2021-01-07 09:27

    Seems the Debug symbols are missing.

    1. Clean your solution, restart Visual Studio, rebuild the solution.
    2. Make sure the Configuration Manager is in Debug mode.
    3. Check in Tools > Options > Debugging and Uncheck "Enable Just My Code"

    Happened to me and this seemed to help.

    0 讨论(0)
  • 2021-01-07 09:37

    I had the same random problem.

    I was able to just fix it by:

    1. Uncheck the Project Properties -> Build -> Define DEBUG constant
    2. Click Save
    3. Check 'Define DEBUG constant' option again
    4. Click Save
    5. Rebuild the Project
    6. Run

    After doing this it hit the #if DEBUG line as expected.

    0 讨论(0)
提交回复
热议问题