Точно так же, на самом деле.
Обратите внимание, что этот класс сейчас только реализует ReadLine ():
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;
}
}
Они добавляются в ConsoleCopy как члены:
TextReader oldIn;
TextReader loggingReader;
Вот новый конструктор ConsoleCopy:
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
}
Содержимое файла журнала:
This is output 1
This is output 2
> Test
Test command received
Enter to exit
>