C# Debug.WriteLine 什么也没显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9369820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debug.WriteLine shows nothing
提问by Zurechtweiser
When using
使用时
using System.Diagnostics;
and
和
Debug.WriteLine("Test");
having run the application, no "Test" can be seen in output. But if I use the msgbox function instead, the msgbox pops up, so the line is reached.
运行应用程序后,在输出中看不到“测试”。但是,如果我改用 msgbox 函数,则会弹出 msgbox,因此到达该行。
Am I looking in the wrong window or what do I have to change?
我是在看错窗口还是我需要改变什么?
I am using VC# Express.
我正在使用 VC# Express。
采纳答案by JaredPar
There are two likely causes for this behavior
这种行为有两个可能的原因
- The application is being compiled in Release mode and the
Debug.WriteLinecall is not in the final program - There is no trace listener in the program and hence nothnig to output the message
- 应用程序正在以 Release 模式编译并且
Debug.WriteLine调用不在最终程序中 - 程序中没有跟踪监听器,因此没有输出消息
The easiest way to diagnose this is to change the code to
诊断此问题的最简单方法是将代码更改为
#if DEBUG
Console.WriteLine("the message");
#endif
If it prints then you have an issue with the trace listeners, else you're compiling in Release
如果它打印,那么您的跟踪侦听器有问题,否则您将在 Release 中进行编译
回答by Sofian Hnaide
Debug.WriteLine("Test");should be showing in the output window when you are in the debug mode. If you want to debug an application running (Release mode) you can use Trace and that would show in Windows events.
Debug.WriteLine("Test");当您处于调试模式时,应显示在输出窗口中。如果您想调试正在运行的应用程序(发布模式),您可以使用 Trace,这将显示在 Windows 事件中。
回答by Zensar
I believe "Debug.WriteLine()" writes to the Listenerscollection. From there you can determine where the debug information will be written. By default "Output" should be where it appears, but if you are having trouble viewing the information then create a different listener to grab the debug info.
我相信“Debug.WriteLine()”会写入Listeners集合。从那里您可以确定将写入调试信息的位置。默认情况下,“输出”应该是它出现的地方,但如果您在查看信息时遇到问题,则创建一个不同的侦听器来获取调试信息。
Here is the MSDN example:
这是 MSDN 示例:
TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(myWriter);
回答by Morten Jensen
I'm not sure anyone has mentioned this reason, but if I compile in Debug-mode and then just run the program (Ctrl + F5) instead of choosing Start Debugging (F5), I won't see the Debug.WriteLineeither.
我不确定是否有人提到过这个原因,但是如果我在调试模式下编译然后只运行程序(Ctrl + F5)而不是选择开始调试(F5),我也不会看到Debug.WriteLine。
So it isn't enough to simply compile in Debug mode, you also have to actively Debug the program and not just run it :)
因此,仅仅在 Debug 模式下编译是不够的,您还必须主动调试程序,而不仅仅是运行它:)
回答by Guillermo Hernandez
On Menu > tools > options > debugging > General:
在菜单 > 工具 > 选项 > 调试 > 常规上:
- Ensure "Redirect all output window text to the immediate window" is NOT checked
- 确保未选中“将所有输出窗口文本重定向到即时窗口”
On Project Properties > Build:
在项目属性 > 构建上:
- Configuration: Debug
- "Define DEBUG constant" is checked
- "Define TRACE constant" is checked
- 配置:调试
- 选中“定义调试常量”
- 选中“定义跟踪常量”
On the Output window:
在输出窗口中:
- Show output from: Debug
- Right-click in the output window and ensure "Program output" is checked
- 显示输出:调试
- 右键单击输出窗口并确保选中“程序输出”
回答by Serge Voloshenko
回答by Mark Ball
For anyone googling: Whilst there's various answers that point to removal of the listeners in the config file also watch out for
对于任何使用谷歌搜索的人:虽然有各种答案指向删除配置文件中的侦听器,但也要注意
<remove name="OPTIONSVerbHandler" />
in the section
在该部分
<handlers>
As this also suppresses debug output.
因为这也会抑制调试输出。
回答by thebenman
In case any of the others answer do not work. Try to place a break point in Debug.WriteLineline and see if it is being hit.
万一其他答案不起作用。尝试在Debug.WriteLine直线上放置一个断点,看看它是否被击中。
In case it is not being hit the reason is because an old version of the code is being executed.
如果它没有被击中,原因是因为正在执行旧版本的代码。
To fix this, first check this
要解决这个问题,首先检查这个
Go to Tools-Options
Under Projects and solution -> Build and Run select "Always build" under "On Run, when projects are out of date"
转到工具-选项
在项目和解决方案 -> 构建和运行下,选择“运行时,项目过期时”下的“始终构建”
And Check if the Temporary files are cleaned out. Check this SO Question
并检查临时文件是否已清除。检查这个问题
This worked for me.
这对我有用。
If even this fails try rebooting VS. Works most of the time.
如果连这都失败了,请尝试重新启动 VS。大部分时间都在工作。


