Skip to content

Commit a28260d

Browse files
committed
feat: 新增 Serilog 日志属性中间件
1 parent a6b711c commit a28260d

File tree

12 files changed

+188
-9
lines changed

12 files changed

+188
-9
lines changed

Bing.All.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution", "solution", "{B6
263263
version.props = version.props
264264
EndProjectSection
265265
EndProject
266-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bing.Uow", "framework\src\Bing.Uow\Bing.Uow.csproj", "{659C6450-6A39-44A3-A1A5-449ECAA1AFBE}"
266+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bing.Uow", "framework\src\Bing.Uow\Bing.Uow.csproj", "{659C6450-6A39-44A3-A1A5-449ECAA1AFBE}"
267+
EndProject
268+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bing.AspNetCore.Serilog", "framework\src\Bing.AspNetCore.Serilog\Bing.AspNetCore.Serilog.csproj", "{46718841-DF9E-41A4-AF87-C8E81AAE6F55}"
267269
EndProject
268270
Global
269271
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -587,6 +589,10 @@ Global
587589
{659C6450-6A39-44A3-A1A5-449ECAA1AFBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
588590
{659C6450-6A39-44A3-A1A5-449ECAA1AFBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
589591
{659C6450-6A39-44A3-A1A5-449ECAA1AFBE}.Release|Any CPU.Build.0 = Release|Any CPU
592+
{46718841-DF9E-41A4-AF87-C8E81AAE6F55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
593+
{46718841-DF9E-41A4-AF87-C8E81AAE6F55}.Debug|Any CPU.Build.0 = Debug|Any CPU
594+
{46718841-DF9E-41A4-AF87-C8E81AAE6F55}.Release|Any CPU.ActiveCfg = Release|Any CPU
595+
{46718841-DF9E-41A4-AF87-C8E81AAE6F55}.Release|Any CPU.Build.0 = Release|Any CPU
590596
EndGlobalSection
591597
GlobalSection(SolutionProperties) = preSolution
592598
HideSolutionNode = FALSE
@@ -702,6 +708,7 @@ Global
702708
{4A87040E-99A6-4A3F-BD6D-D2A3C3663012} = {2BA5C677-C0B9-458B-8A6D-D3E74B6F60EA}
703709
{B6E2B626-F561-4B47-8CFE-E8A305696522} = {2BA5C677-C0B9-458B-8A6D-D3E74B6F60EA}
704710
{659C6450-6A39-44A3-A1A5-449ECAA1AFBE} = {AED4D92B-5196-47E9-BD2C-84D4077F5A70}
711+
{46718841-DF9E-41A4-AF87-C8E81AAE6F55} = {052D4342-8B65-4836-A9B6-681A7812760E}
705712
EndGlobalSection
706713
GlobalSection(ExtensibilityGlobals) = postSolution
707714
SolutionGuid = {C1202A0F-83CC-4602-BCE5-20CB640BCAD4}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFrameworks>netcoreapp3.1;netstandard2.0;</TargetFrameworks>
4+
</PropertyGroup>
5+
6+
<PropertyGroup>
7+
<AssemblyName>Bing.AspNetCore.Serilog</AssemblyName>
8+
<PackageId>Bing.AspNetCore.Serilog</PackageId>
9+
<Description>Bing.AspNetCore.Serilog是Bing应用框架的Serilog日志扩展</Description>
10+
</PropertyGroup>
11+
12+
<Import Project="..\..\..\common.props" />
13+
14+
<Import Project="dependency.props" />
15+
16+
<Import Project="references.props" />
17+
</Project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace Bing.AspNetCore.Serilog
2+
{
3+
/// <summary>
4+
/// AspNetCore 基于 Serilog 选项配置,用于设置 Enricher 信息
5+
/// </summary>
6+
public class BingAspNetCoreSerilogOptions
7+
{
8+
/// <summary>
9+
/// Enricher 属性名称
10+
/// </summary>
11+
public AllEnricherPropertyNames EnricherPropertyNames { get; } = new AllEnricherPropertyNames();
12+
13+
/// <summary>
14+
/// Enricher 属性名称
15+
/// </summary>
16+
public class AllEnricherPropertyNames
17+
{
18+
/// <summary>
19+
/// 租户标识。默认值:TenantId
20+
/// </summary>
21+
public string TenantId { get; set; } = "TenantId";
22+
23+
/// <summary>
24+
/// 用户标识。默认值:UserId
25+
/// </summary>
26+
public string UserId { get; set; } = "UserId";
27+
28+
/// <summary>
29+
/// 客户端标识。默认值:ClientId
30+
/// </summary>
31+
public string ClientId { get; set; } = "ClientId";
32+
33+
/// <summary>
34+
/// 关联标识。默认值:CorrelationId
35+
/// </summary>
36+
public string CorrelationId { get; set; } = "CorrelationId";
37+
}
38+
}
39+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Bing.Clients;
4+
using Bing.DependencyInjection;
5+
using Bing.Tracing;
6+
using Bing.Users;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.Options;
9+
using Serilog.Context;
10+
using Serilog.Core;
11+
using Serilog.Core.Enrichers;
12+
13+
namespace Bing.AspNetCore.Serilog
14+
{
15+
/// <summary>
16+
/// Serilog 中间件
17+
/// </summary>
18+
public class BingSerilogMiddleware : IMiddleware, ITransientDependency
19+
{
20+
/// <summary>
21+
/// 当前客户端
22+
/// </summary>
23+
private readonly ICurrentClient _currentClient;
24+
25+
/// <summary>
26+
/// 当前用户
27+
/// </summary>
28+
private readonly ICurrentUser _currentUser;
29+
30+
/// <summary>
31+
/// 跟踪标识提供程序
32+
/// </summary>
33+
private readonly ICorrelationIdProvider _correlationIdProvider;
34+
35+
/// <summary>
36+
/// Serilog 选项配置
37+
/// </summary>
38+
private readonly BingAspNetCoreSerilogOptions _options;
39+
40+
/// <summary>
41+
/// 方法
42+
/// </summary>
43+
private readonly RequestDelegate _next;
44+
45+
/// <summary>
46+
/// 初始化一个<see cref="BingSerilogMiddleware"/>类型的实例
47+
/// </summary>
48+
/// <param name="next">方法</param>
49+
/// <param name="currentUser">当前用户</param>
50+
/// <param name="currentClient">当前客户端</param>
51+
/// <param name="correlationIdProvider">跟踪标识提供程序</param>
52+
/// <param name="options">Serilog 选项配置</param>
53+
public BingSerilogMiddleware(
54+
RequestDelegate next,
55+
ICurrentUser currentUser,
56+
ICurrentClient currentClient,
57+
ICorrelationIdProvider correlationIdProvider,
58+
IOptions<BingAspNetCoreSerilogOptions> options)
59+
{
60+
_next = next;
61+
_currentUser = currentUser;
62+
_currentClient = currentClient;
63+
_correlationIdProvider = correlationIdProvider;
64+
_options = options.Value;
65+
}
66+
67+
/// <summary>
68+
/// 执行中间件拦截逻辑
69+
/// </summary>
70+
/// <param name="context">Http上下文</param>
71+
public async Task InvokeAsync(HttpContext context)
72+
{
73+
var enrichers = new List<ILogEventEnricher>();
74+
if (_currentUser?.TenantId != null)
75+
enrichers.Add(new PropertyEnricher(_options.EnricherPropertyNames.TenantId, _currentUser.TenantId));
76+
77+
if (_currentUser?.UserId != null)
78+
enrichers.Add(new PropertyEnricher(_options.EnricherPropertyNames.UserId, _currentUser.UserId));
79+
80+
if (_currentClient?.Id != null)
81+
enrichers.Add(new PropertyEnricher(_options.EnricherPropertyNames.ClientId, _currentClient.Id));
82+
83+
var correlationId = _correlationIdProvider.Get();
84+
if (!string.IsNullOrEmpty(correlationId))
85+
enrichers.Add(new PropertyEnricher(_options.EnricherPropertyNames.CorrelationId, correlationId));
86+
87+
using (LogContext.Push(enrichers.ToArray()))
88+
await _next(context);
89+
}
90+
}
91+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Bing.AspNetCore.Serilog;
2+
3+
namespace Microsoft.AspNetCore.Builder
4+
{
5+
/// <summary>
6+
/// 应用程序构建器(<see cref="IApplicationBuilder"/>) 扩展
7+
/// </summary>
8+
public static class BingAspNetCoreSerilogApplicationBuilderExtensions
9+
{
10+
/// <summary>
11+
/// 注册Serilog Enricher的日志信息中间件
12+
/// </summary>
13+
/// <param name="app">应用程序构建器</param>
14+
public static IApplicationBuilder UseBingSerilogEnrichers(this IApplicationBuilder app)
15+
{
16+
return app.UseMiddleware<BingSerilogMiddleware>();
17+
}
18+
}
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project>
2+
<ItemGroup>
3+
<PackageReference Include="Serilog" Version="2.12.0" />
4+
</ItemGroup>
5+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project>
2+
<ItemGroup>
3+
<ProjectReference Include="..\Bing.AspNetCore\Bing.AspNetCore.csproj" />
4+
</ItemGroup>
5+
</Project>

framework/src/Bing.AspNetCore/Bing/AspNetCore/ExceptionHandling/IBingAuthorizationExceptionHandler.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
52
using Bing.Authorization;
63
using Microsoft.AspNetCore.Http;
74

framework/src/Bing.AspNetCore/Microsoft/AspNetCore/Builder/BingApplicationBuilderExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public static IApplicationBuilder UseBingExceptionHandling(this IApplicationBuil
6363
/// <param name="app">应用程序构建器</param>
6464
public static IApplicationBuilder UseBingClaimsMap(this IApplicationBuilder app) => app.UseMiddleware<BingClaimsMapMiddleware>();
6565

66-
6766
/// <summary>
6867
/// Bing框架初始化,适用于AspNetCore环境
6968
/// </summary>

framework/src/Bing.Security/Bing/Users/CurrentUser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public virtual string UserName
9191
/// <summary>
9292
/// 租户标识
9393
/// </summary>
94-
public virtual string TenantId => _principalAccessor.Principal?.FindTenantId()?.ToString() ?? string.Empty;
94+
public virtual string TenantId => _principalAccessor.Principal?.FindTenantId()?.ToString();
9595

9696
/// <summary>
9797
/// 角色列表

0 commit comments

Comments
 (0)