Skip to content

Instantly share code, notes, and snippets.

@plioi
Created October 21, 2013 19:17
Show Gist options
  • Save plioi/7089334 to your computer and use it in GitHub Desktop.
Save plioi/7089334 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();
}
}
@sirfreedom
Copy link

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.

@plioi
Copy link
Author

plioi commented Oct 30, 2022

Glad it was helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment