1
0
mirror of synced 2024-12-01 01:07:18 +01:00
GC-local-server-rewrite/Application/Common/Behaviours/UnhandledExceptionBehaviour.cs

33 lines
899 B
C#
Raw Normal View History

2023-02-09 10:25:42 +01:00
using MediatR;
using Microsoft.Extensions.Logging;
namespace Application.Common.Behaviours;
public class UnhandledExceptionBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly ILogger<TRequest> logger;
// ReSharper disable once ContextualLoggerProblem
public UnhandledExceptionBehaviour(ILogger<TRequest> logger)
{
this.logger = logger;
}
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> 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;
}
}
}