Debug.WriteLine() is not hit

青春壹個敷衍的年華 提交于 2019-11-30 20:27:52

问题


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

In Program.cs file:

static void Main() {

    if (!Environment.UserInteractive) {
        // We are not in debug mode, startup as service

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new MyServer() };
        ServiceBase.Run(ServicesToRun);
    } 
    else {
        // We are in debug mode, startup as application

        MyServer service = new MyServer();
        service.StartService();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }
}

And in MyServer.cs file:

public void StartService() {
    this.OnStart(new string[0]);
}

In the past, I used the Debug.WriteLine("xxx") line without any problem in all my service code, but now I noticed that all Debug.WriteLine() lines are not called anymore.

I clearly see that the debugger is jumping over these lines when I debug with Step Into (F11) - (thus, there is no output text on the output Window), whereas the service is correctly started in "debug mode".

I don't understand why the Debug code won't be called. Suggestions?

I build my solution in debug mode (Define DEBUG constant is checked), but I noticed that the code surrounded by #if DEBUG ... #endif is not called. That is weird...


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/7078508/debug-writeline-is-not-hit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!