diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e9cc3067..1448d94b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -57,7 +57,7 @@ jobs: - uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4 name: Upload coverage to Codecov with: - file: ./artifacts/coverage/coverage.net7.0.cobertura.xml + file: ./artifacts/coverage/coverage.net8.0.cobertura.xml flags: ${{ matrix.os_name }} - name: Publish artifacts @@ -70,7 +70,7 @@ jobs: uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3 with: name: packages-${{ matrix.os_name }} - path: ./artifacts/packages + path: ./artifacts/package/release if-no-files-found: error validate-packages: diff --git a/.vsconfig b/.vsconfig index 06e10617..a991ed17 100644 --- a/.vsconfig +++ b/.vsconfig @@ -3,7 +3,7 @@ "components": [ "Microsoft.VisualStudio.Component.CoreEditor", "Microsoft.VisualStudio.Workload.CoreEditor", - "Microsoft.NetCore.Component.Runtime.7.0", + "Microsoft.NetCore.Component.Runtime.8.0", "Microsoft.NetCore.Component.SDK", "Microsoft.VisualStudio.Component.Roslyn.Compiler", "Microsoft.VisualStudio.Component.Roslyn.LanguageServices" diff --git a/Directory.Build.props b/Directory.Build.props index 65c7b4ff..b561f91e 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,10 @@ + + <_Parameter1>false + <_Parameter1_IsLiteral>true + $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb @@ -21,6 +25,10 @@ latest true en-US + + $(NoWarn);SA1010 $(NoWarn);CA1848 $(NoWarn);SA0001 enable @@ -39,6 +47,7 @@ 00240000048000009400000006020000002400005253413100040000010001004b0b2efbada897147aa03d2076278890aefe2f8023562336d206ec8a719b06e89461c31b43abec615918d509158629f93385930c030494509e418bf396d69ce7dbe0b5b2db1a81543ab42777cb98210677fed69dbeb3237492a7ad69e87a1911ed20eb2d7c300238dc6f6403e3d04a1351c5cb369de4e022b18fbec70f7d21ed snupkg true + true 0.3.0.0 0.3.1 beta$([System.Convert]::ToInt32(`$(GITHUB_RUN_NUMBER)`).ToString(`0000`)) diff --git a/Directory.Build.targets b/Directory.Build.targets index 0765495f..4733ca51 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -20,11 +20,11 @@ - $([System.IO.Path]::Combine($(OutputPath), 'coverage', 'coverage')) + $([System.IO.Path]::Combine($(ArtifactsPath), 'coverage', 'coverage')) true HTML $(ReportGeneratorReportTypes);MarkdownSummaryGitHub - $([System.IO.Path]::Combine($(OutputPath), 'coverage')) + $([System.IO.Path]::Combine($(ArtifactsPath), 'coverage')) <_MarkdownSummaryPrefix><details><summary>:chart_with_upwards_trend: <b>$(AssemblyName) Code Coverage report</b></summary> <_MarkdownSummarySuffix></details> diff --git a/Directory.Packages.props b/Directory.Packages.props index 9fd92bbf..62277eac 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -2,9 +2,8 @@ - + - @@ -21,7 +20,6 @@ - diff --git a/Logging.XUnit.sln b/Logging.XUnit.sln index 5a9514ef..286f207c 100644 --- a/Logging.XUnit.sln +++ b/Logging.XUnit.sln @@ -10,7 +10,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .gitignore = .gitignore build.ps1 = build.ps1 CODE_OF_CONDUCT.md = CODE_OF_CONDUCT.md - CommonAssemblyInfo.cs = CommonAssemblyInfo.cs Directory.Build.props = Directory.Build.props Directory.Build.targets = Directory.Build.targets Directory.Packages.props = Directory.Packages.props diff --git a/README.md b/README.md index 29f42e30..fa3b79c6 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,6 @@ dotnet add package MartinCostello.Logging.XUnit ### Usage ```csharp -using System; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Xunit; @@ -31,26 +30,18 @@ using Xunit.Abstractions; namespace MyApp.Calculator; -public class CalculatorTests +public class CalculatorTests(ITestOutputHelper outputHelper) { - public CalculatorTests(ITestOutputHelper outputHelper) - { - OutputHelper = outputHelper; - } - - private ITestOutputHelper OutputHelper { get; } - [Fact] public void Calculator_Sums_Two_Integers() { // Arrange - var services = new ServiceCollection() - .AddLogging((builder) => builder.AddXUnit(OutputHelper)) - .AddSingleton(); + using var serviceProvider = new ServiceCollection() + .AddLogging((builder) => builder.AddXUnit(outputHelper)) + .AddSingleton() + .BuildServiceProvider(); - var calculator = services - .BuildServiceProvider() - .GetRequiredService(); + var calculator = services.GetRequiredService(); // Act int actual = calculator.Sum(1, 2); @@ -60,20 +51,13 @@ public class CalculatorTests } } -public sealed class Calculator +public sealed class Calculator(ILogger logger) { - private readonly ILogger _logger; - - public Calculator(ILogger logger) - { - _logger = logger; - } - public int Sum(int x, int y) { int sum = x + y; - _logger.LogInformation("The sum of {x} and {y} is {sum}.", x, y, sum); + logger.LogInformation("The sum of {x} and {y} is {sum}.", x, y, sum); return sum; } @@ -98,7 +82,7 @@ This project is licensed under the [Apache 2.0](http://www.apache.org/licenses/L ## Building and Testing -Compiling the library yourself requires Git and the [.NET SDK](https://www.microsoft.com/net/download/core "Download the .NET SDK") to be installed (version `7.0.100` or later). +Compiling the library yourself requires Git and the [.NET SDK](https://www.microsoft.com/net/download/core "Download the .NET SDK") to be installed (version `8.0.100` or later). To build and test the library locally from a terminal/command-line, run one of the following set of commands: diff --git a/build.ps1 b/build.ps1 index 58dd291d..9f548a33 100755 --- a/build.ps1 +++ b/build.ps1 @@ -4,8 +4,6 @@ #Requires -Version 7 param( - [Parameter(Mandatory = $false)][string] $Configuration = "Release", - [Parameter(Mandatory = $false)][string] $OutputPath = "", [Parameter(Mandatory = $false)][switch] $SkipTests ) @@ -27,10 +25,6 @@ $testProjects = @( $dotnetVersion = (Get-Content $sdkFile | Out-String | ConvertFrom-Json).sdk.version -if ($OutputPath -eq "") { - $OutputPath = Join-Path $PSScriptRoot "artifacts" -} - $installDotNetSdk = $false; if (($null -eq (Get-Command "dotnet" -ErrorAction SilentlyContinue)) -and ($null -eq (Get-Command "dotnet.exe" -ErrorAction SilentlyContinue))) { @@ -88,9 +82,7 @@ if ($installDotNetSdk -eq $true) { function DotNetPack { param([string]$Project) - $PackageOutputPath = (Join-Path $OutputPath "packages") - - & $dotnet pack $Project --output $PackageOutputPath --configuration $Configuration --include-symbols --include-source + & $dotnet pack $Project --include-symbols --include-source --tl if ($LASTEXITCODE -ne 0) { throw "dotnet pack failed with exit code $LASTEXITCODE" @@ -107,7 +99,7 @@ function DotNetTest { $additionalArgs += "GitHubActions;report-warnings=false" } - & $dotnet test $Project --output $OutputPath --configuration $Configuration $additionalArgs + & $dotnet test $Project --configuration "Release" --tl $additionalArgs if ($LASTEXITCODE -ne 0) { throw "dotnet test failed with exit code $LASTEXITCODE" diff --git a/global.json b/global.json index 0115ef04..33d26e56 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "7.0.403", + "version": "8.0.100", "allowPrerelease": false, "rollForward": "latestMajor" } diff --git a/src/Logging.XUnit/AmbientTestOutputHelperAccessor.cs b/src/Logging.XUnit/AmbientTestOutputHelperAccessor.cs index a3bdce45..86d7fce9 100644 --- a/src/Logging.XUnit/AmbientTestOutputHelperAccessor.cs +++ b/src/Logging.XUnit/AmbientTestOutputHelperAccessor.cs @@ -16,7 +16,6 @@ internal sealed class AmbientTestOutputHelperAccessor : ITestOutputHelperAccesso /// private static readonly AsyncLocal _current = new(); -#pragma warning disable CA1822 /// /// Gets or sets the current . /// @@ -25,5 +24,4 @@ public ITestOutputHelper? OutputHelper get { return _current.Value; } set { _current.Value = value; } } -#pragma warning restore CA1822 } diff --git a/src/Logging.XUnit/MessageSinkAccessor.cs b/src/Logging.XUnit/MessageSinkAccessor.cs index 4a25b939..f44c52a8 100644 --- a/src/Logging.XUnit/MessageSinkAccessor.cs +++ b/src/Logging.XUnit/MessageSinkAccessor.cs @@ -8,22 +8,17 @@ namespace MartinCostello.Logging.XUnit; /// /// A class representing the default implementation of . This class cannot be inherited. /// -internal sealed class MessageSinkAccessor : IMessageSinkAccessor +/// +/// Initializes a new instance of the class. +/// +/// The to use. +/// +/// is . +/// +internal sealed class MessageSinkAccessor(IMessageSink messageSink) : IMessageSinkAccessor { - /// - /// Initializes a new instance of the class. - /// - /// The to use. - /// - /// is . - /// - internal MessageSinkAccessor(IMessageSink messageSink) - { - MessageSink = messageSink ?? throw new ArgumentNullException(nameof(messageSink)); - } - /// /// Gets or sets the current . /// - public IMessageSink? MessageSink { get; set; } + public IMessageSink? MessageSink { get; set; } = messageSink ?? throw new ArgumentNullException(nameof(messageSink)); } diff --git a/src/Logging.XUnit/StringSyntaxAttribute.cs b/src/Logging.XUnit/StringSyntaxAttribute.cs index 82bcec7f..79390d75 100644 --- a/src/Logging.XUnit/StringSyntaxAttribute.cs +++ b/src/Logging.XUnit/StringSyntaxAttribute.cs @@ -11,24 +11,17 @@ namespace System.Diagnostics.CodeAnalysis; [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] [ExcludeFromCodeCoverage] -internal sealed class StringSyntaxAttribute : Attribute +internal sealed class StringSyntaxAttribute(string syntax, params object?[] arguments) : Attribute { public const string DateTimeFormat = nameof(DateTimeFormat); public StringSyntaxAttribute(string syntax) + : this(syntax, []) { - Syntax = syntax; - Arguments = Array.Empty(); } - public StringSyntaxAttribute(string syntax, params object?[] arguments) - { - Syntax = syntax; - Arguments = arguments; - } - - public string Syntax { get; } + public string Syntax { get; } = syntax; - public object?[] Arguments { get; } + public object?[] Arguments { get; } = arguments; } #endif diff --git a/src/Logging.XUnit/TestOutputHelperAccessor.cs b/src/Logging.XUnit/TestOutputHelperAccessor.cs index 837895b4..5f1c8d63 100644 --- a/src/Logging.XUnit/TestOutputHelperAccessor.cs +++ b/src/Logging.XUnit/TestOutputHelperAccessor.cs @@ -8,22 +8,17 @@ namespace MartinCostello.Logging.XUnit; /// /// A class representing the default implementation of . This class cannot be inherited. /// -internal sealed class TestOutputHelperAccessor : ITestOutputHelperAccessor +/// +/// Initializes a new instance of the class. +/// +/// The to use. +/// +/// is . +/// +internal sealed class TestOutputHelperAccessor(ITestOutputHelper outputHelper) : ITestOutputHelperAccessor { - /// - /// Initializes a new instance of the class. - /// - /// The to use. - /// - /// is . - /// - internal TestOutputHelperAccessor(ITestOutputHelper outputHelper) - { - OutputHelper = outputHelper ?? throw new ArgumentNullException(nameof(outputHelper)); - } - /// /// Gets or sets the current . /// - public ITestOutputHelper? OutputHelper { get; set; } + public ITestOutputHelper? OutputHelper { get; set; } = outputHelper ?? throw new ArgumentNullException(nameof(outputHelper)); } diff --git a/src/Logging.XUnit/XUnitLogScope.cs b/src/Logging.XUnit/XUnitLogScope.cs index edd39698..ba871cd3 100644 --- a/src/Logging.XUnit/XUnitLogScope.cs +++ b/src/Logging.XUnit/XUnitLogScope.cs @@ -6,26 +6,21 @@ namespace MartinCostello.Logging.XUnit; /// /// A class representing a scope for logging. This class cannot be inherited. /// -internal sealed class XUnitLogScope +/// +/// Initializes a new instance of the class. +/// +/// The state object for the scope. +internal sealed class XUnitLogScope(object state) { /// /// The scope for the current thread. /// private static readonly AsyncLocal _value = new(); - /// - /// Initializes a new instance of the class. - /// - /// The state object for the scope. - internal XUnitLogScope(object state) - { - State = state; - } - /// /// Gets the state object for the scope. /// - public object State { get; } + public object State { get; } = state; /// /// Gets or sets the current scope. diff --git a/src/Logging.XUnit/XUnitLogger.cs b/src/Logging.XUnit/XUnitLogger.cs index 17568f0c..e8b4c0e9 100644 --- a/src/Logging.XUnit/XUnitLogger.cs +++ b/src/Logging.XUnit/XUnitLogger.cs @@ -151,10 +151,7 @@ public virtual void WriteMessage(LogLevel logLevel, int eventId, string? message StringBuilder? logBuilder = _logBuilder; _logBuilder = null; - if (logBuilder == null) - { - logBuilder = new StringBuilder(); - } + logBuilder ??= new StringBuilder(); string logLevelString = GetLogLevelString(logLevel); @@ -202,10 +199,7 @@ public virtual void WriteMessage(LogLevel logLevel, int eventId, string? message try { - if (outputHelper != null) - { - outputHelper.WriteLine(line); - } + outputHelper?.WriteLine(line); if (messageSink != null) { diff --git a/src/Logging.XUnit/XUnitLoggerExtensions.IMessageSink.cs b/src/Logging.XUnit/XUnitLoggerExtensions.IMessageSink.cs index 969f038c..058dacfd 100644 --- a/src/Logging.XUnit/XUnitLoggerExtensions.IMessageSink.cs +++ b/src/Logging.XUnit/XUnitLoggerExtensions.IMessageSink.cs @@ -71,9 +71,7 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, IMessageSin configure(options); -#pragma warning disable CA2000 builder.AddProvider(new XUnitLoggerProvider(accessor, options)); -#pragma warning restore CA2000 builder.Services.TryAddSingleton(accessor); @@ -139,9 +137,7 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, IMessageSin configure(options); -#pragma warning disable CA2000 return builder.AddProvider(new XUnitLoggerProvider(messageSink, options)); -#pragma warning restore CA2000 } /// @@ -331,9 +327,7 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, IMessageSink var options = configure(); -#pragma warning disable CA2000 factory.AddProvider(new XUnitLoggerProvider(messageSink, options)); -#pragma warning restore CA2000 return factory; } diff --git a/src/Logging.XUnit/XUnitLoggerExtensions.ITestOutputHelper.cs b/src/Logging.XUnit/XUnitLoggerExtensions.ITestOutputHelper.cs index a95a423c..29df11bd 100644 --- a/src/Logging.XUnit/XUnitLoggerExtensions.ITestOutputHelper.cs +++ b/src/Logging.XUnit/XUnitLoggerExtensions.ITestOutputHelper.cs @@ -93,9 +93,7 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, ITestOutput configure(options); -#pragma warning disable CA2000 builder.AddProvider(new XUnitLoggerProvider(accessor, options)); -#pragma warning restore CA2000 builder.Services.TryAddSingleton(accessor); @@ -161,9 +159,7 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, ITestOutput configure(options); -#pragma warning disable CA2000 return builder.AddProvider(new XUnitLoggerProvider(outputHelper, options)); -#pragma warning restore CA2000 } /// @@ -353,9 +349,7 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHe var options = configure(); -#pragma warning disable CA2000 factory.AddProvider(new XUnitLoggerProvider(outputHelper, options)); -#pragma warning restore CA2000 return factory; } diff --git a/tests/Logging.XUnit.Tests/Examples.cs b/tests/Logging.XUnit.Tests/Examples.cs index 7603514d..c8bbf021 100644 --- a/tests/Logging.XUnit.Tests/Examples.cs +++ b/tests/Logging.XUnit.Tests/Examples.cs @@ -6,20 +6,13 @@ namespace MartinCostello.Logging.XUnit; -public class Examples +public class Examples(ITestOutputHelper outputHelper) { - public Examples(ITestOutputHelper outputHelper) - { - OutputHelper = outputHelper; - } - - private ITestOutputHelper OutputHelper { get; } - [Fact] public void Calculator_Sums_Two_Equal_Integers() { // Arrange using conversion to a logger - var calculator = new Calculator(OutputHelper.ToLogger()); + var calculator = new Calculator(outputHelper.ToLogger()); // Act int actual = calculator.Sum(2, 2); @@ -33,7 +26,7 @@ public void Calculator_Sums_Two_Different_Integers() { // Arrange using the logging provider var services = new ServiceCollection() - .AddLogging((builder) => builder.AddXUnit(OutputHelper)) + .AddLogging((builder) => builder.AddXUnit(outputHelper)) .AddSingleton(); IServiceProvider provider = services.BuildServiceProvider(); @@ -47,20 +40,13 @@ public void Calculator_Sums_Two_Different_Integers() actual.ShouldBe(3); } - private sealed class Calculator + private sealed class Calculator(ILogger logger) { - private readonly ILogger _logger; - - public Calculator(ILogger logger) - { - _logger = logger; - } - public int Sum(int x, int y) { int sum = x + y; - _logger.LogInformation("The sum of {X} and {Y} is {Sum}.", x, y, sum); + logger.LogInformation("The sum of {X} and {Y} is {Sum}.", x, y, sum); return sum; } diff --git a/tests/Logging.XUnit.Tests/Integration/DatabaseTests.cs b/tests/Logging.XUnit.Tests/Integration/DatabaseTests.cs index 645ca4fa..9a9bff77 100644 --- a/tests/Logging.XUnit.Tests/Integration/DatabaseTests.cs +++ b/tests/Logging.XUnit.Tests/Integration/DatabaseTests.cs @@ -3,14 +3,9 @@ namespace MartinCostello.Logging.XUnit.Integration; -public class DatabaseTests : IClassFixture +public class DatabaseTests(DatabaseFixture databaseFixture) : IClassFixture { - public DatabaseTests(DatabaseFixture databaseFixture) - { - DatabaseFixture = databaseFixture; - } - - public DatabaseFixture DatabaseFixture { get; } + public DatabaseFixture DatabaseFixture { get; } = databaseFixture; [Fact] public void Run_Database_Test() diff --git a/tests/Logging.XUnit.Tests/Integration/HttpServerFixture.cs b/tests/Logging.XUnit.Tests/Integration/HttpServerFixture.cs index 5418d0e5..cd3ba1d1 100644 --- a/tests/Logging.XUnit.Tests/Integration/HttpServerFixture.cs +++ b/tests/Logging.XUnit.Tests/Integration/HttpServerFixture.cs @@ -13,20 +13,10 @@ namespace MartinCostello.Logging.XUnit.Integration; /// public sealed class HttpServerFixture : WebApplicationFactory, ITestOutputHelperAccessor { - /// - /// Initializes a new instance of the class. - /// - public HttpServerFixture() - : base() - { - } - /// public ITestOutputHelper? OutputHelper { get; set; } /// protected override void ConfigureWebHost(IWebHostBuilder builder) - { - builder.ConfigureLogging((p) => p.AddXUnit(this)); - } + => builder.ConfigureLogging((p) => p.AddXUnit(this)); } diff --git a/tests/Logging.XUnit.Tests/Integration/PrintableDiagnosticMessage.cs b/tests/Logging.XUnit.Tests/Integration/PrintableDiagnosticMessage.cs index 0eb499d2..4c5d7831 100644 --- a/tests/Logging.XUnit.Tests/Integration/PrintableDiagnosticMessage.cs +++ b/tests/Logging.XUnit.Tests/Integration/PrintableDiagnosticMessage.cs @@ -8,12 +8,7 @@ namespace MartinCostello.Logging.XUnit.Integration; /// /// See https://github.com/xunit/xunit/pull/2148#issuecomment-839838421. /// -internal sealed class PrintableDiagnosticMessage : DiagnosticMessage +internal sealed class PrintableDiagnosticMessage(string message) : DiagnosticMessage(message) { - public PrintableDiagnosticMessage(string message) - : base(message) - { - } - public override string ToString() => Message; } diff --git a/tests/Logging.XUnit.Tests/MartinCostello.Logging.XUnit.Tests.csproj b/tests/Logging.XUnit.Tests/MartinCostello.Logging.XUnit.Tests.csproj index 77e2ff3b..0c404434 100644 --- a/tests/Logging.XUnit.Tests/MartinCostello.Logging.XUnit.Tests.csproj +++ b/tests/Logging.XUnit.Tests/MartinCostello.Logging.XUnit.Tests.csproj @@ -7,7 +7,7 @@ true MartinCostello.Logging.XUnit $(Description) - net7.0 + net8.0 diff --git a/tests/SampleApp/Program.cs b/tests/SampleApp/Program.cs index 37962ff6..83b5bb40 100644 --- a/tests/SampleApp/Program.cs +++ b/tests/SampleApp/Program.cs @@ -1,8 +1,6 @@ // Copyright (c) Martin Costello, 2018. All rights reserved. // Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. -#pragma warning disable CA1852 - var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); diff --git a/tests/SampleApp/SampleApp.csproj b/tests/SampleApp/SampleApp.csproj index 55704ea1..1e5b7408 100644 --- a/tests/SampleApp/SampleApp.csproj +++ b/tests/SampleApp/SampleApp.csproj @@ -2,6 +2,6 @@ false $(NoWarn);CA1801;CA1822;CA1861;SA1600 - net7.0 + net8.0