Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write text on console and file simultaneously

Guten Tag! The problem is: I have some code which prints some text messages via Console class on terminal (command line window). I need this info to be placed in two 'containers' - terminal and text file. Is there a way of adding output stream to Console class in order to make it output data not only on terminal? It'd be grate if I wont need to change existing code too much (there are a lot of places where Console.Write() and Console.WriteLine() are used).

like image 978
Illia Levandovskyi Avatar asked Nov 15 '25 18:11

Illia Levandovskyi


2 Answers

This is not a full implementation but it should be enough to get you started down the path you seek.

class Program
    {
        static void Main(string[] args)
        {
            DualOut.Init();
            Console.WriteLine("Hello");


            Console.ReadLine();
        }
    }

    public static class DualOut
    {
        private static TextWriter _current;

        private class OutputWriter : TextWriter
        {
            public override Encoding Encoding
            {
                get
                {
                    return _current.Encoding;
                }
            }

            public override void WriteLine(string value)
            {
                _current.WriteLine(value);
                File.WriteAllLines("Output.txt", new string[] { value });
            }
        }

        public static void Init()
        {
            _current = Console.Out;
            Console.SetOut(new OutputWriter());
        }
    }

If you run this code you will see that "Hello" is printed to both the console and to the file "Output.txt"

like image 54
iamkrillin Avatar answered Nov 18 '25 10:11

iamkrillin


I can't get the post by iamkrillin to work.

But the following does work:

class Program
{
    static void Main(string[] args)
    {
        ConsoleFileOutput cf = new ConsoleFileOutput("Output.txt", Console.Out);
        Console.SetOut(cf);

        Console.WriteLine("0ne");

        Console.WriteLine("two");

        Console.WriteLine("three");

        cf.Close();
    }

    public class ConsoleFileOutput : TextWriter
    {
        #region Fields  
        private Encoding encoding = Encoding.UTF8;
        private StreamWriter writer;
        private TextWriter console;
        #endregion

        #region Properties  
        public override Encoding Encoding
        {
            get
            {
                return encoding;
            }
        }
        #endregion

        #region Constructors  
        public ConsoleFileOutput(string filePath, TextWriter console, Encoding encoding = null)
        {
            if (encoding != null)
            {
                this.encoding = encoding;
            }
            this.console = console;
            this.writer = new StreamWriter(filePath, false, this.encoding);
            this.writer.AutoFlush = true;
        }
        #endregion

        #region Overrides  
        public override void Write(string value)
        {
            Console.SetOut(console);
            Console.Write(value);
            Console.SetOut(this);
            this.writer.Write(value);
        }

        public override void WriteLine(string value)
        {
            Console.SetOut(console);
            Console.WriteLine(value);
            this.writer.WriteLine(value);
            Console.SetOut(this);
        }

        public override void Flush()
        {
            this.writer.Flush();
        }

        public override void Close()
        {
            this.writer.Close();
        }
        #endregion

        #region IDisposable  
        new public void Dispose()
        {
            this.writer.Flush();
            this.writer.Close();
            this.writer.Dispose();
            base.Dispose();
        }

        #endregion
    }
}
like image 29
John Silletto Avatar answered Nov 18 '25 10:11

John Silletto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!