-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathPerformaceBehavior.cs
37 lines (28 loc) · 939 Bytes
/
PerformaceBehavior.cs
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
using System.Diagnostics;
using Mediator;
using Serilog;
namespace Application.Common.Behaviors;
public class PerformaceBehavior<TMessage, TResponse>(ILogger logger)
: IPipelineBehavior<TMessage, TResponse>
where TMessage : notnull, IMessage
{
private readonly Stopwatch timer = new();
public async ValueTask<TResponse> Handle(
TMessage message,
MessageHandlerDelegate<TMessage, TResponse> next,
CancellationToken cancellationToken
)
{
timer.Start();
TResponse response = await next(message, cancellationToken);
timer.Stop();
long elapsedMilliseconds = timer.ElapsedMilliseconds;
string requestName = typeof(TMessage).Name;
logger.Information(
"\n\nProcessing {Name} request in ({ElapsedMilliseconds} milliseconds)\n\n",
requestName,
elapsedMilliseconds
);
return response;
}
}