using MediatR; using Microsoft.Extensions.Logging; namespace Application.Common.Behaviours; public class UnhandledExceptionBehaviour : IPipelineBehavior where TRequest : IRequest { private readonly ILogger logger; // ReSharper disable once ContextualLoggerProblem public UnhandledExceptionBehaviour(ILogger logger) { this.logger = logger; } public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { try { return await next(); } catch (Exception ex) { var requestName = typeof(TRequest).Name; logger.LogError(ex, "Unhandled Exception for Request {Name} {@Request}", requestName, request); throw; } } }