Skip to content

Instantly share code, notes, and snippets.

@khellang
Created July 3, 2014 13:33
Show Gist options
  • Save khellang/fa6a94abed6b791d997e to your computer and use it in GitHub Desktop.
Save khellang/fa6a94abed6b791d997e to your computer and use it in GitHub Desktop.
public static MidFunc Tracing(ITracer tracer)
{
if (tracer == null) throw new ArgumentNullException("tracer");
tracer = new SafeTracer(tracer);
return next => async env =>
{
if (!tracer.IsEnabled)
{
await next(env);
}
else
{
tracer.Trace("Request Start");
var stopWatch = Stopwatch.StartNew();
await next(env);
stopWatch.Stop();
if (tracer.Filters.All(filter => filter.Invoke(env)))
{
foreach (var item in env)
{
tracer.Trace(item.Key, item.Value);
}
}
tracer.Trace(string.Format("Request completed in {0} ms", stopWatch.ElapsedMilliseconds));
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment