简体   繁体   English

Debug.WriteLine() 未命中

[英]Debug.WriteLine() is not hit

I am debugging a Windows service (by hitting F5 in Visual Studio 2010) using the following code:我正在使用以下代码调试 Windows 服务(通过在 Visual Studio 2010 中按F5 ):

In Program.cs file:Program.cs文件中:

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:MyServer.cs文件中:

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.过去,我在所有服务代码中使用Debug.WriteLine("xxx")行没有任何问题,但现在我注意到不再调用所有Debug.WriteLine()行。

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" .当我使用Step Into ( F11 ) 进行调试时,我清楚地看到调试器正在跳过这些行 - (因此,output 窗口上没有 output 文本),而服务在“调试模式”下正确启动。

I don't understand why the Debug code won't be called.我不明白为什么不会调用Debug代码。 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.我在调试模式下构建我的解决方案(检查了Define DEBUG constant ),但我注意到没有调用#if DEBUG... #endif包围的代码。 That is weird...这很奇怪……

Seems the Debug symbols are missing.似乎缺少调试符号。

  1. Clean your solution, restart Visual Studio, rebuild the solution.清理您的解决方案,重新启动 Visual Studio,重新构建解决方案。
  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取消选中Project Properties -> Build -> Define DEBUG constant
  2. Click Save点击保存
  3. Check 'Define DEBUG constant' option again再次检查'Define DEBUG constant'选项
  4. Click Save点击保存
  5. Rebuild the Project重建项目
  6. Run

After doing this it hit the #if DEBUG line as expected.这样做之后,它按预期命中了#if 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.因此,如果您没有使用 DEBUG 标志集进行编译(例如,发布版本),那么编译器将完全放弃调用。

Same is true of Debug.Assert . Debug.Assert也是如此。

You can use this attribute in your own code too, with any label you like.您也可以在自己的代码中使用此属性,以及您喜欢的任何 label。

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 ?你想通过Environment.UserInteractive实现什么? Perhaps you mean Debugger.IsAttached ?也许您的意思是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.如果您想检查它是否是调试版本,那么您可以使用上面的ConditionalAttribute#if #endif指令。

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. Environment.UserInteractive与调试模式不同——这只是意味着您在控制台中运行它而不是作为服务运行。 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.导航到 Project Properties -> Build 并确保选中“Define DEBUG Constant”和“Define TRACE Constant”复选框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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