I assume what you want is a stream that writes to standard out and to a file. That is, you want everything to still show up on the console, but you also want it to be written to a file. (If you just wanted to redirect output to a file, you could do that from the command line.)
You need to create your own TextWriter
-derived class, and have it do the actual output. You call Console.SetOut
to make the console write to your TextWriter
.
class ConsoleLogger: TextWriter
{
private TextWriter ConsoleOutputStream;
private StreamWriter LogOutputStream;
public ConsoleLogger(string logFilename)
{
ConsoleOutputStream = Console.Out;
// Initialize your log file
LogOutputStream = new StreamWriter(logFilename);
}
public void Write(string s)
{
ConsoleOutputStream.Write(s);
LogOutputStream.Write(s);
}
// Other Write functions format their arguments and call Write(string).
}
And in the program that uses this:
private ConsoleLogger MyLogger;
const string LogFilename = "ConsoleLog.txt";
void Main()
{
MyLogger = new ConsoleLogger(LogFilename);
Console.SetOut(MyLogger);
}
You don't have to override all the TextWriter
methods. As the documentation says, "A derived class must minimally implement the TextWriter.Write(Char)
method to make a useful instance of TextWriter
."
I've done something similar to this, and it works quite well.