From 7d106888f324b4f2579ac5fdd57fa4239a38e6d5 Mon Sep 17 00:00:00 2001 From: Michael Mortensen Date: Wed, 18 Sep 2024 00:24:11 +0200 Subject: [PATCH 1/2] :technologist: improved DX when setting up xUnit test logger --- .../PackageReleaseNotes.txt | 6 ++ CHANGELOG.md | 8 +++ .../LoggerExtensions.cs | 4 +- .../ServiceCollectionExtensions.cs | 55 +++++++++---------- .../AspNetCoreHostTestTest.cs | 2 +- .../MvcAspNetCoreHostTestTest.cs | 2 +- 6 files changed, 42 insertions(+), 35 deletions(-) diff --git a/.nuget/Codebelt.Extensions.Xunit.Hosting/PackageReleaseNotes.txt b/.nuget/Codebelt.Extensions.Xunit.Hosting/PackageReleaseNotes.txt index 4f1d19e..5c86f92 100644 --- a/.nuget/Codebelt.Extensions.Xunit.Hosting/PackageReleaseNotes.txt +++ b/.nuget/Codebelt.Extensions.Xunit.Hosting/PackageReleaseNotes.txt @@ -5,6 +5,12 @@ Availability: .NET 9, .NET 8 and .NET Standard 2.0 - CHANGED Dependencies to latest and greatest with respect to TFMs - REMOVED Support for TFM .NET 6 (LTS)   +# Breaking Changes +- REMOVED AddXunitTestLogging method (the overload that took an ITestOutputHelperAccessor argument) from the ServiceCollectionExtensions class in the Codebelt.Extensions.Xunit.Hosting namespace +  +# Improvements +- CHANGED AddXunitTestLogging method in the Codebelt.Extensions.Xunit.Hosting namespace to use an ILoggerProvider that utilizes either the ITestOutputHelper instance or a previously provided ITestOutputHelperAccessor service +  Version 8.4.1 Availability: .NET 8, .NET 6 and .NET Standard 2.0   diff --git a/CHANGELOG.md b/CHANGELOG.md index 15399c7..594b89c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,14 @@ This major release is first and foremost focused on ironing out any wrinkles tha } ``` +### Changed + +- AddXunitTestLogging method in the Codebelt.Extensions.Xunit.Hosting namespace to use an ILoggerProvider that utilizes either the ITestOutputHelper instance or a previously provided ITestOutputHelperAccessor service + +### Removed + +- AddXunitTestLogging method (the overload that took an ITestOutputHelperAccessor argument) from the ServiceCollectionExtensions class in the Codebelt.Extensions.Xunit.Hosting namespace + ## [8.4.1] - 2024-09-16 ### Added diff --git a/src/Codebelt.Extensions.Xunit.Hosting/LoggerExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting/LoggerExtensions.cs index 7a7e13b..90943b9 100644 --- a/src/Codebelt.Extensions.Xunit.Hosting/LoggerExtensions.cs +++ b/src/Codebelt.Extensions.Xunit.Hosting/LoggerExtensions.cs @@ -2,9 +2,7 @@ using System.Collections; using System.Linq; using Cuemon; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using Xunit.Abstractions; namespace Codebelt.Extensions.Xunit.Hosting { @@ -14,7 +12,7 @@ namespace Codebelt.Extensions.Xunit.Hosting public static class LoggerExtensions { /// - /// Returns the associated that is provided when settings up services from either or . + /// Returns the associated that is provided when settings up services from . /// /// The from which to retrieve the . /// Returns an implementation of with all logged entries expressed as . diff --git a/src/Codebelt.Extensions.Xunit.Hosting/ServiceCollectionExtensions.cs b/src/Codebelt.Extensions.Xunit.Hosting/ServiceCollectionExtensions.cs index f935e08..7a97200 100644 --- a/src/Codebelt.Extensions.Xunit.Hosting/ServiceCollectionExtensions.cs +++ b/src/Codebelt.Extensions.Xunit.Hosting/ServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Cuemon; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -21,39 +22,33 @@ public static class ServiceCollectionExtensions /// /// cannot be null -or- /// cannot be null. - /// + /// + /// This method will add a xUnit logger provider based on either or . public static IServiceCollection AddXunitTestLogging(this IServiceCollection services, ITestOutputHelper output, LogLevel minimumLevel = LogLevel.Trace) { Validator.ThrowIfNull(services); - Validator.ThrowIfNull(output); - services.AddLogging(builder => - { - builder.SetMinimumLevel(minimumLevel); - builder.AddProvider(new XunitTestLoggerProvider(output)); - }); - return services; - } - - /// - /// Adds a unit test optimized implementation of output logging to the collection. - /// - /// The to extend. - /// The that provides access to the output for the logging. - /// The that specifies the minimum level to include for the logging. - /// A reference to so that additional configuration calls can be chained. - /// - /// cannot be null -or- - /// cannot be null. - /// - public static IServiceCollection AddXunitTestLogging(this IServiceCollection services, ITestOutputHelperAccessor accessor, LogLevel minimumLevel = LogLevel.Trace) - { - Validator.ThrowIfNull(services); - Validator.ThrowIfNull(accessor); - services.AddLogging(builder => - { - builder.SetMinimumLevel(minimumLevel); - builder.AddProvider(new XunitTestLoggerProvider(accessor)); - }); + Validator.ThrowIfNull(output); + if (services.Any(d => d.ServiceType == typeof(ITestOutputHelperAccessor))) + { + services.AddLogging(builder => + { + builder.SetMinimumLevel(minimumLevel); + builder.Services.AddSingleton(provider => + { + var accessor = provider.GetRequiredService(); + accessor.TestOutput = output; + return new XunitTestLoggerProvider(accessor); + }); + }); + } + else + { + services.AddLogging(builder => + { + builder.SetMinimumLevel(minimumLevel); + builder.AddProvider(new XunitTestLoggerProvider(output)); + }); + } return services; } diff --git a/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/AspNetCoreHostTestTest.cs b/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/AspNetCoreHostTestTest.cs index de7e2cf..559e90a 100644 --- a/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/AspNetCoreHostTestTest.cs +++ b/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/AspNetCoreHostTestTest.cs @@ -78,7 +78,7 @@ public override void ConfigureServices(IServiceCollection services) o.E = true; }); services.AddXunitTestLoggingOutputHelperAccessor(); - services.AddXunitTestLogging(new TestOutputHelperAccessor(TestOutput)); + services.AddXunitTestLogging(TestOutput); } } } diff --git a/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/MvcAspNetCoreHostTestTest.cs b/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/MvcAspNetCoreHostTestTest.cs index ed459e5..bbaeb66 100644 --- a/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/MvcAspNetCoreHostTestTest.cs +++ b/test/Codebelt.Extensions.Xunit.Hosting.AspNetCore.Tests/MvcAspNetCoreHostTestTest.cs @@ -46,7 +46,7 @@ public override void ConfigureServices(IServiceCollection services) .AddApplicationPart(typeof(FakeController).Assembly); services.AddXunitTestLoggingOutputHelperAccessor(); - services.AddXunitTestLogging(new TestOutputHelperAccessor(TestOutput)); + services.AddXunitTestLogging(TestOutput); } public override void ConfigureApplication(IApplicationBuilder app) From 164723dd831b0fabbb3c85d6a987e168d01ef9d5 Mon Sep 17 00:00:00 2001 From: Michael Mortensen Date: Wed, 18 Sep 2024 00:25:21 +0200 Subject: [PATCH 2/2] :alembic: treat Cuemon.* as a private asset --- Directory.Build.props | 1 + .../Codebelt.Extensions.Xunit.Hosting.AspNetCore.csproj | 4 ++-- .../Codebelt.Extensions.Xunit.Hosting.csproj | 1 + .../Codebelt.Extensions.Xunit.csproj | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index c5737fe..bd7ed95 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -84,6 +84,7 @@ + diff --git a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Codebelt.Extensions.Xunit.Hosting.AspNetCore.csproj b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Codebelt.Extensions.Xunit.Hosting.AspNetCore.csproj index 0d39be5..9dbdc6d 100644 --- a/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Codebelt.Extensions.Xunit.Hosting.AspNetCore.csproj +++ b/src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/Codebelt.Extensions.Xunit.Hosting.AspNetCore.csproj @@ -24,8 +24,8 @@ - - + + diff --git a/src/Codebelt.Extensions.Xunit.Hosting/Codebelt.Extensions.Xunit.Hosting.csproj b/src/Codebelt.Extensions.Xunit.Hosting/Codebelt.Extensions.Xunit.Hosting.csproj index 0b2eb40..fc7c0fc 100644 --- a/src/Codebelt.Extensions.Xunit.Hosting/Codebelt.Extensions.Xunit.Hosting.csproj +++ b/src/Codebelt.Extensions.Xunit.Hosting/Codebelt.Extensions.Xunit.Hosting.csproj @@ -35,6 +35,7 @@ + diff --git a/src/Codebelt.Extensions.Xunit/Codebelt.Extensions.Xunit.csproj b/src/Codebelt.Extensions.Xunit/Codebelt.Extensions.Xunit.csproj index 4e03fb9..c6abf81 100644 --- a/src/Codebelt.Extensions.Xunit/Codebelt.Extensions.Xunit.csproj +++ b/src/Codebelt.Extensions.Xunit/Codebelt.Extensions.Xunit.csproj @@ -10,7 +10,7 @@ - +