From ea4ebaa3c5162bcabc63284ba3b59aa683912af4 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 23 Mar 2022 20:35:24 +0200 Subject: [PATCH] Improve nullable annotations for `ILogger.BeginScope` (#66960) * BeginScope can return a null IDisposable. And TState is not nullable. --- ...crosoft.Extensions.Logging.Abstractions.cs | 24 +++++++++---------- .../src/ILogger.cs | 2 +- .../src/LoggerExtensions.cs | 2 +- .../src/LoggerMessage.cs | 14 +++++------ .../src/LoggerT.cs | 2 +- .../src/NullLogger.cs | 2 +- .../src/NullLoggerT.cs | 2 +- .../MockLogger.cs | 2 +- .../src/Logger.cs | 2 +- .../src/LoggerInformation.cs | 2 +- 10 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs index 33e79ca09252e..ee48f8f22bb39 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs @@ -28,7 +28,7 @@ public partial interface IExternalScopeProvider } public partial interface ILogger { - System.IDisposable BeginScope(TState state); + System.IDisposable? BeginScope(TState state) where TState : notnull; bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel); void Log(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception? exception, System.Func formatter); } @@ -55,7 +55,7 @@ public LogDefineOptions() { } } public static partial class LoggerExtensions { - public static System.IDisposable BeginScope(this Microsoft.Extensions.Logging.ILogger logger, string messageFormat, params object?[] args) { throw null; } + public static System.IDisposable? BeginScope(this Microsoft.Extensions.Logging.ILogger logger, string messageFormat, params object?[] args) { throw null; } public static void Log(this Microsoft.Extensions.Logging.ILogger logger, Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, System.Exception? exception, string? message, params object?[] args) { } public static void Log(this Microsoft.Extensions.Logging.ILogger logger, Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string? message, params object?[] args) { } public static void Log(this Microsoft.Extensions.Logging.ILogger logger, Microsoft.Extensions.Logging.LogLevel logLevel, System.Exception? exception, string? message, params object?[] args) { } @@ -100,13 +100,13 @@ public static partial class LoggerMessage { public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; } public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, Microsoft.Extensions.Logging.LogDefineOptions? options) { throw null; } - public static System.Func DefineScope(string formatString) { throw null; } - public static System.Func DefineScope(string formatString) { throw null; } - public static System.Func DefineScope(string formatString) { throw null; } - public static System.Func DefineScope(string formatString) { throw null; } - public static System.Func DefineScope(string formatString) { throw null; } - public static System.Func DefineScope(string formatString) { throw null; } - public static System.Func DefineScope(string formatString) { throw null; } + public static System.Func DefineScope(string formatString) { throw null; } + public static System.Func DefineScope(string formatString) { throw null; } + public static System.Func DefineScope(string formatString) { throw null; } + public static System.Func DefineScope(string formatString) { throw null; } + public static System.Func DefineScope(string formatString) { throw null; } + public static System.Func DefineScope(string formatString) { throw null; } + public static System.Func DefineScope(string formatString) { throw null; } public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; } public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, Microsoft.Extensions.Logging.LogDefineOptions? options) { throw null; } public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; } @@ -134,7 +134,7 @@ public LoggerMessageAttribute(int eventId, Microsoft.Extensions.Logging.LogLevel public partial class Logger : Microsoft.Extensions.Logging.ILogger, Microsoft.Extensions.Logging.ILogger { public Logger(Microsoft.Extensions.Logging.ILoggerFactory factory) { } - System.IDisposable Microsoft.Extensions.Logging.ILogger.BeginScope(TState state) { throw null; } + System.IDisposable? Microsoft.Extensions.Logging.ILogger.BeginScope(TState state) { throw null; } bool Microsoft.Extensions.Logging.ILogger.IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) { throw null; } void Microsoft.Extensions.Logging.ILogger.Log(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception? exception, System.Func formatter) { } } @@ -168,7 +168,7 @@ public partial class NullLogger : Microsoft.Extensions.Logging.ILogger { internal NullLogger() { } public static Microsoft.Extensions.Logging.Abstractions.NullLogger Instance { get { throw null; } } - public System.IDisposable BeginScope(TState state) { throw null; } + public System.IDisposable BeginScope(TState state) where TState : notnull { throw null; } public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) { throw null; } public void Log(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception? exception, System.Func formatter) { } } @@ -191,7 +191,7 @@ public partial class NullLogger : Microsoft.Extensions.Logging.ILogger, Micro { public static readonly Microsoft.Extensions.Logging.Abstractions.NullLogger Instance; public NullLogger() { } - public System.IDisposable BeginScope(TState state) { throw null; } + public System.IDisposable BeginScope(TState state) where TState : notnull { throw null; } public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) { throw null; } public void Log(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception? exception, System.Func formatter) { } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/ILogger.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/ILogger.cs index 15c7025827647..9c8370c834d3d 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/ILogger.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/ILogger.cs @@ -35,6 +35,6 @@ public interface ILogger /// The identifier for the scope. /// The type of the state to begin scope for. /// An that ends the logical operation scope on dispose. - IDisposable BeginScope(TState state); + IDisposable? BeginScope(TState state) where TState : notnull; } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerExtensions.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerExtensions.cs index 64c53da7275d1..f7cb48e4c498b 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerExtensions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerExtensions.cs @@ -402,7 +402,7 @@ public static void Log(this ILogger logger!!, LogLevel logLevel, EventId eventId /// { /// } /// - public static IDisposable BeginScope( + public static IDisposable? BeginScope( this ILogger logger!!, string messageFormat, params object?[] args) diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs index db1ffdfd1d89f..1f010e6647032 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs @@ -18,7 +18,7 @@ public static class LoggerMessage /// /// The named format string /// A delegate which when invoked creates a log scope. - public static Func DefineScope(string formatString) + public static Func DefineScope(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 0); @@ -33,7 +33,7 @@ public static Func DefineScope(string formatString) /// The type of the first parameter passed to the named format string. /// The named format string /// A delegate which when invoked creates a log scope. - public static Func DefineScope(string formatString) + public static Func DefineScope(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 1); @@ -47,7 +47,7 @@ public static Func DefineScope(string formatString /// The type of the second parameter passed to the named format string. /// The named format string /// A delegate which when invoked creates a log scope. - public static Func DefineScope(string formatString) + public static Func DefineScope(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 2); @@ -62,7 +62,7 @@ public static Func DefineScope(string form /// The type of the third parameter passed to the named format string. /// The named format string /// A delegate which when invoked creates a log scope. - public static Func DefineScope(string formatString) + public static Func DefineScope(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 3); @@ -78,7 +78,7 @@ public static Func DefineScope(str /// The type of the fourth parameter passed to the named format string. /// The named format string /// A delegate which when invoked creates a log scope. - public static Func DefineScope(string formatString) + public static Func DefineScope(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 4); @@ -95,7 +95,7 @@ public static Func DefineScopeThe type of the fifth parameter passed to the named format string. /// The named format string /// A delegate which when invoked creates a log scope. - public static Func DefineScope(string formatString) + public static Func DefineScope(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 5); @@ -113,7 +113,7 @@ public static Func DefineScopeThe type of the sixth parameter passed to the named format string. /// The named format string /// A delegate which when invoked creates a log scope. - public static Func DefineScope(string formatString) + public static Func DefineScope(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 6); diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerT.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerT.cs index 0502293631719..3b9a3442d0bb1 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerT.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerT.cs @@ -25,7 +25,7 @@ public Logger(ILoggerFactory factory!!) } /// - IDisposable ILogger.BeginScope(TState state) + IDisposable? ILogger.BeginScope(TState state) { return _logger.BeginScope(state); } diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/NullLogger.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/NullLogger.cs index 694ba8de9b1f6..991a383dc1ff7 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/NullLogger.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/NullLogger.cs @@ -20,7 +20,7 @@ private NullLogger() } /// - public IDisposable BeginScope(TState state) + public IDisposable BeginScope(TState state) where TState : notnull { return NullScope.Instance; } diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/NullLoggerT.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/NullLoggerT.cs index 1d228a33669f9..579b09208fe21 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/NullLoggerT.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/NullLoggerT.cs @@ -17,7 +17,7 @@ public class NullLogger : ILogger public static readonly NullLogger Instance = new NullLogger(); /// - public IDisposable BeginScope(TState state) + public IDisposable BeginScope(TState state) where TState : notnull { return NullScope.Instance; } diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/MockLogger.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/MockLogger.cs index 3fa88999b3ab5..3768bcc23757c 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/MockLogger.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/MockLogger.cs @@ -35,7 +35,7 @@ public MockLogger() Reset(); } - public IDisposable BeginScope(TState state) + public IDisposable BeginScope(TState state) where TState : notnull { return new Disposable(); } diff --git a/src/libraries/Microsoft.Extensions.Logging/src/Logger.cs b/src/libraries/Microsoft.Extensions.Logging/src/Logger.cs index 570fd396a9c76..8a069dfa88f64 100644 --- a/src/libraries/Microsoft.Extensions.Logging/src/Logger.cs +++ b/src/libraries/Microsoft.Extensions.Logging/src/Logger.cs @@ -111,7 +111,7 @@ static bool LoggerIsEnabled(LogLevel logLevel, ILogger logger, ref List(TState state) + public IDisposable? BeginScope(TState state) where TState : notnull { ScopeLogger[]? loggers = ScopeLoggers; diff --git a/src/libraries/Microsoft.Extensions.Logging/src/LoggerInformation.cs b/src/libraries/Microsoft.Extensions.Logging/src/LoggerInformation.cs index c7abeb7696d55..1a0a01acdcb3b 100644 --- a/src/libraries/Microsoft.Extensions.Logging/src/LoggerInformation.cs +++ b/src/libraries/Microsoft.Extensions.Logging/src/LoggerInformation.cs @@ -57,7 +57,7 @@ public ScopeLogger(ILogger? logger, IExternalScopeProvider? externalScopeProvide public IExternalScopeProvider? ExternalScopeProvider { get; } - public IDisposable CreateScope(TState state) + public IDisposable? CreateScope(TState state) where TState : notnull { if (ExternalScopeProvider != null) {