简体   繁体   中英

Log user input in a c# console application as well as console out

I am currently trying to figure out, what´s the best way to log the user input as well as the output generated by the console. My current approach is using:

class ConsoleCopy : IDisposable
    {

        FileStream fileStream;
        StreamWriter fileWriter;
        TextWriter doubleWriter;
        TextWriter oldOut;

        class DoubleWriter : TextWriter
        {

            TextWriter one;
            TextWriter two;

            public DoubleWriter(TextWriter one, TextWriter two)
            {
                this.one = one;
                this.two = two;
            }

            public override Encoding Encoding
            {
                get { return one.Encoding; }
            }

            public override void Flush()
            {
                one.Flush();
                two.Flush();
            }

            public override void Write(char value)
            {
                one.Write(value);
                two.Write(value);
            }

        }

        public ConsoleCopy(string path)
        {
            oldOut = Console.Out;

            try
            {
                fileStream = File.Create(path);

                fileWriter = new StreamWriter(fileStream);
                fileWriter.AutoFlush = true;

                doubleWriter = new DoubleWriter(fileWriter, oldOut);
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot open file for writing");
                Console.WriteLine(e.Message);
                return;
            }
            Console.SetOut(doubleWriter);
        }

        public void Dispose()
        {
            Console.SetOut(oldOut);
            if (fileWriter != null)
            {
                fileWriter.Flush();
                fileWriter.Close();
                fileWriter = null;
            }
            if (fileStream != null)
            {
                fileStream.Close();
                fileStream = null;
            }
        }

    }

and

using (var cc = new ConsoleCopy(log))
{
    //some code
    Conole.WriteLine(string);
    Console.WriteLine(string2);
    string input = Console.ReadLine();

}

This approach is working fine for the console output, but I still can not figure out, how to add the user input. I am redirecting the console output to the file while also being able to see it in the console. That is what I want to achieve with the uesr input aswell.

Thanks a lot!

The same way, actually.

Note that this class only implements ReadLine() for now:

class LoggingReader : TextReader
{
    TextReader old;
    TextWriter log;

    public LoggingReader(TextReader old, TextWriter log)
    {
        this.old = old;
        this.log = log;
    }

    public override string ReadLine()
    {
        string input = old.ReadLine();
        log.Write("> {0}\r\n", input);
        return input;
    }
}

These are added to ConsoleCopy as members:

TextReader oldIn;
TextReader loggingReader;

Here is the new ConsoleCopy constructor:

public ConsoleCopy(string path)
{
    oldOut = Console.Out;
    oldIn = Console.In; // ADDED

    try
    {
        fileStream = File.Create(path);

        fileWriter = new StreamWriter(fileStream);
        fileWriter.AutoFlush = true;

        doubleWriter = new DoubleWriter(fileWriter, oldOut);
        loggingReader = new LoggingReader(oldIn, fileWriter); // ADDED
    }
    catch (Exception e)
    {
        Console.WriteLine("Cannot open file for writing");
        Console.WriteLine(e.Message);
        return;
    }
    Console.SetOut(doubleWriter);
    Console.SetIn(loggingReader); // ADDED
}

The log file contents:

This is output 1
This is output 2
> Test
Test command received
Enter to exit
> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM