using System.Diagnostics; using MediatR; using Microsoft.Extensions.Logging; namespace BuildingBlocks.Logging; public class LoggingBehavior : IPipelineBehavior where TRequest : notnull, IRequest where TResponse : notnull { private readonly ILogger> _logger; public LoggingBehavior(ILogger> logger) { _logger = logger; } public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { const string prefix = nameof(LoggingBehavior); _logger.LogInformation("[{Prefix}] Handle request={X-RequestData} and response={X-ResponseData}", prefix, typeof(TRequest).Name, typeof(TResponse).Name); var timer = new Stopwatch(); timer.Start(); var response = await next(); timer.Stop(); var timeTaken = timer.Elapsed; if (timeTaken.Seconds > 3) // if the request is greater than 3 seconds, then log the warnings _logger.LogWarning("[{Perf-Possible}] The request {X-RequestData} took {TimeTaken} seconds.", prefix, typeof(TRequest).Name, timeTaken.Seconds); _logger.LogInformation("[{Prefix}] Handled {X-RequestData}", prefix, typeof(TRequest).Name); return response; } }