[英]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.我目前正试图弄清楚,记录用户输入以及控制台生成的 output 的最佳方式是什么。 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.这种方法适用于控制台 output,但我仍然不知道如何添加用户输入。 I am redirecting the console output to the file while also being able to see it in the console.我将控制台 output 重定向到该文件,同时也能够在控制台中看到它。 That is what I want to achieve with the uesr input aswell.这也是我想通过 uesr 输入实现的目标。
Thanks a lot!非常感谢!
The same way, actually.实际上,同样的方式。
Note that this class only implements ReadLine() for now:请注意,此 class 目前仅实现 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;
}
}
These are added to ConsoleCopy as members:这些作为成员添加到 ConsoleCopy:
TextReader oldIn;
TextReader loggingReader;
Here is the new ConsoleCopy constructor:这是新的 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
}
The log file contents:日志文件内容:
This is output 1
This is output 2
> Test
Test command received
Enter to exit
>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.