Is there a way to continuously reflect the result of Console.Write into the collection (array, list, etc.)?

I have a console program, I would like to constantly reflect the result of Console.Write into a collection that I can look at the tail in real time. A collection can be an array, list, etc.

I assume that I will have to use some kind of event handler?

I do not mind pointing towards a third-party library, for example. Nlog

Update

I need to store a collection in memory that reflects the current state of the console (then I can send the remote WinForms application using sockets). Details aside, I think I can do this with a few lines of C # - I don’t want to add a huge log library without needing it.

+5
source share
7 answers

The Console class allows you to replace output and error streams. Just what you need here, you can replace them with a TextWriter, which also writes what is written. Implementation Example:

    class ConsoleLogger : System.IO.TextWriter {
        private System.IO.TextWriter oldOut;
        private Queue<string> log = new Queue<string>();
        private StringBuilder line = new StringBuilder();
        private object locker = new object();
        private int newline;
        private int logmax;

        public ConsoleLogger(int history) {
            logmax = history;
            oldOut = Console.Out;
            Console.SetOut(this);
        }
        public override Encoding Encoding {
            get { return oldOut.Encoding; }
        }
        public override void Write(char value) {
            oldOut.Write(value);
            lock (locker) {
                if (value == '\r') newline++;
                else if (value == '\n') {
                    log.Enqueue(line.ToString());
                    if (log.Count > logmax) log.Dequeue();
                    line.Length = newline = 0;
                }
                else {
                    for (; newline > 0; newline--) line.Append('\r');
                    line.Append(value);
                }
            }
        }
    }

Using:

    static void Main(string[] args) {
        var outLogger = new ConsoleLogger(100);
        // etc...
    }
+5
source

Perhaps you could write a new TextWriterone that Writecauses the list to populate instead of writing to the stream. Then you need to install this viaConsole.SetOut(...)

+2
source

Console.SetOut, TextWriter.

+2

You can create a simple helper method and call it instead of Console.Write.

private void WriteToConsole(string message)
{
    myList.Add(message);

    Console.Write(message);
}
+1
source

If you want to write code like:

    WriteToConsole("{0} tel is: {1:+### ### ### ###}", "Name", 242352356578);

Then you may have the code:

    private static Queue<String> q = new Queue<String>(1000);
    private static void WriteToConsole(String message)
    {
        q.Enqueue(message);
        Console.Write(message);
    }
    private static void WriteToConsole(String message, params Object[] r)
    {
        String s = String.Format(message, r);
        q.Enqueue(s);
        Console.Write(s);
    }
+1
source

Use log4net for a continuous dump in the file, and then use this for the tail: http://tailf.codeplex.com/

0
source

All Articles