Skip to content

Instantly share code, notes, and snippets.

@davidalpert
Forked from plioi/RedirectedConsole.cs
Last active August 29, 2015 14:18
Show Gist options
  • Save davidalpert/efe3c64dbb82d59e951c to your computer and use it in GitHub Desktop.
Save davidalpert/efe3c64dbb82d59e951c to your computer and use it in GitHub Desktop.
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