public async Task Invoke(HttpContext httpContext, ServiceMonitor monitor) { monitor.OnRequestStart(httpContext); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); try { await _next(httpContext); } catch (Exception ex) { monitor.OnRequestException(httpContext, ex); throw; } finally { stopwatch.Stop(); monitor.OnRequestStop(httpContext, stopwatch.Elapsed); } }