Created
August 27, 2010 16:30
-
-
Save trbngr/553684 to your computer and use it in GitHub Desktop.
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
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