2023-09-15 12:37:02 +03:30
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Brizco.Api.WebFramework.MiddleWares;
|
|
|
|
|
|
|
|
|
|
|
|
public static class PerformanceMiddlewareExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
public static IApplicationBuilder UsePerformanceMiddlewar(this IApplicationBuilder applicationBuilder)
|
|
|
|
|
|
{
|
|
|
|
|
|
return applicationBuilder.UseMiddleware<PerformanceMiddleware>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
public class PerformanceMiddleware(
|
|
|
|
|
|
RequestDelegate next,
|
|
|
|
|
|
ILogger<ExceptionHandlerMiddleware> logger)
|
2023-09-15 12:37:02 +03:30
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
private readonly Stopwatch _timer = new();
|
2023-09-15 12:37:02 +03:30
|
|
|
|
|
|
|
|
|
|
public async System.Threading.Tasks.Task Invoke(HttpContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
_timer.Start();
|
2024-08-09 21:42:04 +03:30
|
|
|
|
await next(context);
|
2023-09-15 12:37:02 +03:30
|
|
|
|
_timer.Stop();
|
|
|
|
|
|
|
|
|
|
|
|
var elapsedMilliseconds = _timer.ElapsedMilliseconds;
|
2024-08-09 21:42:04 +03:30
|
|
|
|
logger.LogWarning($"REQUEST TIMER : {elapsedMilliseconds}");
|
2023-09-15 12:37:02 +03:30
|
|
|
|
}
|
|
|
|
|
|
}
|