Skip to content

Instantly share code, notes, and snippets.

@JuergenGutsch
Last active November 23, 2022 21:13
Show Gist options
  • Save JuergenGutsch/a711371d3f9577c01eb97db8110bc7c9 to your computer and use it in GitHub Desktop.
Save JuergenGutsch/a711371d3f9577c01eb97db8110bc7c9 to your computer and use it in GitHub Desktop.
public class StopwatchMiddleWare
{
private readonly RequestDelegate _next;
public StopwatchMiddleWare(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var s = new Stopwatch();
s.Start();
await _next(context);
s.Stop();
var result = s.ElapsedMilliseconds;
await context.Response.WriteAsync($" Time needed: {result }");
}
}
public static class StopwatchMiddlewareExtension
{
public static IApplicationBuilder UseStopwatch(this IApplicationBuilder app)
{
app.UseMiddleware<StopwatchMiddleware>();
return app;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment