Skip to content

Commit bbee086

Browse files
committed
feat: 新增 Serilog 日志输出模块
1 parent c4dea4e commit bbee086

File tree

13 files changed

+368
-93
lines changed

13 files changed

+368
-93
lines changed

Bing.All.sln

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bing.Test.Shared", "framewo
228228
EndProject
229229
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bing.Ddd.Domain.Tests", "framework\tests\Bing.Ddd.Domain.Tests\Bing.Ddd.Domain.Tests.csproj", "{8B581529-D7D2-4DCF-824A-2AAFB58F8C94}"
230230
EndProject
231-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bing.Logging", "framework\src\Bing.Logging\Bing.Logging.csproj", "{ECB340A3-A21C-460D-8EBC-09EA5FB8731C}"
231+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bing.Logging", "framework\src\Bing.Logging\Bing.Logging.csproj", "{ECB340A3-A21C-460D-8EBC-09EA5FB8731C}"
232232
EndProject
233-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bing.Logging.Sinks.Exceptionless", "framework\src\Bing.Logging.Sinks.Exceptionless\Bing.Logging.Sinks.Exceptionless.csproj", "{04EB5533-6885-4292-BED1-4D06A5E162D1}"
233+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bing.Logging.Sinks.Exceptionless", "framework\src\Bing.Logging.Sinks.Exceptionless\Bing.Logging.Sinks.Exceptionless.csproj", "{04EB5533-6885-4292-BED1-4D06A5E162D1}"
234+
EndProject
235+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bing.Logging.Serilog", "framework\src\Bing.Logging.Serilog\Bing.Logging.Serilog.csproj", "{9E817B3A-BA88-42A4-A7D7-C4C935889BC5}"
234236
EndProject
235237
Global
236238
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -522,6 +524,10 @@ Global
522524
{04EB5533-6885-4292-BED1-4D06A5E162D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
523525
{04EB5533-6885-4292-BED1-4D06A5E162D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
524526
{04EB5533-6885-4292-BED1-4D06A5E162D1}.Release|Any CPU.Build.0 = Release|Any CPU
527+
{9E817B3A-BA88-42A4-A7D7-C4C935889BC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
528+
{9E817B3A-BA88-42A4-A7D7-C4C935889BC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
529+
{9E817B3A-BA88-42A4-A7D7-C4C935889BC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
530+
{9E817B3A-BA88-42A4-A7D7-C4C935889BC5}.Release|Any CPU.Build.0 = Release|Any CPU
525531
EndGlobalSection
526532
GlobalSection(SolutionProperties) = preSolution
527533
HideSolutionNode = FALSE
@@ -624,6 +630,7 @@ Global
624630
{8B581529-D7D2-4DCF-824A-2AAFB58F8C94} = {5C7976B4-C243-41B9-8303-8E8FAE099D31}
625631
{ECB340A3-A21C-460D-8EBC-09EA5FB8731C} = {33F6CAB3-FD8A-4B91-9B94-A1E2DBC2759B}
626632
{04EB5533-6885-4292-BED1-4D06A5E162D1} = {33F6CAB3-FD8A-4B91-9B94-A1E2DBC2759B}
633+
{9E817B3A-BA88-42A4-A7D7-C4C935889BC5} = {33F6CAB3-FD8A-4B91-9B94-A1E2DBC2759B}
627634
EndGlobalSection
628635
GlobalSection(ExtensibilityGlobals) = postSolution
629636
SolutionGuid = {C1202A0F-83CC-4602-BCE5-20CB640BCAD4}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="project.props" />
3+
4+
<Import Project="project.dependency.props" />
5+
6+
<Import Project="..\..\..\framework.props" />
7+
8+
</Project>
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using Bing.Extensions;
2+
using Serilog.Core;
3+
using Serilog.Events;
4+
5+
namespace Bing.Logging.Serilog.Enrichers
6+
{
7+
/// <summary>
8+
/// 日志上下文扩展属性
9+
/// </summary>
10+
public class LogContextEnricher : ILogEventEnricher
11+
{
12+
/// <summary>
13+
/// 日志上下文
14+
/// </summary>
15+
private LogContext _context;
16+
17+
/// <summary>
18+
/// 日志上下文访问器
19+
/// </summary>
20+
private readonly ILogContextAccessor _logContextAccessor;
21+
22+
/// <summary>
23+
/// 初始化一个 <see cref="LogContextEnricher"/>类型的实例
24+
/// </summary>
25+
/// <param name="logContextAccessor">日志上下文访问器</param>
26+
public LogContextEnricher(ILogContextAccessor logContextAccessor)
27+
{
28+
_logContextAccessor = logContextAccessor;
29+
}
30+
31+
/// <summary>
32+
/// 扩展属性
33+
/// </summary>
34+
/// <param name="logEvent">日志事件</param>
35+
/// <param name="propertyFactory">日志事件属性工厂</param>
36+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
37+
{
38+
if (_logContextAccessor == null)
39+
return;
40+
_context = _logContextAccessor.Context;
41+
if (_context == null)
42+
return;
43+
AddDuration(logEvent, propertyFactory);
44+
AddTraceId(logEvent, propertyFactory);
45+
AddUserId(logEvent, propertyFactory);
46+
AddApplication(logEvent, propertyFactory);
47+
AddEnvironment(logEvent, propertyFactory);
48+
AddExtraData(logEvent, propertyFactory);
49+
}
50+
51+
/// <summary>
52+
/// 移除默认设置的部分属性
53+
/// </summary>
54+
private void RemoveProperties(LogEvent logEvent)
55+
{
56+
logEvent.RemovePropertyIfPresent("ActionId");
57+
logEvent.RemovePropertyIfPresent("ActionName");
58+
logEvent.RemovePropertyIfPresent("RequestId");
59+
logEvent.RemovePropertyIfPresent("RequestPath");
60+
logEvent.RemovePropertyIfPresent("ConnectionId");
61+
}
62+
63+
/// <summary>
64+
/// 添加执行持续时间
65+
/// </summary>
66+
private void AddDuration(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
67+
{
68+
if (_context.Stopwatch == null)
69+
return;
70+
var property = propertyFactory.CreateProperty("Duration", _context.Stopwatch.Elapsed.Description());
71+
logEvent.AddOrUpdateProperty(property);
72+
}
73+
74+
/// <summary>
75+
/// 添加跟踪号
76+
/// </summary>
77+
private void AddTraceId(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
78+
{
79+
if (string.IsNullOrWhiteSpace(_context.TraceId))
80+
return;
81+
var property = propertyFactory.CreateProperty("TraceId", _context.TraceId);
82+
logEvent.AddOrUpdateProperty(property);
83+
}
84+
85+
/// <summary>
86+
/// 添加用户标识
87+
/// </summary>
88+
private void AddUserId(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
89+
{
90+
if (string.IsNullOrWhiteSpace(_context.UserId))
91+
return;
92+
var property = propertyFactory.CreateProperty("UserId", _context.UserId);
93+
logEvent.AddOrUpdateProperty(property);
94+
}
95+
96+
/// <summary>
97+
/// 添加应用程序
98+
/// </summary>
99+
private void AddApplication(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
100+
{
101+
if (string.IsNullOrWhiteSpace(_context.Application))
102+
return;
103+
var property = propertyFactory.CreateProperty("Application", _context.Application);
104+
logEvent.AddOrUpdateProperty(property);
105+
}
106+
107+
/// <summary>
108+
/// 添加执行环境
109+
/// </summary>
110+
private void AddEnvironment(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
111+
{
112+
if (string.IsNullOrWhiteSpace(_context.Environment))
113+
return;
114+
var property = propertyFactory.CreateProperty("Environment", _context.Environment);
115+
logEvent.AddOrUpdateProperty(property);
116+
}
117+
118+
/// <summary>
119+
/// 添加扩展数据
120+
/// </summary>
121+
private void AddExtraData(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
122+
{
123+
if (_context.Data.Count == 0)
124+
return;
125+
foreach (var item in _context.Data)
126+
{
127+
var property = propertyFactory.CreateProperty(item.Key, item.Value);
128+
logEvent.AddOrUpdateProperty(property);
129+
}
130+
}
131+
}
132+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Bing.Logging.Serilog.Internals;
2+
using Serilog.Core;
3+
using Serilog.Events;
4+
5+
namespace Bing.Logging.Serilog.Enrichers
6+
{
7+
/// <summary>
8+
/// 日志级别扩展属性 - 用于显示标准日志级别
9+
/// </summary>
10+
public class LogLevelEnricher : ILogEventEnricher
11+
{
12+
/// <summary>
13+
/// 扩展属性
14+
/// </summary>
15+
/// <param name="logEvent">日志事件</param>
16+
/// <param name="propertyFactory">日志事件属性工厂</param>
17+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
18+
{
19+
var property = propertyFactory.CreateProperty("LogLevel", LogLevelSwitcher.Switch(logEvent.Level));
20+
logEvent.AddOrUpdateProperty(property);
21+
}
22+
}
23+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Serilog.Events;
2+
3+
namespace Bing.Logging.Serilog.Internals
4+
{
5+
/// <summary>
6+
/// 日志级别切换器
7+
/// </summary>
8+
internal static class LogLevelSwitcher
9+
{
10+
/// <summary>
11+
/// 转换日志级别
12+
/// </summary>
13+
/// <param name="level">Serilog日志级别</param>
14+
/// <returns>MS日志级别</returns>
15+
public static string Switch(LogEventLevel level)
16+
{
17+
return level switch
18+
{
19+
LogEventLevel.Verbose => "Trace",
20+
LogEventLevel.Debug => "Debug",
21+
LogEventLevel.Information => "Information",
22+
LogEventLevel.Warning => "Warning",
23+
LogEventLevel.Error => "Error",
24+
LogEventLevel.Fatal => "Critical",
25+
_ => null
26+
};
27+
}
28+
29+
/// <summary>
30+
/// 转换日志级别
31+
/// </summary>
32+
/// <param name="level">MS日志级别</param>
33+
/// <returns>Serilog日志级别</returns>
34+
public static LogEventLevel Switch(string level)
35+
{
36+
return level.ToUpperInvariant() switch
37+
{
38+
"TRACE" => LogEventLevel.Verbose,
39+
"DEBUG" => LogEventLevel.Debug,
40+
"INFORMATION" => LogEventLevel.Information,
41+
"WARNING" => LogEventLevel.Warning,
42+
"ERROR" => LogEventLevel.Error,
43+
"CRITICAL" => LogEventLevel.Fatal,
44+
"NONE" => LogEventLevel.Fatal,
45+
_ => LogEventLevel.Warning
46+
};
47+
}
48+
}
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using Bing.Logging.Serilog.Internals;
3+
using Microsoft.Extensions.Configuration;
4+
using Serilog;
5+
using Serilog.Core;
6+
7+
namespace Bing.Logging.Serilog
8+
{
9+
/// <summary>
10+
/// Serilog日志配置操作(<see cref="LoggerConfiguration"/>) 扩展
11+
/// </summary>
12+
public static class LoggerConfigurationExtensions
13+
{
14+
/// <summary>
15+
/// 配置日志级别
16+
/// </summary>
17+
/// <param name="source">Serilog日志配置</param>
18+
/// <param name="configuration">配置</param>
19+
public static LoggerConfiguration ConfigLogLevel(this LoggerConfiguration source, IConfiguration configuration)
20+
{
21+
if (source == null)
22+
throw new ArgumentNullException(nameof(source));
23+
if (configuration == null)
24+
throw new ArgumentNullException(nameof(configuration));
25+
var section = configuration.GetSection("Logging:LogLevel");
26+
foreach (var item in section.GetChildren())
27+
{
28+
if (item.Key == "Default")
29+
{
30+
source.MinimumLevel.ControlledBy(new LoggingLevelSwitch(LogLevelSwitcher.Switch(item.Value)));
31+
continue;
32+
}
33+
source.MinimumLevel.Override(item.Key, LogLevelSwitcher.Switch(item.Value));
34+
}
35+
return source;
36+
}
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using Bing.Logging.Serilog.Enrichers;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Serilog;
5+
using Serilog.Configuration;
6+
7+
namespace Bing.Logging.Serilog
8+
{
9+
/// <summary>
10+
/// Serilog扩展属性配置(<see cref="LoggerEnrichmentConfiguration"/>) 扩展
11+
/// </summary>
12+
public static class LoggerEnrichmentConfigurationExtensions
13+
{
14+
/// <summary>
15+
/// 添加日志上下文扩展属性
16+
/// </summary>
17+
/// <param name="source">日志扩展配置</param>
18+
/// <param name="serviceProvider">服务提供程序</param>
19+
public static LoggerConfiguration WithLogContext(this LoggerEnrichmentConfiguration source, IServiceProvider serviceProvider)
20+
{
21+
if (source == null)
22+
throw new ArgumentNullException(nameof(source));
23+
if (serviceProvider == null)
24+
throw new ArgumentNullException(nameof(serviceProvider));
25+
var enricher = serviceProvider.GetRequiredService<LogContextEnricher>();
26+
return source.With(enricher);
27+
}
28+
29+
/// <summary>
30+
/// 添加日志级别扩展属性
31+
/// </summary>
32+
/// <param name="source">日志扩展配置</param>
33+
public static LoggerConfiguration WithLogLevel(this LoggerEnrichmentConfiguration source)
34+
{
35+
if (source == null)
36+
throw new ArgumentNullException(nameof(source));
37+
return source.With<LogLevelEnricher>();
38+
}
39+
}
40+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project>
2+
<ItemGroup>
3+
<ProjectReference Include="..\Bing.Logging\Bing.Logging.csproj" />
4+
</ItemGroup>
5+
6+
<ItemGroup>
7+
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
8+
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
9+
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
10+
<PackageReference Include="Serilog.Sinks.Seq" Version="5.1.0" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project>
2+
<PropertyGroup>
3+
<AssemblyName>Bing.Logging.Serilog</AssemblyName>
4+
<PackageId>Bing.Logging.Serilog</PackageId>
5+
<Description>Bing.Logging.Serilog是Bing应用框架的集成封装Serilog日志框架的核心类库,它包含了写入文件和Seq。</Description>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)