Skip to content

Commit 74a84f5

Browse files
authored
Test logging refactoring (#1638)
Remove reflection-based method name lookup from test log messages and scopes by using caller member name helpers. Switch test logger factories from Serilog sinks to MartinCostello.Logging.XUnit and update test package references accordingly. Replace Serilog test logging with MartinCostello logger.
1 parent 49fad85 commit 74a84f5

91 files changed

Lines changed: 1367 additions & 1292 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

test/SIPSorcery.VP8.TestVectors/SIPSorcery.VP8.TestVectors.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.7.1" />
1011
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.5.1" />
1112
<PackageReference Include="xunit" Version="2.9.3" />
1213
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
@@ -17,9 +18,6 @@
1718
<PrivateAssets>all</PrivateAssets>
1819
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1920
</PackageReference>
20-
<PackageReference Include="Serilog.Extensions.Logging" Version="10.0.0" />
21-
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
22-
<PackageReference Include="Serilog.Sinks.XUnit" Version="3.0.19" />
2321
</ItemGroup>
2422

2523
<ItemGroup>
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//-----------------------------------------------------------------------------
1+
//-----------------------------------------------------------------------------
22
// Filename: TestLogger.cs
33
//
44
// Description: Helper class for test logging.
@@ -14,23 +14,24 @@
1414
//-----------------------------------------------------------------------------
1515

1616
using Microsoft.Extensions.Logging;
17-
using Serilog;
18-
using Serilog.Extensions.Logging;
17+
using MartinCostello.Logging.XUnit;
1918

2019
namespace Vpx.Net.TestVectors
2120
{
2221
public class TestLogger
2322
{
2423
public static ILoggerFactory GetLogger(Xunit.Abstractions.ITestOutputHelper output)
2524
{
26-
string template = "{Timestamp:HH:mm:ss.ffff} [{Level}] {Scope} {Message}{NewLine}{Exception}";
27-
var serilog = new LoggerConfiguration()
28-
.MinimumLevel.Is(Serilog.Events.LogEventLevel.Debug)
29-
.Enrich.WithProperty("ThreadId", System.Threading.Thread.CurrentThread.ManagedThreadId)
30-
.WriteTo.TestOutput(output, outputTemplate: template)
31-
.WriteTo.Console(outputTemplate: template)
32-
.CreateLogger();
33-
return new SerilogLoggerFactory(serilog);
25+
var options = new XUnitLoggerOptions
26+
{
27+
Filter = (category, level) => level >= LogLevel.Debug
28+
};
29+
var loggerProvider = new XUnitLoggerProvider(output, options);
30+
31+
return LoggerFactory.Create(builder =>
32+
{
33+
builder.AddProvider(loggerProvider);
34+
});
3435
}
3536
}
36-
}
37+
}

test/SIPSorcery.VP8.UnitTest/SIPSorcery.VP8.UnitTest.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.7.1" />
1011
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.5.1" />
1112
<PackageReference Include="System.Drawing.Common" Version="10.0.7" />
1213
<PackageReference Include="xunit" Version="2.9.3" />
@@ -18,9 +19,6 @@
1819
<PrivateAssets>all</PrivateAssets>
1920
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2021
</PackageReference>
21-
<PackageReference Include="Serilog.Extensions.Logging" Version="10.0.0" />
22-
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
23-
<PackageReference Include="Serilog.Sinks.XUnit" Version="3.0.19" />
2422
</ItemGroup>
2523

2624
<ItemGroup>
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
using Microsoft.Extensions.Logging;
2-
using Serilog;
3-
using Serilog.Extensions.Logging;
1+
using MartinCostello.Logging.XUnit;
2+
using Microsoft.Extensions.Logging;
43

54
namespace Vpx.Net.UnitTest
65
{
76
public class TestLogger
87
{
98
public static ILoggerFactory GetLogger(Xunit.Abstractions.ITestOutputHelper output)
109
{
11-
string template = "{Timestamp:HH:mm:ss.ffff} [{Level}] {Scope} {Message}{NewLine}{Exception}";
12-
//var loggerFactory = new Microsoft.Extensions.Logging.LoggerFactory();
13-
var serilog = new LoggerConfiguration()
14-
.MinimumLevel.Is(Serilog.Events.LogEventLevel.Debug)
15-
.Enrich.WithProperty("ThreadId", System.Threading.Thread.CurrentThread.ManagedThreadId)
16-
.WriteTo.TestOutput(output, outputTemplate: template)
17-
.WriteTo.Console(outputTemplate: template)
18-
.CreateLogger();
19-
//SIPSorcery.LogFactory.Set(new SerilogLoggerFactory(serilog));
20-
//return new SerilogLoggerProvider(serilog).CreateLogger("unit");
21-
return new SerilogLoggerFactory(serilog);
10+
var options = new XUnitLoggerOptions
11+
{
12+
Filter = (category, level) => level >= LogLevel.Debug
13+
};
14+
var loggerProvider = new XUnitLoggerProvider(output, options);
15+
16+
return LoggerFactory.Create(builder =>
17+
{
18+
builder.AddProvider(loggerProvider);
19+
});
2220
}
2321
}
2422
}

test/SIPSorceryMedia.Abstractions.UnitTest/PixelConverterTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//-----------------------------------------------------------------------------
1+
//-----------------------------------------------------------------------------
22
// Filename: PixelConverterTest.cs
33
//
44
// Description: Unit tests for the pixel conversion methods.
@@ -110,7 +110,7 @@ public unsafe void ConvertKnownI420ToBGRTest()
110110
public unsafe void ConvertKnownNV12ToBGRTest()
111111
{
112112
logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
113-
logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);
113+
logger.BeginScope(TestHelper.GetCurrentMethodName());
114114

115115
int width = 640;
116116
int height = 480;
@@ -285,7 +285,7 @@ public unsafe void Roundtrip_Bitmap_719x405()
285285
public unsafe void ConvertNV12ToI420Test()
286286
{
287287
logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
288-
logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);
288+
logger.BeginScope(TestHelper.GetCurrentMethodName());
289289

290290
int width = 640;
291291
int height = 480;
@@ -316,7 +316,7 @@ public unsafe void ConvertNV12ToI420Test()
316316
public unsafe void ConvertI420ToNV12Test()
317317
{
318318
logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
319-
logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);
319+
logger.BeginScope(TestHelper.GetCurrentMethodName());
320320

321321
int width = 640;
322322
int height = 480;
@@ -347,7 +347,7 @@ public unsafe void ConvertI420ToNV12Test()
347347
public void RoundtripNV12ToI420Test()
348348
{
349349
logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
350-
logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);
350+
logger.BeginScope(TestHelper.GetCurrentMethodName());
351351

352352
int width = 640;
353353
int height = 480;
@@ -372,7 +372,7 @@ public void RoundtripNV12ToI420Test()
372372
public void RoundtripI420ToNV12Test()
373373
{
374374
logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
375-
logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);
375+
logger.BeginScope(TestHelper.GetCurrentMethodName());
376376

377377
int width = 640;
378378
int height = 480;

test/SIPSorceryMedia.Abstractions.UnitTest/SIPSorceryMedia.Abstractions.UnitTest.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.7" />
10+
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.7.1" />
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.5.1" />
1212
<PackageReference Include="System.Drawing.Common" Version="10.0.7" />
1313
<PackageReference Include="xunit" Version="2.9.3" />
@@ -19,9 +19,6 @@
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
<PrivateAssets>all</PrivateAssets>
2121
</PackageReference>
22-
<PackageReference Include="Serilog.Extensions.Logging" Version="10.0.0" />
23-
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
24-
<PackageReference Include="Serilog.Sinks.XUnit" Version="3.0.19" />
2522
</ItemGroup>
2623

2724
<ItemGroup>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace SIPSorceryMedia.Abstractions.UnitTest
4+
{
5+
public static class TestHelper
6+
{
7+
public static string GetCurrentMethodName([CallerMemberName] string methodName = default) => methodName;
8+
}
9+
}
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
using Microsoft.Extensions.Logging;
2-
using Serilog;
3-
using Serilog.Extensions.Logging;
2+
using MartinCostello.Logging.XUnit;
43

54
namespace SIPSorceryMedia.Abstractions.UnitTest
65
{
76
public class TestLogger
87
{
98
public static ILoggerFactory GetLogger(Xunit.Abstractions.ITestOutputHelper output)
109
{
11-
string template = "{Timestamp:HH:mm:ss.ffff} [{Level}] {Scope} {Message}{NewLine}{Exception}";
12-
var serilog = new LoggerConfiguration()
13-
.MinimumLevel.Is(Serilog.Events.LogEventLevel.Debug)
14-
.Enrich.WithProperty("ThreadId", System.Threading.Thread.CurrentThread.ManagedThreadId)
15-
.WriteTo.TestOutput(output, outputTemplate: template)
16-
.WriteTo.Console(outputTemplate: template)
17-
.CreateLogger();
18-
return new SerilogLoggerFactory(serilog);
10+
var options = new XUnitLoggerOptions
11+
{
12+
Filter = (category, level) => level >= LogLevel.Debug
13+
};
14+
var loggerProvider = new XUnitLoggerProvider(output, options);
15+
16+
return LoggerFactory.Create(builder =>
17+
{
18+
builder.AddProvider(loggerProvider);
19+
});
1920
}
2021
}
2122
}

0 commit comments

Comments
 (0)