前言
使用 IPipelineBehavior
,介绍如何在业务层实现记录请求日志,用于跟踪每个请求执行的耗时。
Demo
创建 ASP.NET Core Web API
项目,引用 Nuget 包:
1
2
|
MediatR
MediatR.Extensions.Microsoft.DependencyInjection
|
1.实现 IPipelineBehavior
创建 LoggingBehavior
,用于实现记录请求日志逻辑:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;
public LoggingBehavior(ILogger<LoggingBehavior<TRequest, TResponse>> logger)
{
_logger = logger;
}
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var response = await next();
stopwatch.Stop();
_logger.LogInformation($"{typeof(TRequest).Name}耗时:{stopwatch.ElapsedMilliseconds}");
return response;
}
}
|
2.注册 IPipelineBehavior
修改 Startup.cs:
1
2
|
services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehaviour<,>))
|
3.测试
修改 WeatherForecastController
,使用 Mediator
:
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
|
public class WeatherForecastController : ControllerBase
{
private readonly IMediator _mediator;
public WeatherForecastController(IMediator mediator)
{
this._mediator = mediator;
}
[HttpGet]
public async Task<IEnumerable<WeatherForecast>> Get()
{
return await this._mediator.Send(new GetWeatherForecastQuery());
}
}
public class GetWeatherForecastQuery : IRequest<IEnumerable<WeatherForecast>>
{
}
internal class GetWeatherForecastQueryHandler : IRequestHandler<GetWeatherForecastQuery, IEnumerable<WeatherForecast>>
{
public async Task<IEnumerable<WeatherForecast>> Handle(GetWeatherForecastQuery request, CancellationToken cancellationToken)
{
await Task.Delay(1000);
var rng = new Random();
return Enumerable.Range(1, 1).Select(index => new WeatherForecast
{
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
|
为了体现效果,代码里故意加了等待时间。
运行程序,在控制台界面可以看到输出的日志。
参考
https://mp.weixin.qq.com/s?__biz=MzU3MjUzNjc1Ng==&mid=2247487227&idx=1&sn=b11a78bbbad2d085aaa38cb51db8c1eb&chksm=fcce2c20cbb9a53606fb30846c677d97df41712106e4c901baeb17729bc66be6715ce34c1eda&scene=178&cur_album_id=1986653731890724865#rd