-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathErrorLoggingBehavior.cs
More file actions
31 lines (27 loc) · 922 Bytes
/
Copy pathErrorLoggingBehavior.cs
File metadata and controls
31 lines (27 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
namespace MainCore.Behaviors
{
public sealed class ErrorLoggingBehavior<TRequest, TResponse>
: Behavior<TRequest, TResponse>
where TRequest : ICommand
where TResponse : IResultBase
{
private readonly ILogger _logger;
public ErrorLoggingBehavior(ILogger logger)
{
_logger = logger;
}
public override async ValueTask<TResponse> HandleAsync(TRequest request, CancellationToken cancellationToken)
{
var response = await Next(request, cancellationToken);
if (response.IsFailed)
{
var message = string.Join(Environment.NewLine, response.Reasons.Select(e => e.Message));
if (!string.IsNullOrEmpty(message))
{
_logger.Warning("{Message}", message);
}
}
return response;
}
}
}