visual-studio 调试时将 Console.Write... 方法重定向到 Visual Studio 的输出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2518509/
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
Redirect Console.Write... Methods to Visual Studio's Output Window While Debugging
提问by AMissico
From a Console Applicationproject in Visual Studio, I want to redirect Console's output to the Output Windowwhile debugging.
在Visual Studio 中的控制台应用程序项目中,我想在调试时将 的输出重定向到输出窗口。Console
采纳答案by Alex F
Change application type to Windows before debugging. Without Console window, Console.WriteLine works like Trace.WriteLine. Don't forget to reset application back to Console type after debugging.
调试前将应用程序类型更改为 Windows。如果没有控制台窗口,Console.WriteLine 的工作方式与 Trace.WriteLine 类似。不要忘记在调试后将应用程序重置回控制台类型。
回答by dkackman
class DebugWriter : TextWriter
{
public override void WriteLine(string value)
{
Debug.WriteLine(value);
base.WriteLine(value);
}
public override void Write(string value)
{
Debug.Write(value);
base.Write(value);
}
public override Encoding Encoding
{
get { return Encoding.Unicode; }
}
}
class Program
{
static void Main(string[] args)
{
#if DEBUG
if (Debugger.IsAttached)
Console.SetOut(new DebugWriter());
#endif
Console.WriteLine("hi");
}
}
** note that this is roughed together almost pseudo code. it works but needs work :) **
** 请注意,这几乎是粗略的伪代码。它有效但需要工作:) **
回答by Niike2
You can change it to System.Diagnostics.Debug.Write();
您可以将其更改为 System.Diagnostics.Debug.Write();
回答by ivanatpr
Note if you're using dkackman's method but you want to write the output to BOTH the console window and the debug window, then you can slightly modify his code like this:
请注意,如果您正在使用 dkackman 的方法,但您想将输出写入控制台窗口和调试窗口,那么您可以像这样稍微修改他的代码:
class DebugWriter : TextWriter
{
//save static reference to stdOut
static TextWriter stdOut = Console.Out;
public override void WriteLine(string value)
{
Debug.WriteLine(value);
stdOut.WriteLine(value);
base.WriteLine(value);
}
public override void Write(string value)
{
Debug.Write(value);
stdOut.Write(value);
base.Write(value);
}
public override Encoding Encoding
{
get { return Encoding.Unicode; }
}
}
回答by mr NAE
Thanks, Alex F, nice solution, but didn't work for me, because my project was created by cmake. So, to do as Alex F suggested, add WIN32or MACOSX_BUNDLEto add_executable
谢谢,Alex F,不错的解决方案,但对我不起作用,因为我的项目是由 cmake 创建的。所以,按照 Alex F 的建议,添加WIN32或添加MACOSX_BUNDLE到add_executable
add_executable(target_name WIN32 <source list>)
Or, if you can't edit the CMakeList.txt, you can add -DCMAKE_WIN32_EXECUTABLE=1to the cmakeconfigure command.
或者,如果您无法编辑 CMakeList.txt,则可以添加-DCMAKE_WIN32_EXECUTABLE=1到cmake配置命令中。
回答by TobyEvans
Try Trace.Write and use DebugView
尝试 Trace.Write 并使用 DebugView
回答by Tidjani
Actually, there is an easiest way: In the "Options" window of Visual Studio (from the Tools menu), go to "Debugging" then check the option "Redirect All Output Window Text to the Immediate Window".
实际上,有一个最简单的方法:在 Visual Studio 的“选项”窗口(从“工具”菜单中),转到“调试”,然后选中“将所有输出窗口文本重定向到立即窗口”选项。

