using System.Diagnostics;

namespace DocuMed.Api.WebFramework.MiddleWares;

public static class PerformanceMiddlewareExtensions
{
    public static IApplicationBuilder UsePerformanceMiddlewar(this IApplicationBuilder applicationBuilder)
    {
        return applicationBuilder.UseMiddleware<PerformanceMiddleware>();
    }
}

public class PerformanceMiddleware(
    RequestDelegate next,
    ILogger<ExceptionHandlerMiddleware> logger)
{
    private readonly Stopwatch _timer = new();

    public async System.Threading.Tasks.Task Invoke(HttpContext context)
    {
        _timer.Start();
        await next(context);
        _timer.Stop();

        var elapsedMilliseconds = _timer.ElapsedMilliseconds;
        logger.LogWarning($"REQUEST TIMER : {elapsedMilliseconds}");
    }
}