Skip to content

Commit dc1b514

Browse files
committed
feat: 新增上下文信息
1 parent 41f4367 commit dc1b514

File tree

12 files changed

+292
-7
lines changed

12 files changed

+292
-7
lines changed

framework/src/Bing.Logging.Serilog/Bing.Logging.Serilog.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="project.props" />
33

44
<Import Project="project.dependency.props" />
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using Serilog.Core;
3+
using Serilog.Events;
4+
5+
namespace Bing.Logging.Serilog.Enrichers
6+
{
7+
/// <summary>
8+
/// 环境变量扩展属性
9+
/// </summary>
10+
internal class EnvironmentEnricher : ILogEventEnricher
11+
{
12+
/// <summary>
13+
/// 环境变量名称
14+
/// </summary>
15+
private readonly string _environmentVariable;
16+
17+
/// <summary>
18+
/// 初始化一个<see cref="EnvironmentEnricher"/>类型的实例
19+
/// </summary>
20+
/// <param name="variableName">环境变量名称</param>
21+
public EnvironmentEnricher(string variableName) => _environmentVariable = variableName;
22+
23+
/// <summary>
24+
/// 扩展属性
25+
/// </summary>
26+
/// <param name="logEvent">日志事件</param>
27+
/// <param name="propertyFactory">日志事件属性工厂</param>
28+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
29+
{
30+
var envValue = Environment.GetEnvironmentVariable(_environmentVariable);
31+
if (!string.IsNullOrWhiteSpace(envValue))
32+
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(_environmentVariable, envValue));
33+
}
34+
}
35+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using Serilog.Core;
3+
using Serilog.Debugging;
4+
using Serilog.Events;
5+
6+
namespace Bing.Logging.Serilog.Enrichers
7+
{
8+
/// <summary>
9+
/// 函数扩展属性
10+
/// </summary>
11+
internal class FunctionEnricher : ILogEventEnricher
12+
{
13+
/// <summary>
14+
/// 键名
15+
/// </summary>
16+
private readonly string _key;
17+
18+
/// <summary>
19+
/// 操作函数1
20+
/// </summary>
21+
private readonly Func<object, string> _func1;
22+
23+
/// <summary>
24+
/// 操作函数0
25+
/// </summary>
26+
private readonly Func<string> _func0;
27+
28+
/// <summary>
29+
/// 操作函数2
30+
/// </summary>
31+
private readonly Func<LogEvent, string> _func2;
32+
33+
/// <summary>
34+
/// 参数
35+
/// </summary>
36+
private readonly object _parameter;
37+
38+
/// <summary>
39+
/// 初始化一个<see cref="FunctionEnricher"/>类型的实例
40+
/// </summary>
41+
/// <param name="key">键名</param>
42+
/// <param name="func">操作函数</param>
43+
public FunctionEnricher(string key, Func<string> func)
44+
{
45+
_key = key;
46+
_func0 = func;
47+
}
48+
49+
/// <summary>
50+
/// 初始化一个<see cref="FunctionEnricher"/>类型的实例
51+
/// </summary>
52+
/// <param name="key">键名</param>
53+
/// <param name="func">操作函数</param>
54+
/// <param name="parameter">参数</param>
55+
public FunctionEnricher(string key, Func<object, string> func, object parameter)
56+
{
57+
_key = key;
58+
_func1 = func;
59+
_parameter = parameter;
60+
}
61+
62+
/// <summary>
63+
/// 初始化一个<see cref="FunctionEnricher"/>类型的实例
64+
/// </summary>
65+
/// <param name="key">键名</param>
66+
/// <param name="func">操作函数</param>
67+
public FunctionEnricher(string key, Func<LogEvent, string> func)
68+
{
69+
_key = key;
70+
_func2 = func;
71+
}
72+
73+
/// <summary>
74+
/// 扩展属性
75+
/// </summary>
76+
/// <param name="logEvent">日志事件</param>
77+
/// <param name="propertyFactory">日志事件属性工厂</param>
78+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
79+
{
80+
try
81+
{
82+
var result = string.Empty;
83+
84+
if (_func0 != null)
85+
result = _func0();
86+
else if (_func1 != null)
87+
result = _func1(_parameter);
88+
else if (_func2 != null)
89+
result = _func2(logEvent);
90+
91+
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(_key, result));
92+
}
93+
catch (Exception e)
94+
{
95+
SelfLog.WriteLine(e.Message);
96+
}
97+
}
98+
}
99+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
using Serilog.Core;
4+
using Serilog.Events;
5+
6+
namespace Bing.Logging.Serilog.Enrichers
7+
{
8+
/// <summary>
9+
/// 键值对扩展属性
10+
/// </summary>
11+
internal class KeyValueEnricher : ILogEventEnricher
12+
{
13+
/// <summary>
14+
/// 键值对
15+
/// </summary>
16+
private KeyValuePair<string, object> _keyValue;
17+
18+
/// <summary>
19+
/// 初始化一个<see cref="KeyValueEnricher"/>类型的实例
20+
/// </summary>
21+
/// <param name="keyValue">键值对</param>
22+
public KeyValueEnricher(KeyValuePair<string, object> keyValue) => _keyValue = keyValue;
23+
24+
/// <summary>
25+
/// 扩展属性
26+
/// </summary>
27+
/// <param name="logEvent">日志事件</param>
28+
/// <param name="propertyFactory">日志事件属性工厂</param>
29+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
30+
{
31+
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(_keyValue.Key, JsonConvert.SerializeObject(_keyValue.Value)));
32+
}
33+
}
34+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Bing.Logging.Serilog.Enrichers
88
/// <summary>
99
/// 日志上下文扩展属性
1010
/// </summary>
11-
public class LogContextEnricher : ILogEventEnricher
11+
internal class LogContextEnricher : ILogEventEnricher
1212
{
1313
/// <summary>
1414
/// 日志上下文

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Bing.Logging.Serilog.Enrichers
77
/// <summary>
88
/// 日志级别扩展属性 - 用于显示标准日志级别
99
/// </summary>
10-
public class LogLevelEnricher : ILogEventEnricher
10+
internal class LogLevelEnricher : ILogEventEnricher
1111
{
1212
/// <summary>
1313
/// 扩展属性

framework/src/Bing.Logging.Serilog/Bing/Logging/Serilog/LoggerEnrichmentConfigurationExtensions.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
23
using Bing.Logging.Serilog.Enrichers;
34
using Serilog;
45
using Serilog.Configuration;
6+
using Serilog.Events;
57

68
namespace Bing.Logging.Serilog
79
{
@@ -31,5 +33,87 @@ public static LoggerConfiguration WithLogLevel(this LoggerEnrichmentConfiguratio
3133
throw new ArgumentNullException(nameof(source));
3234
return source.With<LogLevelEnricher>();
3335
}
36+
37+
/// <summary>
38+
/// 添加键值对扩展属性
39+
/// </summary>
40+
/// <param name="source">日志扩展配置</param>
41+
/// <param name="keyValue">键值对</param>
42+
public static LoggerConfiguration WithProperty(this LoggerEnrichmentConfiguration source, KeyValuePair<string, object> keyValue)
43+
{
44+
if (source == null)
45+
throw new ArgumentNullException(nameof(source));
46+
if (keyValue.Equals(default(KeyValuePair<string, object>)))
47+
throw new ArgumentNullException(nameof(keyValue));
48+
return source.With(new KeyValueEnricher(keyValue));
49+
}
50+
51+
/// <summary>
52+
/// 添加函数扩展属性
53+
/// </summary>
54+
/// <param name="source">日志扩展配置</param>
55+
/// <param name="key">键名</param>
56+
/// <param name="func">操作函数</param>
57+
public static LoggerConfiguration WithFunction(this LoggerEnrichmentConfiguration source, string key, Func<LogEvent, string> func)
58+
{
59+
if (source == null)
60+
throw new ArgumentNullException(nameof(source));
61+
if (string.IsNullOrWhiteSpace(key))
62+
throw new ArgumentNullException(nameof(key));
63+
if (func == null)
64+
throw new ArgumentNullException(nameof(func));
65+
return source.With(new FunctionEnricher(key, func));
66+
}
67+
68+
/// <summary>
69+
/// 添加函数扩展属性
70+
/// </summary>
71+
/// <param name="source">日志扩展配置</param>
72+
/// <param name="key">键名</param>
73+
/// <param name="func">操作函数</param>
74+
public static LoggerConfiguration WithFunction(this LoggerEnrichmentConfiguration source, string key, Func<string> func)
75+
{
76+
if (source == null)
77+
throw new ArgumentNullException(nameof(source));
78+
if (string.IsNullOrWhiteSpace(key))
79+
throw new ArgumentNullException(nameof(key));
80+
if (func == null)
81+
throw new ArgumentNullException(nameof(func));
82+
return source.With(new FunctionEnricher(key, func));
83+
}
84+
85+
/// <summary>
86+
/// 添加函数扩展属性
87+
/// </summary>
88+
/// <param name="source">日志扩展配置</param>
89+
/// <param name="key">键名</param>
90+
/// <param name="func">操作函数</param>
91+
/// <param name="parameter">参数</param>
92+
public static LoggerConfiguration WithFunction(this LoggerEnrichmentConfiguration source, string key, Func<object, string> func, object parameter)
93+
{
94+
if (source == null)
95+
throw new ArgumentNullException(nameof(source));
96+
if (string.IsNullOrWhiteSpace(key))
97+
throw new ArgumentNullException(nameof(key));
98+
if (func == null)
99+
throw new ArgumentNullException(nameof(func));
100+
if (parameter == null)
101+
throw new ArgumentNullException(nameof(parameter));
102+
return source.With(new FunctionEnricher(key, func, parameter));
103+
}
104+
105+
/// <summary>
106+
/// 添加环境扩展属性
107+
/// </summary>
108+
/// <param name="source">日志扩展配置</param>
109+
/// <param name="environmentVariable">环境变量</param>
110+
public static LoggerConfiguration WithEnvironment(this LoggerEnrichmentConfiguration source, string environmentVariable)
111+
{
112+
if (source == null)
113+
throw new ArgumentNullException(nameof(source));
114+
if (string.IsNullOrWhiteSpace(environmentVariable))
115+
throw new ArgumentNullException(nameof(environmentVariable));
116+
return source.With(new EnvironmentEnricher(environmentVariable));
117+
}
34118
}
35119
}

framework/src/Bing.Logging.Sinks.Exceptionless/Serilog/Sinks/Exceptionless/ExceptionlessSink.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public ExceptionlessSink(
8989
/// <param name="logEvent">日志事件</param>
9090
public void Emit(LogEvent logEvent)
9191
{
92-
if (logEvent == null || _client.Configuration.IsValid)
92+
if (logEvent == null || !_client.Configuration.IsValid)
9393
return;
9494
var minLogLevel = _client.Configuration.Settings.GetMinLogLevel(logEvent.GetSource());
9595
if (LogLevelSwitcher.Switch(logEvent.Level) < minLogLevel)

modules/admin/src/Bing.Admin/Apis/TestController.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using DotNetCore.CAP.Internal;
1111
using Microsoft.AspNetCore.Authorization;
1212
using Microsoft.AspNetCore.Mvc;
13+
using Microsoft.Extensions.Logging;
1314

1415
namespace Bing.Admin.Apis
1516
{
@@ -43,20 +44,27 @@ public class TestController : ApiControllerBase
4344
/// </summary>
4445
public IExceptionNotifier ExceptionNotifier { get; }
4546

47+
/// <summary>
48+
/// 日志组件
49+
/// </summary>
50+
public ILogger<TestController> Logger { get; }
51+
4652
/// <summary>
4753
/// 初始化一个<see cref="TestController"/>类型的实例
4854
/// </summary>
4955
public TestController(ITestService testService
5056
, IProcessingServer processingServer
5157
, IMessageEventBus messageEventBus
5258
, IAdminUnitOfWork unitOfWork
53-
, IExceptionNotifier exceptionNotifier)
59+
, IExceptionNotifier exceptionNotifier
60+
, ILogger<TestController> logger)
5461
{
5562
TestService = testService;
5663
ProcessingServer = processingServer;
5764
MessageEventBus = messageEventBus;
5865
UnitOfWork = unitOfWork;
5966
ExceptionNotifier = exceptionNotifier;
67+
Logger = logger;
6068
}
6169

6270
/// <summary>
@@ -105,5 +113,18 @@ public async Task<IActionResult> TestArgumentNullAsync()
105113
await TestService.TestArgumentNullAsync(null);
106114
return Success();
107115
}
116+
/// <summary>
117+
/// 测试日志
118+
/// </summary>
119+
[AllowAnonymous]
120+
[HttpGet("testLogger")]
121+
public Task<IActionResult> TestLoggerAsync(string text)
122+
{
123+
Logger.LogTrace($"输出调试信息: {text}");
124+
Logger.LogDebug($"输出调试信息: {text}");
125+
Logger.LogInformation($"输出调试信息: {text}");
126+
Logger.LogWarning($"输出调试信息: {text}");
127+
return Task.FromResult(Success());
128+
}
108129
}
109130
}

modules/admin/src/Bing.Admin/Bing.Admin.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<PackageReference Include="Bing.Extensions.Swashbuckle" Version="1.2.2" />
2020
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="5.6.3" />
2121
<PackageReference Include="DotNetCore.CAP.Dashboard" Version="3.1.2" />
22+
23+
<PackageReference Include="Serilog.Enrichers.Span" Version="2.0.1" />
2224
</ItemGroup>
2325

2426
<ItemGroup>

0 commit comments

Comments
 (0)