1
0
mirror of synced 2024-12-20 02:05:52 +01:00
GC-local-server-rewrite/Application/Common/Behaviours/UnhandledExceptionBehaviour.cs
2023-02-09 17:25:42 +08:00

33 lines
899 B
C#

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;
}
}
}