哪里Console.WriteLine去调试?Console、WriteLine

2023-09-03 00:03:53 作者:佯装着自己不痛不痒

我发现这个问题的,但我想知道不同的是 - 不从Console.WriteLine输出到任何地方调试时?我知道它去输出窗口,我应该要的Debug.WriteLine()或其他方法,但如果不标准的Console.WriteLine()去了?

I found this question, but what I want to know is different - does the output from Console.WriteLine go anywhere when debugging? I know that for it to go to the output window I should should Debug.WriteLine() or other methods, but where does the standard Console.WriteLine() go?

修改 调试时,看不到黑色的控制台窗口/测试日志 - 因此在真正的问题是我怎么能访问/查看在调试这个输出

Edit When debugging, you don't see the black console window / test log - so the real question is how can I access/view this output during debugging?

推荐答案

控制台可以重定向它的输出到任何的TextWriter。如果您实现的TextWriter写入到Diagnostics.Debug,你都设置。

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

下面是一个TextWriter的写入调试器。

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);
        }
    }
}

由于它采用Diagnostics.Debug它会坚持你的编译器设置,还判定是否应该写任何输出与否。此输出还可以看出在的Sysinternals DebugView中

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.

下面是你如何使用它:

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.");
        }
    }
}

如果你想看到的Sysinternals DebugView中,当你在编制发布模式输出,可以使用的TextWriter写入到OutputDebugString的API。它可能是这样的:

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:

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);
        }
    }
}