33using Bing . Tracing ;
44using Bing . Users ;
55using Microsoft . AspNetCore . Http ;
6+ using Microsoft . Extensions . DependencyInjection ;
67using Microsoft . Extensions . Options ;
78using Serilog . Context ;
89using Serilog . Core ;
@@ -15,74 +16,95 @@ namespace Bing.AspNetCore.Serilog;
1516/// </summary>
1617public class BingSerilogMiddleware : IMiddleware , ITransientDependency
1718{
18- /// <summary>
19- /// 当前客户端
20- /// </summary>
21- private readonly ICurrentClient _currentClient ;
22-
23- /// <summary>
24- /// 当前用户
25- /// </summary>
26- private readonly ICurrentUser _currentUser ;
27-
28- /// <summary>
29- /// 跟踪标识提供程序
30- /// </summary>
31- private readonly ICorrelationIdProvider _correlationIdProvider ;
32-
3319 /// <summary>
3420 /// Serilog 选项配置
3521 /// </summary>
3622 private readonly BingAspNetCoreSerilogOptions _options ;
3723
3824 /// <summary>
39- /// 方法
25+ /// 下一个中间件
4026 /// </summary>
4127 private readonly RequestDelegate _next ;
4228
4329 /// <summary>
4430 /// 初始化一个<see cref="BingSerilogMiddleware"/>类型的实例
4531 /// </summary>
4632 /// <param name="next">方法</param>
47- /// <param name="currentUser">当前用户</param>
48- /// <param name="currentClient">当前客户端</param>
49- /// <param name="correlationIdProvider">跟踪标识提供程序</param>
5033 /// <param name="options">Serilog 选项配置</param>
5134 public BingSerilogMiddleware (
5235 RequestDelegate next ,
53- ICurrentUser currentUser ,
54- ICurrentClient currentClient ,
55- ICorrelationIdProvider correlationIdProvider ,
5636 IOptions < BingAspNetCoreSerilogOptions > options )
5737 {
5838 _next = next ;
59- _currentUser = currentUser ;
60- _currentClient = currentClient ;
61- _correlationIdProvider = correlationIdProvider ;
6239 _options = options . Value ;
6340 }
6441
6542 /// <summary>
6643 /// 执行中间件拦截逻辑
6744 /// </summary>
68- /// <param name="context">Http上下文 </param>
45+ /// <param name="context">当前 HTTP 请求上下文 </param>
6946 public async Task InvokeAsync ( HttpContext context )
7047 {
7148 var enrichers = new List < ILogEventEnricher > ( ) ;
72- if ( _currentUser ? . TenantId != null )
73- enrichers . Add ( new PropertyEnricher ( _options . EnricherPropertyNames . TenantId , _currentUser . TenantId ) ) ;
7449
75- if ( _currentUser ? . UserId != null )
76- enrichers . Add ( new PropertyEnricher ( _options . EnricherPropertyNames . UserId , _currentUser . UserId ) ) ;
50+ AddUserEnrichers ( context , enrichers ) ;
51+ AddClientEnrichers ( context , enrichers ) ;
52+ AddCorrelationIdEnrichers ( context , enrichers ) ;
7753
78- if ( _currentClient ? . Id != null )
79- enrichers . Add ( new PropertyEnricher ( _options . EnricherPropertyNames . ClientId , _currentClient . Id ) ) ;
54+ if ( enrichers . Count > 0 )
55+ {
56+ using ( LogContext . Push ( enrichers . ToArray ( ) ) )
57+ await _next ( context ) ;
58+ }
59+ else
60+ {
61+ await _next ( context ) ;
62+ }
63+ }
8064
81- var correlationId = _correlationIdProvider . Get ( ) ;
65+ /// <summary>
66+ /// 添加当前用户相关的日志事件增强器
67+ /// </summary>
68+ /// <param name="context">当前 HTTP 请求上下文</param>
69+ /// <param name="enrichers">日志事件增强器集合</param>
70+ private void AddUserEnrichers ( HttpContext context , ICollection < ILogEventEnricher > enrichers )
71+ {
72+ var currentUser = context ? . RequestServices ? . GetRequiredService < ICurrentUser > ( ) ;
73+ if ( currentUser == null )
74+ return ;
75+ if ( ! string . IsNullOrWhiteSpace ( currentUser . TenantId ) )
76+ enrichers . Add ( new PropertyEnricher ( _options . EnricherPropertyNames . TenantId , currentUser . TenantId ) ) ;
77+
78+ if ( ! string . IsNullOrWhiteSpace ( currentUser . UserId ) )
79+ enrichers . Add ( new PropertyEnricher ( _options . EnricherPropertyNames . UserId , currentUser . UserId ) ) ;
80+ }
81+
82+ /// <summary>
83+ /// 添加当前客户端相关的日志事件增强器
84+ /// </summary>
85+ /// <param name="context">当前 HTTP 请求上下文</param>
86+ /// <param name="enrichers">日志事件增强器集合</param>
87+ private void AddClientEnrichers ( HttpContext context , ICollection < ILogEventEnricher > enrichers )
88+ {
89+ var currentClient = context ? . RequestServices ? . GetRequiredService < ICurrentClient > ( ) ;
90+ if ( currentClient == null )
91+ return ;
92+ if ( ! string . IsNullOrWhiteSpace ( currentClient . Id ) )
93+ enrichers . Add ( new PropertyEnricher ( _options . EnricherPropertyNames . ClientId , currentClient . Id ) ) ;
94+ }
95+
96+ /// <summary>
97+ /// 添加关联ID相关的日志事件增强器
98+ /// </summary>
99+ /// <param name="context">当前 HTTP 请求上下文</param>
100+ /// <param name="enrichers">日志事件增强器集合</param>
101+ private void AddCorrelationIdEnrichers ( HttpContext context , ICollection < ILogEventEnricher > enrichers )
102+ {
103+ var correlationIdProvider = context ? . RequestServices ? . GetRequiredService < ICorrelationIdProvider > ( ) ;
104+ if ( correlationIdProvider == null )
105+ return ;
106+ var correlationId = correlationIdProvider . Get ( ) ;
82107 if ( ! string . IsNullOrEmpty ( correlationId ) )
83108 enrichers . Add ( new PropertyEnricher ( _options . EnricherPropertyNames . CorrelationId , correlationId ) ) ;
84-
85- using ( LogContext . Push ( enrichers . ToArray ( ) ) )
86- await _next ( context ) ;
87109 }
88- }
110+ }
0 commit comments