Skip to content

Commit a425ac9

Browse files
committed
✨ feat(Logging): 增加用户和租户信息的日志记录
- 在 LogContext 中添加 TenantId 和 SessionId 属性 - 在 AspNetCoreLogContextAccessor 中获取当前用户信息并设置到 LogContext - 在 LogContextEnricher 中添加 SessionId 和 TenantId 的日志 enricher - 更新 LogModule 中的 Exceptionless 配置
1 parent 3759a99 commit a425ac9

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

framework/src/Bing.AspNetCore/Bing/AspNetCore/Logs/AspNetCoreLogContextAccessor.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Bing.DependencyInjection;
33
using Bing.Logging;
44
using Bing.Tracing;
5+
using Bing.Users;
56
using Microsoft.AspNetCore.Http;
67
using Microsoft.AspNetCore.Http.Extensions;
78
using Microsoft.Extensions.DependencyInjection;
@@ -24,17 +25,25 @@ public class AspNetCoreLogContextAccessor : LogContextAccessor
2425
/// </summary>
2526
protected IWebClientInfoProvider WebClientInfoProvider { get; }
2627

28+
/// <summary>
29+
/// 当前用户
30+
/// </summary>
31+
protected ICurrentUser CurrentUser { get; }
32+
2733
/// <summary>
2834
/// 初始化一个<see cref="AspNetCoreLogContextAccessor"/>类型的实例
2935
/// </summary>
3036
/// <param name="webClientInfoProvider">Web客户端信息提供程序</param>
3137
/// <param name="httpContextAccessor">Http上下文访问器</param>
38+
/// <param name="currentUser">当前用户</param>
3239
public AspNetCoreLogContextAccessor(
3340
IHttpContextAccessor httpContextAccessor,
34-
IWebClientInfoProvider webClientInfoProvider)
41+
IWebClientInfoProvider webClientInfoProvider,
42+
ICurrentUser currentUser)
3543
{
3644
HttpContextAccessor = httpContextAccessor;
3745
WebClientInfoProvider = webClientInfoProvider;
46+
CurrentUser = currentUser;
3847
}
3948

4049
/// <summary>
@@ -47,6 +56,13 @@ protected override LogContext Create()
4756
context.Browser = WebClientInfoProvider.ClientIpAddress;
4857
context.Url = HttpContextAccessor.HttpContext?.Request?.GetDisplayUrl();
4958
context.IsWebEnv = HttpContextAccessor.HttpContext?.Request != null;
59+
context.UserId = CurrentUser.UserId;
60+
context.TenantId = CurrentUser.TenantId;
61+
context.Application = CurrentUser.GetApplicationName();
62+
context.Data["UserName"] = CurrentUser.GetUserName();
63+
context.Data["FullName"] = CurrentUser.GetFullName();
64+
context.Data["TenantCode"] = CurrentUser.GetTenantCode();
65+
context.Data["TenantName"] = CurrentUser.GetTenantName();
5066
if (!context.IsWebEnv)
5167
context.TraceId = base.GetTraceId();
5268
return context;

framework/src/Bing.Logging.Serilog/Bing/Logging/Serilog/Enrichers/LogContextEnricher.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
3131
return;
3232
//RemoveProperties(logEvent);
3333
AddTraceId(logEvent, propertyFactory);
34+
AddSessionId(logEvent, propertyFactory);
3435
AddUserId(logEvent, propertyFactory);
36+
AddTenantId(logEvent, propertyFactory);
3537
AddApplication(logEvent, propertyFactory);
3638
AddEnvironment(logEvent, propertyFactory);
3739
AddExtraData(logEvent, propertyFactory);
@@ -56,7 +58,7 @@ private void RemoveProperties(LogEvent logEvent)
5658
private void AddTraceId(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
5759
{
5860
var traceId = _context.TraceId;
59-
if (!_context.IsWebEnv)
61+
if (!_context.IsWebEnv)
6062
traceId = TraceIdContext.Current?.TraceId;
6163
if (string.IsNullOrWhiteSpace(traceId))
6264
return;
@@ -65,6 +67,17 @@ private void AddTraceId(LogEvent logEvent, ILogEventPropertyFactory propertyFact
6567
logEvent.AddOrUpdateProperty(property);
6668
}
6769

70+
/// <summary>
71+
/// 添加会话标识
72+
/// </summary>
73+
private void AddSessionId(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
74+
{
75+
if (string.IsNullOrWhiteSpace(_context.SessionId))
76+
return;
77+
var property = propertyFactory.CreateProperty("SessionId", _context.SessionId);
78+
logEvent.AddOrUpdateProperty(property);
79+
}
80+
6881
/// <summary>
6982
/// 添加用户标识
7083
/// </summary>
@@ -76,6 +89,17 @@ private void AddUserId(LogEvent logEvent, ILogEventPropertyFactory propertyFacto
7689
logEvent.AddOrUpdateProperty(property);
7790
}
7891

92+
/// <summary>
93+
/// 添加租户标识
94+
/// </summary>
95+
private void AddTenantId(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
96+
{
97+
if (string.IsNullOrWhiteSpace(_context.TenantId))
98+
return;
99+
var property = propertyFactory.CreateProperty("TenantId", _context.UserId);
100+
logEvent.AddOrUpdateProperty(property);
101+
}
102+
79103
/// <summary>
80104
/// 添加应用程序
81105
/// </summary>
@@ -107,6 +131,8 @@ private void AddExtraData(LogEvent logEvent, ILogEventPropertyFactory propertyFa
107131
return;
108132
foreach (var item in _context.Data)
109133
{
134+
if (item.Value == null)
135+
continue;
110136
var property = propertyFactory.CreateProperty(item.Key, item.Value);
111137
logEvent.AddOrUpdateProperty(property);
112138
}

framework/src/Bing.Logging/Bing/Logging/LogContext.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public class LogContext
2020
/// </summary>
2121
public string UserId { get; set; }
2222

23+
/// <summary>
24+
/// 租户标识
25+
/// </summary>
26+
public string TenantId { get; set; }
27+
2328
/// <summary>
2429
/// 应用程序
2530
/// </summary>
@@ -50,6 +55,11 @@ public class LogContext
5055
/// </summary>
5156
public string Url { get; set; }
5257

58+
/// <summary>
59+
/// 会话标识
60+
/// </summary>
61+
public string SessionId { get; set; }
62+
5363
/// <summary>
5464
/// 是否Web环境
5565
/// </summary>

modules/admin/src/Bing.Admin/Modules/LogModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override IServiceCollection AddServices(IServiceCollection services)
4747
//ExceptionlessClient.Default.Startup();
4848
services.AddExceptionless(x =>
4949
{
50-
x.ApiKey = "vCFssLV6HPlElQ6wkQJaLvaCqvhTTsWWTOm8dzQo";
50+
x.ApiKey = "aUBmHcfhK8VvPLqsTYvVOeXJYY4jN5QghOY68FZe";
5151
x.ServerUrl = "http://10.186.135.147:5100";
5252
});
5353
services.AddLogging(loggingBuilder =>

tests/Bing.Tests.Samples/Bing/Tests/Samples/AggregateRootSample.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ protected override void AddChanges(AggregateRootSample newObj)
178178
AddChange(IntSamples,newObj.IntSamples);
179179
}
180180

181+
/// <summary>
182+
/// 获取名称
183+
/// </summary>
184+
public string GetName() => "c";
185+
181186
/// <summary>
182187
/// 创建测试样例
183188
/// </summary>
@@ -223,4 +228,4 @@ public class AggregateRootSample2 : AggregateRoot<AggregateRootSample>
223228
public AggregateRootSample2(Guid id) : base(id)
224229
{
225230
}
226-
}
231+
}

0 commit comments

Comments
 (0)