Skip to content

Instantly share code, notes, and snippets.

@trbngr
Created August 27, 2010 16:30
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 trbngr/553684 to your computer and use it in GitHub Desktop.
Save trbngr/553684 to your computer and use it in GitHub Desktop.
public class TestRunner
{
private static readonly ILog log = LogManager.GetLogger(typeof (TestRunner));
public static void Main()
{
Action foo = () =>
{
const string ID = "gsp";
log.InfoWithTime(() => "GSP".Equals(ID, StringComparison.InvariantCultureIgnoreCase), span => string.Format("equals check took {0} ms", span.Milliseconds));
};
Console.Out.WriteTimeLine(foo, elapsed => string.Format("foo took {0} ms.", elapsed.Milliseconds));
}
}
public static class Log4netExtensions
{
public static void InfoWithTime(this ILog log, Action action, Func<TimeSpan, string> formatter)
{
log.Info(formatter(action.Time()));
}
}
public static class TextWriterExtensions
{
public static void WriteTimeLine(this TextWriter writer, Action action, Func<TimeSpan, string> formatter)
{
writer.WriteLine(formatter(action.Time()));
}
}
public static class ActionExtensions
{
public static TimeSpan Time(this Action action)
{
var watch = new Stopwatch();
watch.Start();
action();
watch.Stop();
return watch.Elapsed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment