Created
June 7, 2017 18:53
-
-
Save audinue/03a5ba9a06a651123fbdd50908139c1d to your computer and use it in GitHub Desktop.
Captures console output.
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
using System; | |
using System.IO; | |
partial class Util { | |
/// <summary> | |
/// Captures console output. | |
/// </summary> | |
public static string Capture(Action action) { | |
var before = Console.Out; | |
using (var after = new StringWriter()) { | |
Console.SetOut(after); | |
try { | |
action(); | |
} finally { | |
Console.SetOut(before); | |
} | |
return after.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment