I’d like to redirect my process console output to file, while mantaining the utility of displaying it on the curretnly attached console.
The redirection is done easily with:
using (FileStream fs = new FileStream(TestLogName("Texture"), FileMode.Create, FileAccess.Write)) {
Console.SetOut(new StreamWriter(fs));
...
Console.Out.Flush();
}
But how I can achieve the same result while mantaining the console output on already attached console (de-facto duplicating the stream)?
You can sub-class TextWriter with a class that writes to a file and another TextWriter class. Then initialize an instance of this class with the current value of System.Console.Out.
SplitWriter then takes responsibility for writing to both the file and the original value of Console.Out.