Skip to content

Instantly share code, notes, and snippets.

@audinue
Created June 7, 2017 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audinue/03a5ba9a06a651123fbdd50908139c1d to your computer and use it in GitHub Desktop.
Save audinue/03a5ba9a06a651123fbdd50908139c1d to your computer and use it in GitHub Desktop.
Captures console output.
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