Created
October 21, 2013 19:17
-
-
Save plioi/7089334 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RedirectedConsole : IDisposable | |
{ | |
readonly TextWriter outBefore; | |
readonly TextWriter errBefore; | |
readonly StringWriter console; | |
public RedirectedConsole() | |
{ | |
console = new StringWriter(); | |
outBefore = Console.Out; | |
errBefore = Console.Error; | |
Console.SetOut(console); | |
Console.SetError(console); | |
} | |
public string Output | |
{ | |
get { return console.ToString(); } | |
} | |
public void Dispose() | |
{ | |
Console.SetOut(outBefore); | |
Console.SetError(errBefore); | |
console.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Amazing code man..
I have a lot of void methods and I needed to test the content of the console.write on events.
Thank u so much.