C# 捕获控制台输出以在 VS 中进行调试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/122778/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 14:35:45  来源:igfitidea点击:

Capture console output for debugging in VS?

提问by BCS

Under VS's external tools settings there is a "Use Output Window" check box that captures the tools command line output and dumps it to a VS tab.

在 VS 的外部工具设置下,有一个“使用输出窗口”复选框,用于捕获工具命令行输出并将其转储到 VS 选项卡。

The question is: can I get the same processing for my program when I hit F5?

问题是:当我按 F5 时,我的程序可以得到相同的处理吗?

Edit:FWIW I'm in C# but if that makes a difference to your answer then it's unlikely that your answer is what I'm looking for.

编辑:FWIW 我在 C# 中,但如果这对你的答案有影响,那么你的答案不太可能是我正在寻找的。

What I want would take the output stream of the program and transfer it to the output tab in VS using the same devices that output redirection ('|' and '>') uses in the cmd prompt.

我想要的是获取程序的输出流并使用与输出重定向('|' 和'>')在 cmd 提示符中使用的相同设备将其传输到 VS 中的输出选项卡。

采纳答案by Mark

I'm going to make a few assumptions here. First, I presume that you are talking about printf output from an application (whether it be from a console app or from a windows GUI app). My second assumption is the C language.

我将在这里做一些假设。首先,我假设您正在谈论应用程序的 printf 输出(无论是来自控制台应用程序还是来自 Windows GUI 应用程序)。我的第二个假设是 C 语言。

To my knowledge, you cannot direct printf output to the output window in dev studio, not directly anyway.[emphasis added by OP]

据我所知,您不能将 printf 输出定向到 dev studio 中的输出窗口,无论如何都不能直接输出。[强调由 OP 添加]

There might be a way but I'm not aware of it. One thing that you could do though would be to direct printf function calls to your own routine which will

可能有办法,但我不知道。您可以做的一件事是将 printf 函数调用定向到您自己的例程,这将

  1. call printf and print the string
  2. call OuputDebugString() to print the string to the output window
  1. 调用 printf 并打印字符串
  2. 调用 OutputDebugString() 将字符串打印到输出窗口

You could do several things to accomplish this goal. First would be to write your own printf function and then call printf and the OuputDebugString()

你可以做几件事来实现这个目标。首先是编写自己的 printf 函数,然后调用 printf 和 OuputDebugString()

void my_printf(const char *format, ...)
{
    char buf[2048];

    // get the arg list and format it into a string
    va_start(arglist, format);
    vsprintf_s(buf, 2048, format, arglist);
    va_end(arglist); 

    vprintf_s(buf);            // prints to the standard output stream
    OutputDebugString(buf);    // prints to the output window
}

The code above is mostly untested, but it should get the concepts across.

上面的代码大多未经测试,但它应该可以理解概念。

If you are not doing this in C/C++, then this method won't work for you. :-)

如果您不是在 C/C++ 中执行此操作,则此方法对您不起作用。:-)

回答by ScaleOvenStove

System.Diagnostics.Debug.Writeline() or Trace.Writeline()

System.Diagnostics.Debug.Writeline() 或 Trace.Writeline()

回答by cori

You can use Systems.Diagnostics.Trace class to write your output to the Output window instead of (or in addition to) the console. It take a little configuration, but it works. Is that along the line of what you want?

您可以使用 Systems.Diagnostics.Trace 类将您的输出写入输出窗口而不是(或除了)控制台。它需要一些配置,但它可以工作。这符合你想要的吗?

You can also add your own tab per this article, but I've never tried it.

您还可以根据本文添加自己的选项卡,但我从未尝试过。

回答by Jay Bazuzi

You should be able to capture the output in a text file and use that.

您应该能够在文本文件中捕获输出并使用它。

I don't have a VS handy, so this is from memory:

我手头没有 VS,所以这是凭记忆:

  1. Create a C++ project
  2. Open the project settings, debugging tab
  3. Enable managed debugging
  4. Edit command line to add "> output.txt"
  5. Run your program under the debugger
  1. 创建 C++ 项目
  2. 打开项目设置,调试选项卡
  3. 启用托管调试
  4. 编辑命令行添加“ > output.txt
  5. 在调试器下运行你的程序

If things work the way I remember, this will redirect STDOUT to a file, even though you're not actually running under CMD.EXE.

如果事情按照我记得的方式工作,这会将 STDOUT 重定向到一个文件,即使您实际上并未在 CMD.EXE 下运行。

(The debugger has its own implementation of redirection syntax, which is not 100% the same as cmd, but it's pretty good.)

(调试器有自己的重定向语法实现,与 cmd 不是 100% 相同,但它非常好。)

Now, if you open this file in VS, you can still see the output from within VS, although not in exactly the same window you were hoping for.

现在,如果您在 VS 中打开此文件,您仍然可以在 VS 中看到输出,尽管与您希望的窗口不同。

回答by Jay Bazuzi

Maybe this will work for you: set a breakpoint on the close }in Main, and then look at the console window before it closes. You can even copy the text out of it if you need to.

也许这对你有用:在 close }in上设置一个断点Main,然后在它关闭之前查看控制台窗口。如果需要,您甚至可以从中复制文本。

On every machine that I use for development, I configure my console window in a certain way, which happens to make this approach work better:

在我用于开发的每台机器上,我以某种方式配置我的控制台窗口,这恰好使这种方法更好地工作:

  1. Run cmd.exe
  2. ALT-SPACE, D
  3. In Options, enable QuickEdit mode.
  4. In Layout, set Buffer Height to 9999
  5. Click OK
  6. Exit the CMD window.
  1. 运行cmd.exe
  2. ALT-空格,D
  3. 在选项中,启用快速编辑模式。
  4. 在 Layout 中,将 Buffer Height 设置为 9999
  5. 单击确定
  6. 退出 CMD 窗口。

回答by Carl R

The console can redirect it's output to any textwriter. If you implement a textwriter that writes to Diagnostics.Debug, you are all set.

控制台可以将其输出重定向到任何文本编写器。如果您实现了一个写入 Diagnostics.Debug 的文本编写器,则一切就绪。

Here's a textwriter that writes to the debugger.

这是一个写入调试器的文本编写器。

using System.Diagnostics;
using System.IO;
using System.Text;

namespace TestConsole
{
    public class DebugTextWriter : TextWriter
    {
        public override Encoding Encoding
        {
            get { return Encoding.UTF8; }
        }

        //Required
        public override void Write(char value)
        {
            Debug.Write(value);
        }

        //Added for efficiency
        public override void Write(string value)
        {
            Debug.Write(value);
        }

        //Added for efficiency
        public override void WriteLine(string value)
        {
            Debug.WriteLine(value);
        }
    }
}

Since it uses Diagnostics.Debug it will adhere to your compiler settings to wether it should write any output or not. This output can also be seen in Sysinternals DebugView.

由于它使用 Diagnostics.Debug,它将遵守您的编译器设置,以决定是否应该写入任何输出。此输出也可以在 Sysinternals DebugView 中看到。

Here's how you use it:

以下是您如何使用它:

using System;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.SetOut(new DebugTextWriter());
            Console.WriteLine("This text goes to the Visual Studio output window.");
        }
    }
}

If you want to see the output in Sysinternals DebugView when you are compiling in Release mode, you can use a TextWriter that writes to the OutputDebugString API. It could look like this:

如果要在发布模式下编译时在 Sysinternals DebugView 中看到输出,可以使用写入 OutputDebugString API 的 TextWriter。它可能看起来像这样:

using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace TestConsole
{
    public class OutputDebugStringTextWriter : TextWriter
    {
        [DllImport("kernel32.dll")]
        static extern void OutputDebugString(string lpOutputString);

        public override Encoding Encoding
        {
            get { return Encoding.UTF8; }
        }

        //Required
        public override void Write(char value)
        {
            OutputDebugString(value.ToString());
        }

        //Added for efficiency
        public override void Write(string value)
        {
            OutputDebugString(value);
        }

        //Added for efficiency
        public override void WriteLine(string value)
        {
            OutputDebugString(value);
        }
    }
}