Capture console output for debugging in VS?

后端 未结 6 471
说谎
说谎 2020-12-06 17:20

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.

The question is:

相关标签:
6条回答
  • 2020-12-06 17:43

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

    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.

    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:

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-06 17:44

    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:

    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

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

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

    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.

    0 讨论(0)
  • 2020-12-06 17:51

    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?

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

    0 讨论(0)
  • 2020-12-06 17:56

    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.

    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.
    0 讨论(0)
  • 2020-12-06 17:58

    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.

    To my knowledge, you cannot direct printf output to the output window in dev studio, not directly anyway. [emphasis added by 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

    1. call printf and print the string
    2. call OuputDebugString() to print the string to the output window

    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()

    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. :-)

    0 讨论(0)
  • 2020-12-06 18:04

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

    0 讨论(0)
提交回复
热议问题