-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestLoggingPipelineBehavior.cs
More file actions
79 lines (65 loc) · 3.36 KB
/
Copy pathRequestLoggingPipelineBehavior.cs
File metadata and controls
79 lines (65 loc) · 3.36 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Text.Json;
using Microsoft.Extensions.Logging;
using Vulthil.Results;
using Vulthil.SharedKernel.Application.Messaging;
using Vulthil.SharedKernel.Application.Pipeline;
using Vulthil.SharedKernel.Events;
namespace Vulthil.SharedKernel.Application.Behaviors;
internal static partial class LoggingBehaviors
{
internal sealed partial class RequestLoggingPipelineBehavior<TRequest, TResponse>(
ILogger<RequestLoggingPipelineBehavior<TRequest, TResponse>> logger)
: IPipelineHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TResponse : Result
{
private static readonly string _requestName = typeof(TRequest).Name;
private readonly ILogger<RequestLoggingPipelineBehavior<TRequest, TResponse>> _logger = logger;
/// <inheritdoc />
public async Task<TResponse> HandleAsync(TRequest request, PipelineDelegate<TResponse> next, CancellationToken cancellationToken = default)
{
LogProcessingRequest(_logger, _requestName);
var result = await next(cancellationToken);
if (result.IsSuccess)
{
LogCompletedRequest(_logger, _requestName);
}
else
{
using (_logger.BeginScope(new Dictionary<string, string>
{
["Error"] = JsonSerializer.Serialize(result.Error, result.Error.GetType())
}))
{
LogCompletedRequestWithError(_logger, _requestName);
}
}
return result;
}
[LoggerMessage(Level = LogLevel.Information, Message = "Processing request {RequestName}")]
private static partial void LogProcessingRequest(ILogger logger, string requestName);
[LoggerMessage(Level = LogLevel.Information, Message = "Completed request {RequestName}")]
private static partial void LogCompletedRequest(ILogger logger, string requestName);
[LoggerMessage(Level = LogLevel.Information, Message = "Completed request {RequestName} with error")]
private static partial void LogCompletedRequestWithError(ILogger logger, string requestName);
}
internal sealed partial class DomainEventLoggingPipelineBehavior<TDomainEvent>(
ILogger<DomainEventLoggingPipelineBehavior<TDomainEvent>> logger)
: IDomainEventPipelineHandler<TDomainEvent>
where TDomainEvent : IDomainEvent
{
private static readonly string _domainEventName = typeof(TDomainEvent).Name;
private readonly ILogger<DomainEventLoggingPipelineBehavior<TDomainEvent>> _logger = logger;
/// <inheritdoc />
public async Task HandleAsync(TDomainEvent domainEvent, DomainEventPipelineDelegate next, CancellationToken cancellationToken = default)
{
LogProcessingEvent(_logger, _domainEventName);
await next(cancellationToken);
LogCompletedEvent(_logger, _domainEventName);
}
[LoggerMessage(Level = LogLevel.Information, Message = "Processing event {DomainEventName}")]
private static partial void LogProcessingEvent(ILogger logger, string domainEventName);
[LoggerMessage(Level = LogLevel.Information, Message = "Completed processing event {DomainEventName}")]
private static partial void LogCompletedEvent(ILogger logger, string domainEventName);
}
}