Where does Console.WriteLine go in Debug?

后端 未结 10 2121
后悔当初
后悔当初 2020-12-15 16:23

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 windo

相关标签:
10条回答
  • 2020-12-15 17:23

    Console.writeline() goes to a console window: the black command / dos prompt.

    0 讨论(0)
  • 2020-12-15 17:25

    I'll actually second James on this one.

    http://www.csharp411.com/console-output-from-winforms-application

    describes it in gross detail (if directing output to a file is enough though then you could easily use amissico's method). Most of the methods they describe mimic those described in http://dslweb.nwnexus.com/~ast/dload/guicon.htm

    Changing your project to a "console" project would have a similar effect, as mentioned. Cheers!

    0 讨论(0)
  • 2020-12-15 17:28

    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-15 17:30

    Debug and Release do not control whether or not you get a console window. That is controlled by the project's output type. (Properties -> Application -> Output Type). Console Application will get you a console window which will visualize and receive input from the window into the Error, In, and Out streams in System.Console.

    The System.Console class exposes several properties and methods for interacting with its streams even if you cannot see it. Most notably: Error, In, Out, SetError(), SetIn(), SetOut(), and the Read and Write methods.

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