未验证 提交 ea4ebaa3 编写于 作者: M Maksym Koshovyi 提交者: GitHub

Improve nullable annotations for `ILogger.BeginScope` (#66960)

* BeginScope can return a null IDisposable. And TState is not nullable.
上级 2724b7ef
......@@ -28,7 +28,7 @@ public partial interface IExternalScopeProvider
}
public partial interface ILogger
{
System.IDisposable BeginScope<TState>(TState state);
System.IDisposable? BeginScope<TState>(TState state) where TState : notnull;
bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel);
void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception? exception, System.Func<TState, System.Exception?, string> formatter);
}
......@@ -55,7 +55,7 @@ public partial class 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<Microsoft.Extensions.Logging.ILogger, System.Exception?> Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; }
public static System.Action<Microsoft.Extensions.Logging.ILogger, System.Exception?> Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, Microsoft.Extensions.Logging.LogDefineOptions? options) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, System.IDisposable> DefineScope(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, System.IDisposable> DefineScope<T1>(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, System.IDisposable> DefineScope<T1, T2>(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, System.IDisposable> DefineScope<T1, T2, T3>(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, System.IDisposable> DefineScope<T1, T2, T3, T4>(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, T5, System.IDisposable> DefineScope<T1, T2, T3, T4, T5>(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, T5, T6, System.IDisposable> DefineScope<T1, T2, T3, T4, T5, T6>(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, System.IDisposable?> DefineScope(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, System.IDisposable?> DefineScope<T1>(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, System.IDisposable?> DefineScope<T1, T2>(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, System.IDisposable?> DefineScope<T1, T2, T3>(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, System.IDisposable?> DefineScope<T1, T2, T3, T4>(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, T5, System.IDisposable?> DefineScope<T1, T2, T3, T4, T5>(string formatString) { throw null; }
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, T5, T6, System.IDisposable?> DefineScope<T1, T2, T3, T4, T5, T6>(string formatString) { throw null; }
public static System.Action<Microsoft.Extensions.Logging.ILogger, T1, System.Exception?> Define<T1>(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; }
public static System.Action<Microsoft.Extensions.Logging.ILogger, T1, System.Exception?> Define<T1>(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, Microsoft.Extensions.Logging.LogDefineOptions? options) { throw null; }
public static System.Action<Microsoft.Extensions.Logging.ILogger, T1, T2, System.Exception?> Define<T1, T2>(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; }
......@@ -134,7 +134,7 @@ public sealed partial class LoggerMessageAttribute : System.Attribute
public partial class Logger<T> : Microsoft.Extensions.Logging.ILogger, Microsoft.Extensions.Logging.ILogger<T>
{
public Logger(Microsoft.Extensions.Logging.ILoggerFactory factory) { }
System.IDisposable Microsoft.Extensions.Logging.ILogger.BeginScope<TState>(TState state) { throw null; }
System.IDisposable? Microsoft.Extensions.Logging.ILogger.BeginScope<TState>(TState state) { throw null; }
bool Microsoft.Extensions.Logging.ILogger.IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) { throw null; }
void Microsoft.Extensions.Logging.ILogger.Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception? exception, System.Func<TState, System.Exception?, string> 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>(TState state) { throw null; }
public System.IDisposable BeginScope<TState>(TState state) where TState : notnull { throw null; }
public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) { throw null; }
public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception? exception, System.Func<TState, System.Exception?, string> formatter) { }
}
......@@ -191,7 +191,7 @@ public partial class NullLogger<T> : Microsoft.Extensions.Logging.ILogger, Micro
{
public static readonly Microsoft.Extensions.Logging.Abstractions.NullLogger<T> Instance;
public NullLogger() { }
public System.IDisposable BeginScope<TState>(TState state) { throw null; }
public System.IDisposable BeginScope<TState>(TState state) where TState : notnull { throw null; }
public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) { throw null; }
public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception? exception, System.Func<TState, System.Exception?, string> formatter) { }
}
......
......@@ -35,6 +35,6 @@ public interface ILogger
/// <param name="state">The identifier for the scope.</param>
/// <typeparam name="TState">The type of the state to begin scope for.</typeparam>
/// <returns>An <see cref="IDisposable"/> that ends the logical operation scope on dispose.</returns>
IDisposable BeginScope<TState>(TState state);
IDisposable? BeginScope<TState>(TState state) where TState : notnull;
}
}
......@@ -402,7 +402,7 @@ public static void Log(this ILogger logger!!, LogLevel logLevel, EventId eventId
/// {
/// }
/// </example>
public static IDisposable BeginScope(
public static IDisposable? BeginScope(
this ILogger logger!!,
string messageFormat,
params object?[] args)
......
......@@ -18,7 +18,7 @@ public static class LoggerMessage
/// </summary>
/// <param name="formatString">The named format string</param>
/// <returns>A delegate which when invoked creates a log scope.</returns>
public static Func<ILogger, IDisposable> DefineScope(string formatString)
public static Func<ILogger, IDisposable?> DefineScope(string formatString)
{
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 0);
......@@ -33,7 +33,7 @@ public static class LoggerMessage
/// <typeparam name="T1">The type of the first parameter passed to the named format string.</typeparam>
/// <param name="formatString">The named format string</param>
/// <returns>A delegate which when invoked creates a log scope.</returns>
public static Func<ILogger, T1, IDisposable> DefineScope<T1>(string formatString)
public static Func<ILogger, T1, IDisposable?> DefineScope<T1>(string formatString)
{
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 1);
......@@ -47,7 +47,7 @@ public static class LoggerMessage
/// <typeparam name="T2">The type of the second parameter passed to the named format string.</typeparam>
/// <param name="formatString">The named format string</param>
/// <returns>A delegate which when invoked creates a log scope.</returns>
public static Func<ILogger, T1, T2, IDisposable> DefineScope<T1, T2>(string formatString)
public static Func<ILogger, T1, T2, IDisposable?> DefineScope<T1, T2>(string formatString)
{
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 2);
......@@ -62,7 +62,7 @@ public static class LoggerMessage
/// <typeparam name="T3">The type of the third parameter passed to the named format string.</typeparam>
/// <param name="formatString">The named format string</param>
/// <returns>A delegate which when invoked creates a log scope.</returns>
public static Func<ILogger, T1, T2, T3, IDisposable> DefineScope<T1, T2, T3>(string formatString)
public static Func<ILogger, T1, T2, T3, IDisposable?> DefineScope<T1, T2, T3>(string formatString)
{
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 3);
......@@ -78,7 +78,7 @@ public static class LoggerMessage
/// <typeparam name="T4">The type of the fourth parameter passed to the named format string.</typeparam>
/// <param name="formatString">The named format string</param>
/// <returns>A delegate which when invoked creates a log scope.</returns>
public static Func<ILogger, T1, T2, T3, T4, IDisposable> DefineScope<T1, T2, T3, T4>(string formatString)
public static Func<ILogger, T1, T2, T3, T4, IDisposable?> DefineScope<T1, T2, T3, T4>(string formatString)
{
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 4);
......@@ -95,7 +95,7 @@ public static class LoggerMessage
/// <typeparam name="T5">The type of the fifth parameter passed to the named format string.</typeparam>
/// <param name="formatString">The named format string</param>
/// <returns>A delegate which when invoked creates a log scope.</returns>
public static Func<ILogger, T1, T2, T3, T4, T5, IDisposable> DefineScope<T1, T2, T3, T4, T5>(string formatString)
public static Func<ILogger, T1, T2, T3, T4, T5, IDisposable?> DefineScope<T1, T2, T3, T4, T5>(string formatString)
{
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 5);
......@@ -113,7 +113,7 @@ public static class LoggerMessage
/// <typeparam name="T6">The type of the sixth parameter passed to the named format string.</typeparam>
/// <param name="formatString">The named format string</param>
/// <returns>A delegate which when invoked creates a log scope.</returns>
public static Func<ILogger, T1, T2, T3, T4, T5, T6, IDisposable> DefineScope<T1, T2, T3, T4, T5, T6>(string formatString)
public static Func<ILogger, T1, T2, T3, T4, T5, T6, IDisposable?> DefineScope<T1, T2, T3, T4, T5, T6>(string formatString)
{
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 6);
......
......@@ -25,7 +25,7 @@ public Logger(ILoggerFactory factory!!)
}
/// <inheritdoc />
IDisposable ILogger.BeginScope<TState>(TState state)
IDisposable? ILogger.BeginScope<TState>(TState state)
{
return _logger.BeginScope(state);
}
......
......@@ -20,7 +20,7 @@ private NullLogger()
}
/// <inheritdoc />
public IDisposable BeginScope<TState>(TState state)
public IDisposable BeginScope<TState>(TState state) where TState : notnull
{
return NullScope.Instance;
}
......
......@@ -17,7 +17,7 @@ public class NullLogger<T> : ILogger<T>
public static readonly NullLogger<T> Instance = new NullLogger<T>();
/// <inheritdoc />
public IDisposable BeginScope<TState>(TState state)
public IDisposable BeginScope<TState>(TState state) where TState : notnull
{
return NullScope.Instance;
}
......
......@@ -35,7 +35,7 @@ public MockLogger()
Reset();
}
public IDisposable BeginScope<TState>(TState state)
public IDisposable BeginScope<TState>(TState state) where TState : notnull
{
return new Disposable();
}
......
......@@ -111,7 +111,7 @@ static bool LoggerIsEnabled(LogLevel logLevel, ILogger logger, ref List<Exceptio
}
}
public IDisposable BeginScope<TState>(TState state)
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
ScopeLogger[]? loggers = ScopeLoggers;
......
......@@ -57,7 +57,7 @@ public ScopeLogger(ILogger? logger, IExternalScopeProvider? externalScopeProvide
public IExternalScopeProvider? ExternalScopeProvider { get; }
public IDisposable CreateScope<TState>(TState state)
public IDisposable? CreateScope<TState>(TState state) where TState : notnull
{
if (ExternalScopeProvider != null)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册