简体   繁体   中英

Debug.WriteLine() is not hit

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...

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.

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM