using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using System.Diagnostics; namespace AipGateway.API.Application.Pipeline.Middlewares.Behaviours { public class PerformanceBehaviour { private readonly Stopwatch _timer; private readonly ILogger _log; //private readonly ICurrentUserService _currentUserService; private readonly RequestDelegate _next; public PerformanceBehaviour(ILogger logger, RequestDelegate next) { _timer = new Stopwatch(); //_currentUserService = currentUserService; _log = logger; _next = next; _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: PerformanceBehaviour"); } public async Task InvokeAsync(HttpContext context) { try { _timer.Start(); //_log.LogError("PerformanceBehaviour Invoke Before:"); await _next.Invoke(context); _timer.Stop(); //_log.LogError("PerformanceBehaviour Invoke After:"); var elapsedMilliseconds = _timer.ElapsedMilliseconds; if (elapsedMilliseconds > 500) { //var requestName = context.Request.Path; //var requestedPath = $"{context.Request.Scheme}://{context.Request.Host}{context.Request.Path} {context.Request.QueryString}".Trim(); //var userName = "Swagger"; //if (!string.IsNullOrEmpty(_currentUserService.Email)) //{ // userName = _currentUserService.Email; //} //_log.LogWarning("API Long Running Request: {Name} ({ElapsedMilliseconds} milliseconds) {@RequestedPath} {@UserName}", // requestName, elapsedMilliseconds, requestedPath, userName); } _log.LogError("PerformanceBehaviour........................................................................."); } catch (Exception) { throw; } await Task.FromResult(Task.CompletedTask); return; } } }