将控制台重定向到 app.config 中的 Visual Studio 调试输出窗口

发布于 2024-08-04 08:37:49 字数 259 浏览 1 评论 0 原文

我想让我的 Console.WriteLine() 命令与我的 Debug.WriteLine() 语句一起出现在我的“输出”窗口中。我想我曾经弄清楚如何做到这一点,但我不记得/在谷歌上找到如何再次做到这一点。我似乎记得能够在 app.config 文件中执行此操作。

我找到了大量关于如何使控制台和调试语句出现在控制台输出中的说明,但没有找到如何使它们出现在“输出”窗口中的说明。

是否可以?

I want to get my Console.WriteLine() commands to appear in my "Output" window with my Debug.WriteLine() statements. I think I figured out how to do this once, but I can't remember / find on google how to do it again. I seem to remember being able to do this in the app.config file.

I find plenty of instructions on how to get console and debug statements to appear in the output of the Console, but not how to get them to appear in the "Output" window.

Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

向日葵 2024-08-11 08:37:49

基本上最简单的解决方案如下所示。

public class ToDebugWriter : StringWriter
{
    public override void WriteLine(string value)
    {
        Debug.WriteLine(value);
        base.WriteLine(value);
    }
}

并且您必须将此行添加到程序的初始化中:

Console.SetOut(new ToDebugWriter());

Basically the most simple solution looks like this.

public class ToDebugWriter : StringWriter
{
    public override void WriteLine(string value)
    {
        Debug.WriteLine(value);
        base.WriteLine(value);
    }
}

And you must add this line to the initialization of the program:

Console.SetOut(new ToDebugWriter());
半枫 2024-08-11 08:37:49

@Avram 的答案 对我有用,只是他的代码中的单个重载不是 log4net 的 的重载ConsoleAppender 在我的系统上使用。 (我对 Console.SetOut 感兴趣,以便 log4net 的 ConsoleAppender 输出到 Visual Studio 的“调试”输出窗格。)因此我覆盖了所有 StringWriter > 的 WriteWriteLine 方法接受 stringobjectchar[]等等,假设其中一个或多个是 ConsoleAppender 通过 Console 调用的。

这成功了,log4net 日志记录现在出现在我的“调试”窗格中。

我添加了下面的代码,以便任何具有类似目标的人受益。 (为了完全安全,可以重写其余的 StringWriter.Write.WriteLine 方法。)我删除了对 base 的调用,因为它们似乎是不必要的,我认为它们只是在 StringWriter 内部构建了一个大缓冲区(通常通过该类的 .ToString() 访问。)

namespace GeneralLibrary.Logging
{
    using System.Diagnostics;
    using System.IO;

    public class DebugWriter : StringWriter
    {
        public override void Write(string format, object arg0)
        {
            Debug.Write(string.Format(format, arg0));
        }

        public override void Write(string format, object arg0, object arg1)
        {
            Debug.Write(string.Format(format, arg0, arg1));
        }

        public override void Write(string format, object arg0, object arg1, object arg2)
        {
            Debug.Write(string.Format(format, arg0, arg1, arg2));
        }

        public override void Write(string format, params object[] arg)
        {
            Debug.Write(string.Format(format, arg));
        }

        public override void Write(object value)
        {
            Debug.Write(value);
        }

        public override void Write(string value)
        {
            Debug.Write(value);
        }

        public override void Write(char[] buffer)
        {
            Debug.Write(buffer);
        }

        public override void Write(char[] buffer, int index, int count)
        {
            Debug.Write(new string(buffer, index, count));
        }

        public override void WriteLine(string value)
        {
            Debug.WriteLine(value);
        }

        public override void WriteLine(object value)
        {
            Debug.WriteLine(value);
        }

        public override void WriteLine(string format, object arg0)
        {
            Debug.WriteLine(format, arg0);
        }

        public override void WriteLine(string format, object arg0, object arg1)
        {
            Debug.WriteLine(format, arg0, arg1);
        }

        public override void WriteLine(string format, object arg0, object arg1, object arg2)
        {
            Debug.WriteLine(format, arg0, arg1, arg2);
        }

        public override void WriteLine(string format, params object[] arg)
        {
            Debug.WriteLine(format, arg);
        }

        public override void WriteLine(char[] buffer)
        {
            Debug.WriteLine(buffer);
        }

        public override void WriteLine(char[] buffer, int index, int count)
        {
            Debug.WriteLine(new string(buffer, index, count));
        }

        public override void WriteLine()
        {
            Debug.WriteLine(string.Empty);
        }
    }
}

@Avram's answer has worked for me, except that the single overload in his code wasn't the one that log4net's ConsoleAppender was using on my system. (I'm interested in Console.SetOut so that log4net's ConsoleAppender outputs to Visual Studio's "Debug" output pane.) So I overrode all of StringWriter's Write and WriteLine methods accepting string, object, char[], etc. on the assumption that one or more of these was what ConsoleAppender was calling via Console.

This succeeded, and log4net logging now appears in my "Debug" pane.

I'm including the code below for the benefit of anyone with similar goals. (To be entirely safe, one could override the remaining StringWriter.Write and .WriteLine methods.) I've removed the calls to base because they appear to be unnecessary, and I think they just build up a large buffer inside StringWriter (usually accessed via that class's .ToString().)

namespace GeneralLibrary.Logging
{
    using System.Diagnostics;
    using System.IO;

    public class DebugWriter : StringWriter
    {
        public override void Write(string format, object arg0)
        {
            Debug.Write(string.Format(format, arg0));
        }

        public override void Write(string format, object arg0, object arg1)
        {
            Debug.Write(string.Format(format, arg0, arg1));
        }

        public override void Write(string format, object arg0, object arg1, object arg2)
        {
            Debug.Write(string.Format(format, arg0, arg1, arg2));
        }

        public override void Write(string format, params object[] arg)
        {
            Debug.Write(string.Format(format, arg));
        }

        public override void Write(object value)
        {
            Debug.Write(value);
        }

        public override void Write(string value)
        {
            Debug.Write(value);
        }

        public override void Write(char[] buffer)
        {
            Debug.Write(buffer);
        }

        public override void Write(char[] buffer, int index, int count)
        {
            Debug.Write(new string(buffer, index, count));
        }

        public override void WriteLine(string value)
        {
            Debug.WriteLine(value);
        }

        public override void WriteLine(object value)
        {
            Debug.WriteLine(value);
        }

        public override void WriteLine(string format, object arg0)
        {
            Debug.WriteLine(format, arg0);
        }

        public override void WriteLine(string format, object arg0, object arg1)
        {
            Debug.WriteLine(format, arg0, arg1);
        }

        public override void WriteLine(string format, object arg0, object arg1, object arg2)
        {
            Debug.WriteLine(format, arg0, arg1, arg2);
        }

        public override void WriteLine(string format, params object[] arg)
        {
            Debug.WriteLine(format, arg);
        }

        public override void WriteLine(char[] buffer)
        {
            Debug.WriteLine(buffer);
        }

        public override void WriteLine(char[] buffer, int index, int count)
        {
            Debug.WriteLine(new string(buffer, index, count));
        }

        public override void WriteLine()
        {
            Debug.WriteLine(string.Empty);
        }
    }
}
寒尘 2024-08-11 08:37:49

如果您可以获取输出窗口的流,则可以使用 Console.SetOut() 重定向到它。然而这种方法似乎不可能。

System.Debug 输出到每个 TraceListener 位于其 TraceListenerCollection。最初只注册了一个 TraceListener,即 DefaultTraceListener。它不使用流对象,而是使用本机方法进行输出。

使用 Visual Studio API 的方法可能是最佳选择。

If you can get hold of the stream for the output window you can use Console.SetOut() to redirect to it. However this approach doesn't appear to be possible.

System.Debug outputs to every TraceListener in its TraceListenerCollection. There is only one TraceListener registered initially which is the DefaultTraceListener. It does not make use of a stream object and instead uses native methods for output.

An approach that uses the Visual Studio API is probably the way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文