Skip to content

Commit 0a9f2f2

Browse files
committed
test: 新增 Serilog 日志单元测试
1 parent bf3d58f commit 0a9f2f2

File tree

11 files changed

+361
-27
lines changed

11 files changed

+361
-27
lines changed

Bing.All.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bing.Logging.Serilog", "fra
236236
EndProject
237237
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bing.Logging.Tests", "framework\tests\Bing.Logging.Tests\Bing.Logging.Tests.csproj", "{3DDB338B-8212-4D81-858D-FEF355960773}"
238238
EndProject
239+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bing.Logging.Serilog.Tests", "framework\tests\Bing.Logging.Serilog.Tests\Bing.Logging.Serilog.Tests.csproj", "{C572A0EF-5A1C-46DE-8465-AD6A0672F55A}"
240+
EndProject
239241
Global
240242
GlobalSection(SolutionConfigurationPlatforms) = preSolution
241243
Debug|Any CPU = Debug|Any CPU
@@ -534,6 +536,10 @@ Global
534536
{3DDB338B-8212-4D81-858D-FEF355960773}.Debug|Any CPU.Build.0 = Debug|Any CPU
535537
{3DDB338B-8212-4D81-858D-FEF355960773}.Release|Any CPU.ActiveCfg = Release|Any CPU
536538
{3DDB338B-8212-4D81-858D-FEF355960773}.Release|Any CPU.Build.0 = Release|Any CPU
539+
{C572A0EF-5A1C-46DE-8465-AD6A0672F55A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
540+
{C572A0EF-5A1C-46DE-8465-AD6A0672F55A}.Debug|Any CPU.Build.0 = Debug|Any CPU
541+
{C572A0EF-5A1C-46DE-8465-AD6A0672F55A}.Release|Any CPU.ActiveCfg = Release|Any CPU
542+
{C572A0EF-5A1C-46DE-8465-AD6A0672F55A}.Release|Any CPU.Build.0 = Release|Any CPU
537543
EndGlobalSection
538544
GlobalSection(SolutionProperties) = preSolution
539545
HideSolutionNode = FALSE
@@ -638,6 +644,7 @@ Global
638644
{04EB5533-6885-4292-BED1-4D06A5E162D1} = {33F6CAB3-FD8A-4B91-9B94-A1E2DBC2759B}
639645
{9E817B3A-BA88-42A4-A7D7-C4C935889BC5} = {33F6CAB3-FD8A-4B91-9B94-A1E2DBC2759B}
640646
{3DDB338B-8212-4D81-858D-FEF355960773} = {5C7976B4-C243-41B9-8303-8E8FAE099D31}
647+
{C572A0EF-5A1C-46DE-8465-AD6A0672F55A} = {5C7976B4-C243-41B9-8303-8E8FAE099D31}
641648
EndGlobalSection
642649
GlobalSection(ExtensibilityGlobals) = postSolution
643650
SolutionGuid = {C1202A0F-83CC-4602-BCE5-20CB640BCAD4}

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Bing.Extensions;
1+
using Bing.DependencyInjection;
2+
using Bing.Extensions;
23
using Serilog.Core;
34
using Serilog.Events;
45

@@ -14,30 +15,19 @@ public class LogContextEnricher : ILogEventEnricher
1415
/// </summary>
1516
private LogContext _context;
1617

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-
3118
/// <summary>
3219
/// 扩展属性
3320
/// </summary>
3421
/// <param name="logEvent">日志事件</param>
3522
/// <param name="propertyFactory">日志事件属性工厂</param>
3623
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
3724
{
38-
if (_logContextAccessor == null)
25+
if (!ServiceLocator.Instance.IsProviderEnabled)
26+
return;
27+
var accessor = ServiceLocator.Instance.GetService<ILogContextAccessor>();
28+
if (accessor == null)
3929
return;
40-
_context = _logContextAccessor.Context;
30+
_context = accessor.Context;
4131
if (_context == null)
4232
return;
4333
AddDuration(logEvent, propertyFactory);

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using Bing.Logging.Serilog.Enrichers;
3-
using Microsoft.Extensions.DependencyInjection;
43
using Serilog;
54
using Serilog.Configuration;
65

@@ -15,15 +14,11 @@ public static class LoggerEnrichmentConfigurationExtensions
1514
/// 添加日志上下文扩展属性
1615
/// </summary>
1716
/// <param name="source">日志扩展配置</param>
18-
/// <param name="serviceProvider">服务提供程序</param>
19-
public static LoggerConfiguration WithLogContext(this LoggerEnrichmentConfiguration source, IServiceProvider serviceProvider)
17+
public static LoggerConfiguration WithLogContext(this LoggerEnrichmentConfiguration source)
2018
{
2119
if (source == null)
2220
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);
21+
return source.With<LogContextEnricher>();
2722
}
2823

2924
/// <summary>

framework/src/Bing.Logging/Bing/Logging/ILog.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Bing.DependencyInjection;
23
using Microsoft.Extensions.Logging;
34

45
namespace Bing.Logging
@@ -7,7 +8,7 @@ namespace Bing.Logging
78
/// 日志操作
89
/// </summary>
910
/// <typeparam name="TCategoryName">日志类别</typeparam>
10-
public interface ILog<out TCategoryName>
11+
public interface ILog<out TCategoryName> : ITransientDependency
1112
{
1213
/// <summary>
1314
/// 设置日志事件标识
@@ -74,7 +75,7 @@ public interface ILog<out TCategoryName>
7475
/// 写警告日志
7576
/// </summary>
7677
ILog<TCategoryName> LogWarning();
77-
78+
7879
/// <summary>
7980
/// 写错误日志
8081
/// </summary>

framework/src/Bing.Logging/Bing/Logging/ILoggerWrapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Bing.DependencyInjection;
23
using Microsoft.Extensions.Logging;
34

45
namespace Bing.Logging
@@ -7,7 +8,7 @@ namespace Bing.Logging
78
/// 日志记录包装器
89
/// </summary>
910
/// <typeparam name="TCategoryName">日志类别</typeparam>
10-
public interface ILoggerWrapper<out TCategoryName>
11+
public interface ILoggerWrapper<out TCategoryName> : ITransientDependency
1112
{
1213
/// <summary>
1314
/// 是否启用
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<RootNamespace>Bing.Logging.Tests</RootNamespace>
6+
<IsPackable>false</IsPackable>
7+
<XunitStartupFullName>Bing.Logging.Tests.Startup</XunitStartupFullName>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.21" />
12+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.21" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
14+
<PackageReference Include="xunit" Version="2.4.1" />
15+
<PackageReference Include="Xunit.DependencyInjection" Version="8.3.0" />
16+
<PackageReference Include="Xunit.DependencyInjection.Logging" Version="8.0.0" />
17+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
<PackageReference Include="coverlet.collector" Version="1.3.0">
22+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23+
<PrivateAssets>all</PrivateAssets>
24+
</PackageReference>
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<ProjectReference Include="..\..\src\Bing.Logging.Serilog\Bing.Logging.Serilog.csproj" />
29+
</ItemGroup>
30+
31+
<ItemGroup>
32+
<None Update="appsettings.json">
33+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
34+
</None>
35+
</ItemGroup>
36+
37+
</Project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Bing.Helpers;
2+
3+
namespace Bing.Logging.Tests
4+
{
5+
/// <summary>
6+
/// 日志上下文访问器
7+
/// </summary>
8+
public class LogContextAccessor : ILogContextAccessor
9+
{
10+
/// <summary>
11+
/// 日志上下文键名
12+
/// </summary>
13+
public const string LogContextKey = "Bing.Logging.LogContext";
14+
15+
/// <summary>
16+
/// 日志上下文
17+
/// </summary>
18+
public LogContext Context
19+
{
20+
get
21+
{
22+
var slot = System.Threading.Thread.GetNamedDataSlot(LogContextKey);
23+
return Conv.To<LogContext>(System.Threading.Thread.GetData(slot));
24+
25+
}
26+
set
27+
{
28+
var slot = System.Threading.Thread.GetNamedDataSlot(LogContextKey);
29+
System.Threading.Thread.SetData(slot, value);
30+
}
31+
}
32+
}
33+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using Bing.Helpers;
5+
using Bing.Logging.Tests.Samples;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Xunit;
8+
9+
namespace Bing.Logging.Tests
10+
{
11+
/// <summary>
12+
/// 日志操作测试
13+
/// </summary>
14+
public class LogTest
15+
{
16+
/// <summary>
17+
/// 日志操作
18+
/// </summary>
19+
private readonly ILog<LogTest> _log;
20+
21+
/// <summary>
22+
/// 测试初始化
23+
/// </summary>
24+
public LogTest(IServiceProvider serviceProvider, ILog<LogTest> log, ILogContextAccessor accessor)
25+
{
26+
serviceProvider.UseBing();
27+
_log = log;
28+
accessor.Context = new LogContext {Stopwatch = Stopwatch.StartNew(), TraceId = Id.NewString()};
29+
}
30+
31+
/// <summary>
32+
/// 测试写跟踪日志 - 设置简单消息
33+
/// </summary>
34+
[Fact]
35+
public void Test_LogTrace_Message_1()
36+
{
37+
_log.Message($"Test_LogTrace_Message_1_{Id.NewString()}").LogTrace();
38+
}
39+
40+
/// <summary>
41+
/// 测试写跟踪日志 - 给日志传递1个参数
42+
/// </summary>
43+
[Fact]
44+
public void Test_LogTrace_Message_2()
45+
{
46+
_log.Message("Test_LogTrace_Message_2_{ProductCode}", Id.NewString()).LogTrace();
47+
}
48+
49+
/// <summary>
50+
/// 测试写跟踪日志 - 给日志传递2个参数
51+
/// </summary>
52+
[Fact]
53+
public void Test_LogTrace_Message_3()
54+
{
55+
_log.Message("Test_LogTrace_Message_3_{id}_{code}", $"id_{Id.NewString()}",$"code_{Id.NewString()}").LogTrace();
56+
}
57+
58+
/// <summary>
59+
/// 测试写跟踪日志 - 多次调用Message方法设置日志消息
60+
/// </summary>
61+
[Fact]
62+
public void Test_LogTrace_Message_4()
63+
{
64+
_log.Message("Test_LogTrace_Message_4_{id}_{code}_", $"id_{Id.NewString()}", $"code_{Id.NewString()}")
65+
.Message("{name}_{note}", $"name_{Id.NewString()}", $"note_{Id.NewString()}")
66+
.LogTrace();
67+
}
68+
69+
/// <summary>
70+
/// 测试写跟踪日志 - 设置@消息 - 对象
71+
/// </summary>
72+
[Fact]
73+
public void Test_LogTrace_Message_5()
74+
{
75+
_log.Message("Test_LogTrace_Message_5,@Product={@Product}", new Product {Code = "a", Name = "b", Price = 123})
76+
.LogTrace();
77+
}
78+
79+
/// <summary>
80+
/// 测试写跟踪日志 - 设置$消息 - 对象
81+
/// </summary>
82+
[Fact]
83+
public void Test_LogTrace_Message_6()
84+
{
85+
_log.Message("Test_LogTrace_Message_6,$Product={$Product}", new Product { Code = "a", Name = "b", Price = 123 })
86+
.LogTrace();
87+
}
88+
89+
/// <summary>
90+
/// 测试写跟踪日志 - 设置@消息 - 字典
91+
/// </summary>
92+
[Fact]
93+
public void Test_LogTrace_Message_7()
94+
{
95+
_log.Message("Test_LogTrace_Message_7,@Dictionary={@Dictionary}", new Dictionary<string, object> {{"Code", "a"}, {"Name", "b"}, {"Price", 123}})
96+
.LogTrace();
97+
}
98+
99+
/// <summary>
100+
/// 测试写跟踪日志 - 设$消息 - 字典
101+
/// </summary>
102+
[Fact]
103+
public void Test_LogTrace_Message_8()
104+
{
105+
_log.Message("Test_LogTrace_Message_8,$Dictionary={$Dictionary}", new Dictionary<string, object> { { "Code", "a" }, { "Name", "b" }, { "Price", 123 } })
106+
.LogTrace();
107+
}
108+
109+
/// <summary>
110+
/// 测试写跟踪日志 - 同时设置各属性
111+
/// </summary>
112+
[Fact]
113+
public void Test_LogTrace_Message_9()
114+
{
115+
_log.EventId(119)
116+
.Message("Test_LogTrace_Message_9_{id}", $"id_{Id.NewString()}")
117+
.State(new Product { Code = "a", Name = "b", Price = 123 })
118+
.Property("Age","18")
119+
.Property("Description","hello")
120+
.LogTrace();
121+
}
122+
123+
/// <summary>
124+
/// 测试写跟踪日志 - 设置自定义扩展属性
125+
/// </summary>
126+
[Fact]
127+
public void Test_LogTrace_Property_1()
128+
{
129+
_log.EventId(120)
130+
.Property("Age", "18")
131+
.Property("Description", "hello")
132+
.LogTrace();
133+
}
134+
135+
/// <summary>
136+
/// 测试写跟踪日志 - 设置自定义扩展属性、状态对象
137+
/// </summary>
138+
[Fact]
139+
public void Test_LogTrace_Property_2()
140+
{
141+
_log.EventId(121)
142+
.Property("Age", "18")
143+
.Property("Description", "hello")
144+
.State(new Product { Code = "a", Name = "b", Price = 123 })
145+
.LogTrace();
146+
}
147+
}
148+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Bing.Logging.Tests.Samples
2+
{
3+
/// <summary>
4+
/// 产品
5+
/// </summary>
6+
public class Product
7+
{
8+
/// <summary>
9+
/// 产品编码
10+
/// </summary>
11+
public string Code { get; set; }
12+
13+
/// <summary>
14+
/// 产品名称
15+
/// </summary>
16+
public string Name { get; set; }
17+
18+
/// <summary>
19+
/// 价格
20+
/// </summary>
21+
public int Price { get; set; }
22+
}
23+
}

0 commit comments

Comments
 (0)