It is possible to replace System.Out with your own implementation of TextWriter.
One reason why you would want to do that is if you want to both capture all output to a stream and write it to console.
I was using this approach and I stumbled upon an issue when using TextRunner.
When using TextRunner, and not providing a file, TextRunner defaults to System.Out.
When TextRunner is done running it disposes the underlying stream that it was writing to. This is correct behaviour when using files, but not desired behaviour when using System.Out.
See: source code
I modified ConsoleColorWriter so Dispose is a NOP. (The base class disposes the writer provided by ConsoleColorWriter = System.Out).
I've also added a unit test to verify that the fix works. If you want a code example of what crashes, check the unit test.
It is possible to replace
System.Outwith your own implementation ofTextWriter.One reason why you would want to do that is if you want to both capture all output to a stream and write it to console.
I was using this approach and I stumbled upon an issue when using
TextRunner.When using
TextRunner, and not providing a file,TextRunnerdefaults toSystem.Out.When
TextRunneris done running it disposes the underlying stream that it was writing to. This is correct behaviour when using files, but not desired behaviour when usingSystem.Out.See: source code
I modified
ConsoleColorWritersoDisposeis a NOP. (The base class disposes the writer provided byConsoleColorWriter=System.Out).I've also added a unit test to verify that the fix works. If you want a code example of what crashes, check the unit test.