Skip to content

Commit bf3d58f

Browse files
committed
feat: 新增 Bing.Logging 单元测试
1 parent 68c9629 commit bf3d58f

File tree

12 files changed

+522
-5
lines changed

12 files changed

+522
-5
lines changed

Bing.All.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bing.Logging", "framework\s
232232
EndProject
233233
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}"
234234
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}"
235+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bing.Logging.Serilog", "framework\src\Bing.Logging.Serilog\Bing.Logging.Serilog.csproj", "{9E817B3A-BA88-42A4-A7D7-C4C935889BC5}"
236+
EndProject
237+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bing.Logging.Tests", "framework\tests\Bing.Logging.Tests\Bing.Logging.Tests.csproj", "{3DDB338B-8212-4D81-858D-FEF355960773}"
236238
EndProject
237239
Global
238240
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -528,6 +530,10 @@ Global
528530
{9E817B3A-BA88-42A4-A7D7-C4C935889BC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
529531
{9E817B3A-BA88-42A4-A7D7-C4C935889BC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
530532
{9E817B3A-BA88-42A4-A7D7-C4C935889BC5}.Release|Any CPU.Build.0 = Release|Any CPU
533+
{3DDB338B-8212-4D81-858D-FEF355960773}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
534+
{3DDB338B-8212-4D81-858D-FEF355960773}.Debug|Any CPU.Build.0 = Debug|Any CPU
535+
{3DDB338B-8212-4D81-858D-FEF355960773}.Release|Any CPU.ActiveCfg = Release|Any CPU
536+
{3DDB338B-8212-4D81-858D-FEF355960773}.Release|Any CPU.Build.0 = Release|Any CPU
531537
EndGlobalSection
532538
GlobalSection(SolutionProperties) = preSolution
533539
HideSolutionNode = FALSE
@@ -631,6 +637,7 @@ Global
631637
{ECB340A3-A21C-460D-8EBC-09EA5FB8731C} = {33F6CAB3-FD8A-4B91-9B94-A1E2DBC2759B}
632638
{04EB5533-6885-4292-BED1-4D06A5E162D1} = {33F6CAB3-FD8A-4B91-9B94-A1E2DBC2759B}
633639
{9E817B3A-BA88-42A4-A7D7-C4C935889BC5} = {33F6CAB3-FD8A-4B91-9B94-A1E2DBC2759B}
640+
{3DDB338B-8212-4D81-858D-FEF355960773} = {5C7976B4-C243-41B9-8303-8E8FAE099D31}
634641
EndGlobalSection
635642
GlobalSection(ExtensibilityGlobals) = postSolution
636643
SolutionGuid = {C1202A0F-83CC-4602-BCE5-20CB640BCAD4}

framework/src/Bing.Logging/Bing/Logging/Log.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text;
44
using Bing.Extensions;
5+
using Bing.Helpers;
56
using Bing.Text;
67
using Microsoft.Extensions.Logging;
78

@@ -275,11 +276,14 @@ protected virtual void AddLogContext()
275276
/// </summary>
276277
protected virtual void ConvertStateToContent()
277278
{
278-
// TODO:此处需要将对象转换成字典的方式,键值对
279-
if (LogState is IDictionary<string, object> state)
279+
if (LogState == null)
280+
return;
281+
var state = Conv.ToDictionary(LogState);
282+
foreach (var item in state)
280283
{
281-
foreach (var item in state)
282-
LogProperties.Add(item);
284+
if(item.Value.SafeString().IsEmpty())
285+
continue;
286+
LogProperties.Add(item);
283287
}
284288
}
285289

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
11+
<PackageReference Include="xunit" Version="2.4.1" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
<PrivateAssets>all</PrivateAssets>
15+
</PackageReference>
16+
<PackageReference Include="coverlet.collector" Version="1.3.0">
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
<PrivateAssets>all</PrivateAssets>
19+
</PackageReference>
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<ProjectReference Include="..\..\src\Bing.Logging\Bing.Logging.csproj" />
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Bing.Logging.Tests.Samples;
4+
using Microsoft.Extensions.Logging;
5+
using Moq;
6+
using Xunit;
7+
8+
namespace Bing.Logging.Tests
9+
{
10+
/// <summary>
11+
/// 日志操作测试 - 测试致命级别
12+
/// </summary>
13+
public partial class LogTest
14+
{
15+
/// <summary>
16+
/// 测试写致命日志 - 同时设置自定义扩展属性、状态对象、日志消息
17+
/// </summary>
18+
[Fact]
19+
public void Test_LogCritical_Message()
20+
{
21+
var product = new Product { Code = "a", Name = "b", Price = 123 };
22+
_log.Message("a{b}{c}", 1, 2)
23+
.Property("d", "3")
24+
.Property("e", "4")
25+
.State(product)
26+
.LogCritical();
27+
_mockLogger.Verify(t => t.LogCritical(0, null, "[d:{d},e:{e},Code:{Code},Name:{Name},Price:{Price}]a{b}{c}", "3", "4", "a", "b", 123, 1, 2));
28+
}
29+
30+
/// <summary>
31+
/// 测试写致命日志 - 设置自定义扩展属性、状态对象
32+
/// </summary>
33+
[Fact]
34+
public void Test_LogCritical_Property()
35+
{
36+
var product = new Product { Code = "a", Name = "b", Price = 123 };
37+
_log.Property("Age", "18")
38+
.State(product)
39+
.LogCritical();
40+
_mockLogger.Verify(t => t.Log(LogLevel.Critical, 0,
41+
It.Is<IDictionary<string, object>>(dict => dict.Count == 4 && dict["Age"].ToString() == "18" && dict["Name"].ToString() == "b"),
42+
null,
43+
It.IsAny<Func<IDictionary<string, object>, Exception, string>>()));
44+
}
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Bing.Logging.Tests.Samples;
4+
using Microsoft.Extensions.Logging;
5+
using Moq;
6+
using Xunit;
7+
8+
namespace Bing.Logging.Tests
9+
{
10+
/// <summary>
11+
/// 日志操作测试 - 测试调试级别
12+
/// </summary>
13+
public partial class LogTest
14+
{
15+
/// <summary>
16+
/// 测试写调试日志 - 同时设置自定义扩展属性、状态对象、日志消息
17+
/// </summary>
18+
[Fact]
19+
public void Test_LogDebug_Message()
20+
{
21+
var product = new Product { Code = "a", Name = "b", Price = 123 };
22+
_log.Message("a{b}{c}", 1, 2)
23+
.Property("d", "3")
24+
.Property("e", "4")
25+
.State(product)
26+
.LogDebug();
27+
_mockLogger.Verify(t => t.LogDebug(0, null, "[d:{d},e:{e},Code:{Code},Name:{Name},Price:{Price}]a{b}{c}", "3", "4", "a", "b", 123, 1, 2));
28+
}
29+
30+
/// <summary>
31+
/// 测试写调试日志 - 设置自定义扩展属性、状态对象
32+
/// </summary>
33+
[Fact]
34+
public void Test_LogDebug_Property()
35+
{
36+
var product = new Product { Code = "a", Name = "b", Price = 123 };
37+
_log.Property("Age", "18")
38+
.State(product)
39+
.LogDebug();
40+
_mockLogger.Verify(t => t.Log(LogLevel.Debug, 0,
41+
It.Is<IDictionary<string, object>>(dict => dict.Count == 4 && dict["Age"].ToString() == "18" && dict["Name"].ToString() == "b"),
42+
null,
43+
It.IsAny<Func<IDictionary<string, object>, Exception, string>>()));
44+
}
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Bing.Logging.Tests.Samples;
4+
using Microsoft.Extensions.Logging;
5+
using Moq;
6+
using Xunit;
7+
8+
namespace Bing.Logging.Tests
9+
{
10+
/// <summary>
11+
/// 日志操作测试 - 测试错误级别
12+
/// </summary>
13+
public partial class LogTest
14+
{
15+
/// <summary>
16+
/// 测试写错误日志 - 同时设置自定义扩展属性、状态对象、日志消息
17+
/// </summary>
18+
[Fact]
19+
public void Test_LogError_Message()
20+
{
21+
var product = new Product { Code = "a", Name = "b", Price = 123 };
22+
_log.Message("a{b}{c}", 1, 2)
23+
.Property("d", "3")
24+
.Property("e", "4")
25+
.State(product)
26+
.LogError();
27+
_mockLogger.Verify(t => t.LogError(0, null, "[d:{d},e:{e},Code:{Code},Name:{Name},Price:{Price}]a{b}{c}", "3", "4", "a", "b", 123, 1, 2));
28+
}
29+
30+
/// <summary>
31+
/// 测试写错误日志 - 设置自定义扩展属性、状态对象
32+
/// </summary>
33+
[Fact]
34+
public void Test_LogError_Property()
35+
{
36+
var product = new Product { Code = "a", Name = "b", Price = 123 };
37+
_log.Property("Age", "18")
38+
.State(product)
39+
.LogError();
40+
_mockLogger.Verify(t => t.Log(LogLevel.Error, 0,
41+
It.Is<IDictionary<string, object>>(dict => dict.Count == 4 && dict["Age"].ToString() == "18" && dict["Name"].ToString() == "b"),
42+
null,
43+
It.IsAny<Func<IDictionary<string, object>, Exception, string>>()));
44+
}
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Bing.Logging.Tests.Samples;
4+
using Microsoft.Extensions.Logging;
5+
using Moq;
6+
using Xunit;
7+
8+
namespace Bing.Logging.Tests
9+
{
10+
/// <summary>
11+
/// 日志操作测试 - 测试信息级别
12+
/// </summary>
13+
public partial class LogTest
14+
{
15+
/// <summary>
16+
/// 测试写信息日志 - 同时设置自定义扩展属性、状态对象、日志消息
17+
/// </summary>
18+
[Fact]
19+
public void Test_LogInformation_Message()
20+
{
21+
var product = new Product { Code = "a", Name = "b", Price = 123 };
22+
_log.Message("a{b}{c}", 1, 2)
23+
.Property("d", "3")
24+
.Property("e", "4")
25+
.State(product)
26+
.LogInformation();
27+
_mockLogger.Verify(t => t.LogInformation(0, null, "[d:{d},e:{e},Code:{Code},Name:{Name},Price:{Price}]a{b}{c}", "3", "4", "a", "b", 123, 1, 2));
28+
}
29+
30+
/// <summary>
31+
/// 测试写信息日志 - 设置自定义扩展属性、状态对象
32+
/// </summary>
33+
[Fact]
34+
public void Test_LogInformation_Property()
35+
{
36+
var product = new Product { Code = "a", Name = "b", Price = 123 };
37+
_log.Property("Age", "18")
38+
.State(product)
39+
.LogInformation();
40+
_mockLogger.Verify(t => t.Log(LogLevel.Information, 0,
41+
It.Is<IDictionary<string, object>>(dict => dict.Count == 4 && dict["Age"].ToString() == "18" && dict["Name"].ToString() == "b"),
42+
null,
43+
It.IsAny<Func<IDictionary<string, object>, Exception, string>>()));
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)