16

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Quinn Wilson
  • 7,995
  • 1
  • 22
  • 31
  • After much effort, I found the [question with straight answer](https://stackoverflow.com/questions/2542599/having-the-output-of-a-console-application-in-visual-studio-instead-of-the-conso/). In the plain view. Whew. Here is the answer by [stolsvic](https://stackoverflow.com/users/39334/stolsvik): _Options Dialog -> Debugging -> Check the "Redirect All Output Window Text to the Immediate Window"_. The project should be of "Windows Application" type rather than "Console Application" – Alex Fainshtein May 15 '21 at 20:09

3 Answers3

28

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());
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Avram
  • 4,267
  • 33
  • 40
5

@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);
        }
    }
}
Community
  • 1
  • 1
Carl G
  • 17,394
  • 14
  • 91
  • 115
1

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.

Alex Angas
  • 59,219
  • 41
  • 137
  • 210