Skip to content

Commit

Permalink
Improve nullable annotations for ILogger.BeginScope (dotnet#66960)
Browse files Browse the repository at this point in the history
* BeginScope can return a null IDisposable. And TState is not nullable.
  • Loading branch information
maxkoshevoi authored and radekdoulik committed Mar 30, 2022
1 parent 0749f84 commit 837cb78
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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) { }
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -134,7 +134,7 @@ public LoggerMessageAttribute(int eventId, Microsoft.Extensions.Logging.LogLevel
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) { }
}
Expand Down Expand Up @@ -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) { }
}
Expand All @@ -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) { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -33,7 +33,7 @@ public static Func<ILogger, IDisposable> DefineScope(string formatString)
/// <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);

Expand All @@ -47,7 +47,7 @@ public static Func<ILogger, T1, IDisposable> DefineScope<T1>(string formatString
/// <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);

Expand All @@ -62,7 +62,7 @@ public static Func<ILogger, T1, T2, IDisposable> DefineScope<T1, T2>(string form
/// <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);

Expand All @@ -78,7 +78,7 @@ public static Func<ILogger, T1, T2, T3, IDisposable> DefineScope<T1, T2, T3>(str
/// <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);

Expand All @@ -95,7 +95,7 @@ public static Func<ILogger, T1, T2, T3, T4, IDisposable> DefineScope<T1, T2, T3,
/// <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);

Expand All @@ -113,7 +113,7 @@ public static Func<ILogger, T1, T2, T3, T4, T5, IDisposable> DefineScope<T1, T2,
/// <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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Microsoft.Extensions.Logging/src/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 837cb78

Please sign in to comment.