This repository was archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathLoggingBehavior.cs
More file actions
45 lines (38 loc) · 1.36 KB
/
LoggingBehavior.cs
File metadata and controls
45 lines (38 loc) · 1.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
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.Logging;
namespace OpenCodeFoundation.ESchool.Services.Attending.API.Application.Behaviors
{
public class LoggingBehavior<TRequest, TResponse>
: IPipelineBehavior<TRequest, TResponse>
where TRequest : notnull
{
private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;
public LoggingBehavior(
ILogger<LoggingBehavior<TRequest, TResponse>> logger)
{
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
}
public async Task<TResponse> Handle(
TRequest request,
CancellationToken cancellationToken,
RequestHandlerDelegate<TResponse> next)
{
if (next is null)
{
throw new System.ArgumentNullException(nameof(next));
}
_logger.LogInformation(
"Handling request {RequestName} ({@Request})",
request.GetType().Name,
request);
var response = await next().ConfigureAwait(false);
_logger.LogInformation(
"Request {RequestName} handled. Response: {@Response}",
request.GetType().Name,
response);
return response;
}
}
}