12345678910111213141516171819202122232425262728293031323334353637383940 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Logging;
- using System.Diagnostics;
- using System.Threading.Tasks;
- namespace EVCB_OCPP.WEBAPI.Middleware;
- public class PerformaceMonitorMiddlewareOptions
- {
- public string Label { get; set; }
- }
- public class PerformaceMonitorMiddleware
- {
- private readonly RequestDelegate _next;
- private readonly PerformaceMonitorMiddlewareOptions options;
- private readonly ILogger<PerformaceMonitorMiddleware> logger;
- public PerformaceMonitorMiddleware(
- RequestDelegate next,
- PerformaceMonitorMiddlewareOptions options,
- ILogger<PerformaceMonitorMiddleware> logger)
- {
- _next = next;
- this.options = options;
- this.logger = logger;
- }
- public async Task Invoke(HttpContext context)
- {
- var timer = Stopwatch.StartNew();
- await _next(context);
- timer.Stop();
- if (timer.ElapsedMilliseconds / 1000 > 1)
- {
- logger.LogCritical("{0} {1} {2} cost {3}", options.Label, context.Request.Method, context.Request.Path, timer.ElapsedMilliseconds);
- }
- }
- }
|