1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- 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<HttpRequest> _log;
- //private readonly ICurrentUserService _currentUserService;
- private readonly RequestDelegate _next;
- public PerformanceBehaviour(ILogger<HttpRequest> 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;
- }
- }
- }
|