简体   繁体   English

Debug.WriteLine不起作用

[英]Debug.WriteLine not working

In the past, perhaps versions of Visual Studio prior to the 2008 that I am using now, I would do something like this in my VB.NET code: 在过去,也许是我现在使用的2008之前的Visual Studio版本,我会在我的VB.NET代码中做这样的事情:

System.Diagnostics.Debug.WriteLine("Message")

..and the output would go to the output window. ..并且输出将转到输出窗口。

Now it doesn't. 现在它没有。 Something must first apparently be enabled. 首先必须显示某些东西。

If this involves "attaching a debugger", please explain how to do it. 如果这涉及“附加调试器”,请解释如何执行此操作。 It seems to me that it should just work without too much of a fuss. 在我看来,它应该没有太多的大惊小怪。

Here's a video explaining the issue in real time and showing you all my settings: 这是一个实时解释问题并向您显示我所有设置的视频:

http://screencast.com/t/YQnPb0mJcs http://screencast.com/t/YQnPb0mJcs

I'm using Visual Studio 2008. 我正在使用Visual Studio 2008。

Check to see if the "Redirect all Output Window text to the Immediate Window" is checked under Tools -> Options -> Debugging -> General. 检查“工具” - >“选项” - >“调试” - >“常规”下是否选中“将所有输出窗口文本重定向到立即窗口”。

Alternatively, you can use the Console.WriteLine() function as well. 或者,您也可以使用Console.WriteLine()函数。

All good suggestions. 所有好建议。 I noticed that I don't see this tip mentioned, so I'll say it: In your app.config file, make sure you don't have a <clear/> element in your trace listeners. 我注意到我没有看到这个提示,所以我会说: 在你的app.config文件中,确保你的跟踪监听器中没有<clear/>元素

You will effectively be clearing the list of trace listeners, including the default trace listener used for Debug statements. 您将有效地清除跟踪侦听器列表, 包括用于Debug语句的默认跟踪侦听器。

Here's what this would look like in your app.config file: 以下是app.config文件中的内容:

<system.diagnostics>
    <trace>
      <listeners>
          <!-- This next line is the troublemaker.  It looks so innocent -->
          <clear/>
      </listeners>
    </trace>
  </system.diagnostics>

If you want to have a placeholder for trace listeners in your app.config file, you should instead use something like this: 如果要在app.config文件中为跟踪侦听器设置占位符,则应使用以下内容:

<system.diagnostics>
    <trace>
      <listeners>
      </listeners>
    </trace>
  </system.diagnostics>

在输出窗口中单击鼠标右键,确保选中“程序输出”。

Do you definitely have the DEBUG constant defined? 你肯定定义了DEBUG常量吗? Check under project properties -> Compile -> Advanced Compile Options (there's a checkbox for the DEBUG constant. If it isn't checked, your Debug.XXX statements will not be executed). 检查项目属性 - > 编译 - > 高级编译选项 (DEBUG常量的复选框。如果未选中,则不会执行Debug.XXX语句)。

It should go to the output window if your app is compiled with the Debug configuration rather than the Release configuration. 如果使用Debug配置而不是Release配置编译应用程序,它应该转到输出窗口。 But instead of Debug.WriteLine() , try using Trace.WriteLine() (optionally with a ConsoleTraceListener attached). 但是,不要使用Debug.WriteLine() ,而是使用Trace.WriteLine() (可选择附加ConsoleTraceListener )。

Some extra ideas to try or check: 尝试或检查一些额外的想法:

  • Put a breakpoint before Debug.WriteLine and see what's in System.Diagnostics.Trace.Listeners collection. 在Debug.WriteLine之前放置一个断点,看看System.Diagnostics.Trace.Listeners集合中有什么。 You should see DefaultTraceListener . 您应该看到DefaultTraceListener If you don't see anything, then no one is listening and that's problem. 如果你没有看到任何东西,那么没有人在听,那就是问题。
  • Is it possible that the trace listeners being cleared/modified somewhere such as in config file or in the code? 是否有可能在配置文件或代码中的某处清除/修改跟踪侦听器?
  • Have you installed any package or add-in to Visual Studio? 您是否在Visual Studio中安装了任何包或加载项? or using a third-party library? 或使用第三方库?
  • Can you see debug messages outside of VS? 你能看到VS以外的调试消息吗? There is a SysInternals application called DebugView that monitors and shows debug output in your system. 有一个名为DebugView的SysInternals应用程序可监视并显示系统中的调试输出。 Run that tool and then run your application. 运行该工具,然后运行您的应用程序。 You should see your debug message in DebugView. 您应该在DebugView中看到您的调试消息。 At least you will know that your application is outputting debug messages but VS does not seem to be listening. 至少你会知道你的应用程序正在输出调试消息,但VS似乎没有正在监听。
  • Have you gone through the contents of the output window to see if there is any exception or error being reported. 您是否浏览了输出窗口的内容以查看是否报告了任何异常或错误。 Your debug output is not there but there might be somethings in there that can provide some clues. 你的调试输出不存在,但可能会有一些可以提供一些线索的东西。

Check your Immediate Window . 检查您的立即窗口 You might have all the output redirected to it. 您可能将所有输出重定向到它。

For me this was the fact that Debug.WriteLine shows in the Immediate window, not the Output. 对我来说,这是Debug.WriteLine在立即窗口中显示而不是输出的事实。 My installation of Visual Studio 2013 by default doesn't even show an option to open the Immediate window, so you have to do the following: 我默认安装Visual Studio 2013甚至没有显示打开立即窗口的选项,因此您必须执行以下操作:

Select Tools → Customize 
Commands Tab
View | Other Windows menu bar dropdown
Add Command...
The Immediate option is in the Debug section.

Once you have Ok'd that, you can go to menu ViewOther Windows and select the Immediate Window and hey presto all of the debug output can be seen. 一旦你有了Ok,那你就可以进入菜单ViewOther Windows并选择Immediate Window,然后就可以看到所有的调试输出。

Unfortunately for me it also showed about 50 errors that I wasn't aware of in my project... maybe I'll just turn it off again :-) 不幸的是,它还显示了我在项目中没有发现的大约50个错误...也许我会再次关闭它:-)

It happens. 它发生了。 I have the similar symptom when I am developing ASP.NET MVC applications on Visual Studio 2010 web developer Express Edition. 当我在Visual Studio 2010 Web开发人员Express Edition上开发ASP.NET MVC应用程序时,我有类似的症状。 The execution doesn't break at the breakpoint. 执行不会在断点处中断。 There is no output when it executes System.Diagnostic.Debug.Writeline (even though it runs with debug start), and there is nothing wrong with web.config . 执行System.Diagnostic.Debug.Writeline时没有输出(即使它在调试启动时运行), web.config没有任何问题。

My workaround is: - Goto project properties--> web - In the Debugger section, check the the ASP.NET option 我的解决方法是: - 转到项目属性 - > web - 在Debugger部分中,检查ASP.NET选项

Hope this helps someone who comes across this thread. 希望这可以帮助遇到这个帖子的人。

I had a similar issue with Visual Studio 2013 and MS unit testing. 我在Visual Studio 2013和MS单元测试中遇到了类似的问题。 Right clicking on a unit test method and selecting Run Tests any Debug.WriteLine calls would not show up in either the immediate window or the debug output window. 右键单击单元测试方法并选择Run Tests,任何Debug.WriteLine调用都不会显示在立即窗口或调试输出窗口中。 Even though the library I was testing and the unit test project itself both had DEBUG conditional checked in the build section of there project properties. 即使我正在测试的库和单元测试项目本身都在项目属性的构建部分中进行了DEBUG条件检查。

In order for the Debug.WriteLine statements to output anything I needed to run the unit tests by right clicking and selecting Debug Tests. 为了让Debug.WriteLine语句通过右键单击并选择Debug Tests来输出运行单元测试所需的任何内容。 Only then did I get the debug output being written to the debug output window. 只有这样我才能将调试输出写入调试输出窗口。

I was having the same problem for an ASP.NET application, and I found out that my Web.Config had the following line: 我对ASP.NET应用程序遇到了同样的问题,我发现我的Web.Config有以下几行:

<system.web>
    <trace enabled="false"/>
</system.web>

Just changing it to true, and I started seeing the Debug.WriteLine in Output window. 只需将其更改为true,我就开始在Output窗口中看到Debug.WriteLine

I had the same problem in Visual Studio Express 2010. I fresh installed on a debug machine and none of the suggestions worked. 我在Visual Studio Express 2010中遇到了同样的问题。我在调试机上安装了新的,没有任何建议有效。 I ended up using NLog and logging to a text file as a workaround. 我最终使用NLog并记录到文本文件作为解决方法。

Make sure you press F5 to Start Debugging mode (not Ctr + F5 ). 确保按F5键启动调试模式(不是Ctr + F5 )。

F5 Starting Debugging F5开始调试

CTRL + F5 Starting Without Debugging CTRL + F5无需调试即可启动

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

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