diff --git a/.vsconfig b/.vsconfig index 8114a325..f9ba009d 100644 --- a/.vsconfig +++ b/.vsconfig @@ -3,7 +3,7 @@ "components": [ "Microsoft.VisualStudio.Component.CoreEditor", "Microsoft.VisualStudio.Workload.CoreEditor", - "Microsoft.NetCore.Component.Runtime.9.0", + "Microsoft.NetCore.Component.Runtime.10.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 bdb3c864..27d84fe0 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -7,9 +7,9 @@ xunit;logging true true - 0.6.0.0 + 0.7.0.0 0.6.0 - 0.6.1 + 0.7.0 true diff --git a/Directory.Packages.props b/Directory.Packages.props index 2ee6e8af..b7ee46f2 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -8,9 +8,9 @@ - + - + diff --git a/global.json b/global.json index f23a6708..d93a357d 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "9.0.306", + "version": "10.0.100", "allowPrerelease": false, "rollForward": "latestMajor", "paths": [ ".dotnet", "$host$" ], diff --git a/src/Logging.XUnit.v3/MartinCostello.Logging.XUnit.v3.csproj b/src/Logging.XUnit.v3/MartinCostello.Logging.XUnit.v3.csproj index c3f3fa9a..bc13b03f 100644 --- a/src/Logging.XUnit.v3/MartinCostello.Logging.XUnit.v3.csproj +++ b/src/Logging.XUnit.v3/MartinCostello.Logging.XUnit.v3.csproj @@ -9,7 +9,7 @@ Library MartinCostello.Logging.XUnit.v3 MartinCostello.Logging.XUnit - net8.0;net472 + net10.0;net8.0;net472 xunit v3 Logging Extensions @@ -19,6 +19,9 @@ + + + diff --git a/src/Logging.XUnit/MartinCostello.Logging.XUnit.csproj b/src/Logging.XUnit/MartinCostello.Logging.XUnit.csproj index 5350e3d9..3f422e00 100644 --- a/src/Logging.XUnit/MartinCostello.Logging.XUnit.csproj +++ b/src/Logging.XUnit/MartinCostello.Logging.XUnit.csproj @@ -8,7 +8,7 @@ Library MartinCostello.Logging.XUnit MartinCostello.Logging.XUnit - netstandard2.0;net8.0 + net10.0;net8.0;netstandard2.0 xunit Logging Extensions @@ -19,6 +19,9 @@ + + + diff --git a/src/Shared/ArgumentNullExceptionExtensions.cs b/src/Shared/ArgumentNullExceptionExtensions.cs new file mode 100644 index 00000000..b89153e5 --- /dev/null +++ b/src/Shared/ArgumentNullExceptionExtensions.cs @@ -0,0 +1,29 @@ +// 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. + +#if NETFRAMEWORK || NETSTANDARD +using System.Runtime.CompilerServices; + +#pragma warning disable IDE0130 +namespace System; + +/// +/// Extension methods for . +/// +internal static class ArgumentNullExceptionExtensions +{ + extension(ArgumentNullException) + { + /// Throws an if is null. + /// The reference type argument to validate as non-null. + /// The name of the parameter with which corresponds. + public static void ThrowIfNull(object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) + { + if (argument is null) + { + throw new ArgumentNullException(paramName); + } + } + } +} +#endif diff --git a/src/Shared/CallerArgumentExpressionAttribute.cs b/src/Shared/CallerArgumentExpressionAttribute.cs new file mode 100644 index 00000000..6460291d --- /dev/null +++ b/src/Shared/CallerArgumentExpressionAttribute.cs @@ -0,0 +1,20 @@ +// 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. + +#if NETFRAMEWORK || NETSTANDARD +#pragma warning disable IDE0130 +namespace System.Runtime.CompilerServices; + +/// +/// Indicates that a parameter captures the expression passed for another parameter as a string. +/// +/// The name of the parameter whose expression should be captured as a string. +[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] +internal sealed class CallerArgumentExpressionAttribute(string parameterName) : Attribute +{ + /// + /// Gets the name of the parameter whose expression should be captured as a string. + /// + public string ParameterName { get; } = parameterName; +} +#endif diff --git a/src/Shared/IMessageSinkExtensions.cs b/src/Shared/IMessageSinkExtensions.cs index 08217734..4b5a1cdc 100644 --- a/src/Shared/IMessageSinkExtensions.cs +++ b/src/Shared/IMessageSinkExtensions.cs @@ -29,14 +29,7 @@ public static class IMessageSinkExtensions /// public static ILoggerFactory ToLoggerFactory(this IMessageSink messageSink) { -#if NET ArgumentNullException.ThrowIfNull(messageSink); -#else - if (messageSink == null) - { - throw new ArgumentNullException(nameof(messageSink)); - } -#endif return new LoggerFactory().AddXUnit(messageSink); } diff --git a/src/Shared/ITestOutputHelperExtensions.cs b/src/Shared/ITestOutputHelperExtensions.cs index 4b4f22a2..321b08b5 100644 --- a/src/Shared/ITestOutputHelperExtensions.cs +++ b/src/Shared/ITestOutputHelperExtensions.cs @@ -29,14 +29,7 @@ public static class ITestOutputHelperExtensions /// public static ILoggerFactory ToLoggerFactory(this ITestOutputHelper outputHelper) { -#if NET ArgumentNullException.ThrowIfNull(outputHelper); -#else - if (outputHelper == null) - { - throw new ArgumentNullException(nameof(outputHelper)); - } -#endif return new LoggerFactory().AddXUnit(outputHelper); } diff --git a/src/Shared/StringSyntaxAttribute.cs b/src/Shared/StringSyntaxAttribute.cs index b3dcc7c5..9d23769f 100644 --- a/src/Shared/StringSyntaxAttribute.cs +++ b/src/Shared/StringSyntaxAttribute.cs @@ -3,7 +3,7 @@ //// Based on https://github.com/dotnet/runtime/blob/65067052e433eda400c5e7cc9f7b21c84640f901/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/StringSyntaxAttribute.cs -#if NETSTANDARD || !NETCOREAPP +#if NETFRAMEWORK || NETSTANDARD #pragma warning disable IDE0130 #pragma warning disable SA1600 diff --git a/src/Shared/XUnitLogger.cs b/src/Shared/XUnitLogger.cs index e877a833..4ef452f7 100644 --- a/src/Shared/XUnitLogger.cs +++ b/src/Shared/XUnitLogger.cs @@ -105,14 +105,7 @@ private XUnitLogger(string name, XUnitLoggerOptions? options) public IDisposable? BeginScope(TState state) where TState : notnull { -#if NET ArgumentNullException.ThrowIfNull(state); -#else - if (state == null) - { - throw new ArgumentNullException(nameof(state)); - } -#endif return XUnitLogScope.Push(state); } @@ -136,14 +129,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except return; } -#if NET ArgumentNullException.ThrowIfNull(formatter); -#else - if (formatter == null) - { - throw new ArgumentNullException(nameof(formatter)); - } -#endif string? message = formatter(state, exception); diff --git a/src/Shared/XUnitLoggerExtensions.IMessageSink.cs b/src/Shared/XUnitLoggerExtensions.IMessageSink.cs index fa101e02..624ad1be 100644 --- a/src/Shared/XUnitLoggerExtensions.IMessageSink.cs +++ b/src/Shared/XUnitLoggerExtensions.IMessageSink.cs @@ -24,20 +24,8 @@ public static partial class XUnitLoggerExtensions /// public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, IMessageSinkAccessor accessor) { -#if NET ArgumentNullException.ThrowIfNull(builder); ArgumentNullException.ThrowIfNull(accessor); -#else - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (accessor == null) - { - throw new ArgumentNullException(nameof(accessor)); - } -#endif return builder.AddXUnit(accessor, static (_) => { }); } @@ -56,26 +44,9 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, IMessageSin /// public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, IMessageSinkAccessor accessor, Action configure) { -#if NET ArgumentNullException.ThrowIfNull(builder); ArgumentNullException.ThrowIfNull(accessor); ArgumentNullException.ThrowIfNull(configure); -#else - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (accessor == null) - { - throw new ArgumentNullException(nameof(accessor)); - } - - if (configure == null) - { - throw new ArgumentNullException(nameof(configure)); - } -#endif var options = new XUnitLoggerOptions(); @@ -103,20 +74,8 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, IMessageSin /// public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, IMessageSink messageSink) { -#if NET ArgumentNullException.ThrowIfNull(builder); ArgumentNullException.ThrowIfNull(messageSink); -#else - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (messageSink == null) - { - throw new ArgumentNullException(nameof(messageSink)); - } -#endif return builder.AddXUnit(messageSink, static (_) => { }); } @@ -135,26 +94,9 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, IMessageSin /// public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, IMessageSink messageSink, Action configure) { -#if NET ArgumentNullException.ThrowIfNull(builder); ArgumentNullException.ThrowIfNull(messageSink); ArgumentNullException.ThrowIfNull(configure); -#else - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (messageSink == null) - { - throw new ArgumentNullException(nameof(messageSink)); - } - - if (configure == null) - { - throw new ArgumentNullException(nameof(configure)); - } -#endif var options = new XUnitLoggerOptions(); @@ -179,20 +121,8 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, IMessageSin /// public static ILoggerFactory AddXUnit(this ILoggerFactory factory, IMessageSink messageSink, LogLevel minLevel) { -#if NET ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(messageSink); -#else - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (messageSink == null) - { - throw new ArgumentNullException(nameof(messageSink)); - } -#endif return factory.AddXUnit(messageSink, (_, level) => level >= minLevel); } @@ -211,26 +141,9 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, IMessageSink /// public static ILoggerFactory AddXUnit(this ILoggerFactory factory, IMessageSink messageSink, Func filter) { -#if NET ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(messageSink); ArgumentNullException.ThrowIfNull(filter); -#else - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (messageSink == null) - { - throw new ArgumentNullException(nameof(messageSink)); - } - - if (filter == null) - { - throw new ArgumentNullException(nameof(filter)); - } -#endif return factory.AddXUnit(messageSink, (options) => options.Filter = filter); } @@ -248,20 +161,8 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, IMessageSink /// public static ILoggerFactory AddXUnit(this ILoggerFactory factory, IMessageSink messageSink) { -#if NET ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(messageSink); -#else - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (messageSink == null) - { - throw new ArgumentNullException(nameof(messageSink)); - } -#endif return factory.AddXUnit(messageSink, static (_) => { }); } @@ -280,26 +181,9 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, IMessageSink /// public static ILoggerFactory AddXUnit(this ILoggerFactory factory, IMessageSink messageSink, XUnitLoggerOptions options) { -#if NET ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(messageSink); ArgumentNullException.ThrowIfNull(options); -#else - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (messageSink == null) - { - throw new ArgumentNullException(nameof(messageSink)); - } - - if (options == null) - { - throw new ArgumentNullException(nameof(options)); - } -#endif return factory.AddXUnit(messageSink, () => options); } @@ -318,26 +202,9 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, IMessageSink /// public static ILoggerFactory AddXUnit(this ILoggerFactory factory, IMessageSink messageSink, Action configure) { -#if NET ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(messageSink); ArgumentNullException.ThrowIfNull(configure); -#else - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (messageSink == null) - { - throw new ArgumentNullException(nameof(messageSink)); - } - - if (configure == null) - { - throw new ArgumentNullException(nameof(configure)); - } -#endif return factory.AddXUnit(messageSink, () => { @@ -361,26 +228,9 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, IMessageSink /// public static ILoggerFactory AddXUnit(this ILoggerFactory factory, IMessageSink messageSink, Func configure) { -#if NET ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(messageSink); ArgumentNullException.ThrowIfNull(configure); -#else - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (messageSink == null) - { - throw new ArgumentNullException(nameof(messageSink)); - } - - if (configure == null) - { - throw new ArgumentNullException(nameof(configure)); - } -#endif var options = configure(); diff --git a/src/Shared/XUnitLoggerExtensions.ITestOutputHelper.cs b/src/Shared/XUnitLoggerExtensions.ITestOutputHelper.cs index 4b92e11a..4ef1cd71 100644 --- a/src/Shared/XUnitLoggerExtensions.ITestOutputHelper.cs +++ b/src/Shared/XUnitLoggerExtensions.ITestOutputHelper.cs @@ -50,20 +50,8 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder) /// public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, ITestOutputHelperAccessor accessor) { -#if NET ArgumentNullException.ThrowIfNull(builder); ArgumentNullException.ThrowIfNull(accessor); -#else - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (accessor == null) - { - throw new ArgumentNullException(nameof(accessor)); - } -#endif return builder.AddXUnit(accessor, static (_) => { }); } @@ -82,26 +70,9 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, ITestOutput /// public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, ITestOutputHelperAccessor accessor, Action configure) { -#if NET ArgumentNullException.ThrowIfNull(builder); ArgumentNullException.ThrowIfNull(accessor); ArgumentNullException.ThrowIfNull(configure); -#else - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (accessor == null) - { - throw new ArgumentNullException(nameof(accessor)); - } - - if (configure == null) - { - throw new ArgumentNullException(nameof(configure)); - } -#endif var options = new XUnitLoggerOptions(); @@ -129,20 +100,8 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, ITestOutput /// public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, ITestOutputHelper outputHelper) { -#if NET ArgumentNullException.ThrowIfNull(builder); ArgumentNullException.ThrowIfNull(outputHelper); -#else - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (outputHelper == null) - { - throw new ArgumentNullException(nameof(outputHelper)); - } -#endif return builder.AddXUnit(outputHelper, static (_) => { }); } @@ -161,26 +120,9 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, ITestOutput /// public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, ITestOutputHelper outputHelper, Action configure) { -#if NET ArgumentNullException.ThrowIfNull(builder); ArgumentNullException.ThrowIfNull(outputHelper); ArgumentNullException.ThrowIfNull(configure); -#else - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (outputHelper == null) - { - throw new ArgumentNullException(nameof(outputHelper)); - } - - if (configure == null) - { - throw new ArgumentNullException(nameof(configure)); - } -#endif var options = new XUnitLoggerOptions(); @@ -205,20 +147,8 @@ public static ILoggingBuilder AddXUnit(this ILoggingBuilder builder, ITestOutput /// public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHelper outputHelper, LogLevel minLevel) { -#if NET ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(outputHelper); -#else - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (outputHelper == null) - { - throw new ArgumentNullException(nameof(outputHelper)); - } -#endif return factory.AddXUnit(outputHelper, (_, level) => level >= minLevel); } @@ -237,26 +167,9 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHe /// public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHelper outputHelper, Func filter) { -#if NET ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(outputHelper); ArgumentNullException.ThrowIfNull(filter); -#else - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (outputHelper == null) - { - throw new ArgumentNullException(nameof(outputHelper)); - } - - if (filter == null) - { - throw new ArgumentNullException(nameof(filter)); - } -#endif return factory.AddXUnit(outputHelper, (options) => options.Filter = filter); } @@ -274,20 +187,8 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHe /// public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHelper outputHelper) { -#if NET ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(outputHelper); -#else - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (outputHelper == null) - { - throw new ArgumentNullException(nameof(outputHelper)); - } -#endif return factory.AddXUnit(outputHelper, static (_) => { }); } @@ -306,26 +207,9 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHe /// public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHelper outputHelper, XUnitLoggerOptions options) { -#if NET ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(outputHelper); ArgumentNullException.ThrowIfNull(options); -#else - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (outputHelper == null) - { - throw new ArgumentNullException(nameof(outputHelper)); - } - - if (options == null) - { - throw new ArgumentNullException(nameof(options)); - } -#endif return factory.AddXUnit(outputHelper, () => options); } @@ -344,26 +228,9 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHe /// public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHelper outputHelper, Action configure) { -#if NET ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(outputHelper); ArgumentNullException.ThrowIfNull(configure); -#else - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (outputHelper == null) - { - throw new ArgumentNullException(nameof(outputHelper)); - } - - if (configure == null) - { - throw new ArgumentNullException(nameof(configure)); - } -#endif return factory.AddXUnit(outputHelper, () => { @@ -387,26 +254,9 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHe /// public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHelper outputHelper, Func configure) { -#if NET ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(outputHelper); ArgumentNullException.ThrowIfNull(configure); -#else - if (factory == null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (outputHelper == null) - { - throw new ArgumentNullException(nameof(outputHelper)); - } - - if (configure == null) - { - throw new ArgumentNullException(nameof(configure)); - } -#endif var options = configure(); diff --git a/tests/Logging.XUnit.Tests/MartinCostello.Logging.XUnit.Tests.csproj b/tests/Logging.XUnit.Tests/MartinCostello.Logging.XUnit.Tests.csproj index 64bcb80e..10cb8167 100644 --- a/tests/Logging.XUnit.Tests/MartinCostello.Logging.XUnit.Tests.csproj +++ b/tests/Logging.XUnit.Tests/MartinCostello.Logging.XUnit.Tests.csproj @@ -4,7 +4,7 @@ false $(NoWarn);CA1873 MartinCostello.Logging.XUnit - net9.0 + net10.0 true diff --git a/tests/Logging.XUnit.v3.Tests/MartinCostello.Logging.XUnit.v3.Tests.csproj b/tests/Logging.XUnit.v3.Tests/MartinCostello.Logging.XUnit.v3.Tests.csproj index 53186f9d..b01eada2 100644 --- a/tests/Logging.XUnit.v3.Tests/MartinCostello.Logging.XUnit.v3.Tests.csproj +++ b/tests/Logging.XUnit.v3.Tests/MartinCostello.Logging.XUnit.v3.Tests.csproj @@ -6,7 +6,7 @@ $(NoWarn);CA1873 Exe MartinCostello.Logging.XUnit - net9.0 + net10.0 true diff --git a/tests/SampleApp/SampleApp.csproj b/tests/SampleApp/SampleApp.csproj index 9335bc40..e543c3e5 100644 --- a/tests/SampleApp/SampleApp.csproj +++ b/tests/SampleApp/SampleApp.csproj @@ -2,7 +2,7 @@ false $(NoWarn);CA1801;CA1822;CA1861;SA1600;SA1601 - net9.0 + net10.0