Skip to content

Commit ea4ebaa

Browse files
authored
Improve nullable annotations for ILogger.BeginScope (#66960)
* BeginScope can return a null IDisposable. And TState is not nullable.
1 parent 2724b7e commit ea4ebaa

File tree

10 files changed

+27
-27
lines changed

10 files changed

+27
-27
lines changed

src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public partial interface IExternalScopeProvider
2828
}
2929
public partial interface ILogger
3030
{
31-
System.IDisposable BeginScope<TState>(TState state);
31+
System.IDisposable? BeginScope<TState>(TState state) where TState : notnull;
3232
bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel);
3333
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);
3434
}
@@ -55,7 +55,7 @@ public LogDefineOptions() { }
5555
}
5656
public static partial class LoggerExtensions
5757
{
58-
public static System.IDisposable BeginScope(this Microsoft.Extensions.Logging.ILogger logger, string messageFormat, params object?[] args) { throw null; }
58+
public static System.IDisposable? BeginScope(this Microsoft.Extensions.Logging.ILogger logger, string messageFormat, params object?[] args) { throw null; }
5959
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) { }
6060
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) { }
6161
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
100100
{
101101
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; }
102102
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; }
103-
public static System.Func<Microsoft.Extensions.Logging.ILogger, System.IDisposable> DefineScope(string formatString) { throw null; }
104-
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, System.IDisposable> DefineScope<T1>(string formatString) { throw null; }
105-
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, System.IDisposable> DefineScope<T1, T2>(string formatString) { throw null; }
106-
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, System.IDisposable> DefineScope<T1, T2, T3>(string formatString) { throw null; }
107-
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, System.IDisposable> DefineScope<T1, T2, T3, T4>(string formatString) { throw null; }
108-
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; }
109-
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; }
103+
public static System.Func<Microsoft.Extensions.Logging.ILogger, System.IDisposable?> DefineScope(string formatString) { throw null; }
104+
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, System.IDisposable?> DefineScope<T1>(string formatString) { throw null; }
105+
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, System.IDisposable?> DefineScope<T1, T2>(string formatString) { throw null; }
106+
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, System.IDisposable?> DefineScope<T1, T2, T3>(string formatString) { throw null; }
107+
public static System.Func<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, System.IDisposable?> DefineScope<T1, T2, T3, T4>(string formatString) { throw null; }
108+
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; }
109+
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; }
110110
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; }
111111
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; }
112112
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 LoggerMessageAttribute(int eventId, Microsoft.Extensions.Logging.LogLevel
134134
public partial class Logger<T> : Microsoft.Extensions.Logging.ILogger, Microsoft.Extensions.Logging.ILogger<T>
135135
{
136136
public Logger(Microsoft.Extensions.Logging.ILoggerFactory factory) { }
137-
System.IDisposable Microsoft.Extensions.Logging.ILogger.BeginScope<TState>(TState state) { throw null; }
137+
System.IDisposable? Microsoft.Extensions.Logging.ILogger.BeginScope<TState>(TState state) { throw null; }
138138
bool Microsoft.Extensions.Logging.ILogger.IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) { throw null; }
139139
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) { }
140140
}
@@ -168,7 +168,7 @@ public partial class NullLogger : Microsoft.Extensions.Logging.ILogger
168168
{
169169
internal NullLogger() { }
170170
public static Microsoft.Extensions.Logging.Abstractions.NullLogger Instance { get { throw null; } }
171-
public System.IDisposable BeginScope<TState>(TState state) { throw null; }
171+
public System.IDisposable BeginScope<TState>(TState state) where TState : notnull { throw null; }
172172
public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) { throw null; }
173173
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) { }
174174
}
@@ -191,7 +191,7 @@ public partial class NullLogger<T> : Microsoft.Extensions.Logging.ILogger, Micro
191191
{
192192
public static readonly Microsoft.Extensions.Logging.Abstractions.NullLogger<T> Instance;
193193
public NullLogger() { }
194-
public System.IDisposable BeginScope<TState>(TState state) { throw null; }
194+
public System.IDisposable BeginScope<TState>(TState state) where TState : notnull { throw null; }
195195
public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) { throw null; }
196196
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) { }
197197
}

src/libraries/Microsoft.Extensions.Logging.Abstractions/src/ILogger.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public interface ILogger
3535
/// <param name="state">The identifier for the scope.</param>
3636
/// <typeparam name="TState">The type of the state to begin scope for.</typeparam>
3737
/// <returns>An <see cref="IDisposable"/> that ends the logical operation scope on dispose.</returns>
38-
IDisposable BeginScope<TState>(TState state);
38+
IDisposable? BeginScope<TState>(TState state) where TState : notnull;
3939
}
4040
}

src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ public static void Log(this ILogger logger!!, LogLevel logLevel, EventId eventId
402402
/// {
403403
/// }
404404
/// </example>
405-
public static IDisposable BeginScope(
405+
public static IDisposable? BeginScope(
406406
this ILogger logger!!,
407407
string messageFormat,
408408
params object?[] args)

src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class LoggerMessage
1818
/// </summary>
1919
/// <param name="formatString">The named format string</param>
2020
/// <returns>A delegate which when invoked creates a log scope.</returns>
21-
public static Func<ILogger, IDisposable> DefineScope(string formatString)
21+
public static Func<ILogger, IDisposable?> DefineScope(string formatString)
2222
{
2323
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 0);
2424

@@ -33,7 +33,7 @@ public static Func<ILogger, IDisposable> DefineScope(string formatString)
3333
/// <typeparam name="T1">The type of the first parameter passed to the named format string.</typeparam>
3434
/// <param name="formatString">The named format string</param>
3535
/// <returns>A delegate which when invoked creates a log scope.</returns>
36-
public static Func<ILogger, T1, IDisposable> DefineScope<T1>(string formatString)
36+
public static Func<ILogger, T1, IDisposable?> DefineScope<T1>(string formatString)
3737
{
3838
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 1);
3939

@@ -47,7 +47,7 @@ public static Func<ILogger, T1, IDisposable> DefineScope<T1>(string formatString
4747
/// <typeparam name="T2">The type of the second parameter passed to the named format string.</typeparam>
4848
/// <param name="formatString">The named format string</param>
4949
/// <returns>A delegate which when invoked creates a log scope.</returns>
50-
public static Func<ILogger, T1, T2, IDisposable> DefineScope<T1, T2>(string formatString)
50+
public static Func<ILogger, T1, T2, IDisposable?> DefineScope<T1, T2>(string formatString)
5151
{
5252
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 2);
5353

@@ -62,7 +62,7 @@ public static Func<ILogger, T1, T2, IDisposable> DefineScope<T1, T2>(string form
6262
/// <typeparam name="T3">The type of the third parameter passed to the named format string.</typeparam>
6363
/// <param name="formatString">The named format string</param>
6464
/// <returns>A delegate which when invoked creates a log scope.</returns>
65-
public static Func<ILogger, T1, T2, T3, IDisposable> DefineScope<T1, T2, T3>(string formatString)
65+
public static Func<ILogger, T1, T2, T3, IDisposable?> DefineScope<T1, T2, T3>(string formatString)
6666
{
6767
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 3);
6868

@@ -78,7 +78,7 @@ public static Func<ILogger, T1, T2, T3, IDisposable> DefineScope<T1, T2, T3>(str
7878
/// <typeparam name="T4">The type of the fourth parameter passed to the named format string.</typeparam>
7979
/// <param name="formatString">The named format string</param>
8080
/// <returns>A delegate which when invoked creates a log scope.</returns>
81-
public static Func<ILogger, T1, T2, T3, T4, IDisposable> DefineScope<T1, T2, T3, T4>(string formatString)
81+
public static Func<ILogger, T1, T2, T3, T4, IDisposable?> DefineScope<T1, T2, T3, T4>(string formatString)
8282
{
8383
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 4);
8484

@@ -95,7 +95,7 @@ public static Func<ILogger, T1, T2, T3, T4, IDisposable> DefineScope<T1, T2, T3,
9595
/// <typeparam name="T5">The type of the fifth parameter passed to the named format string.</typeparam>
9696
/// <param name="formatString">The named format string</param>
9797
/// <returns>A delegate which when invoked creates a log scope.</returns>
98-
public static Func<ILogger, T1, T2, T3, T4, T5, IDisposable> DefineScope<T1, T2, T3, T4, T5>(string formatString)
98+
public static Func<ILogger, T1, T2, T3, T4, T5, IDisposable?> DefineScope<T1, T2, T3, T4, T5>(string formatString)
9999
{
100100
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 5);
101101

@@ -113,7 +113,7 @@ public static Func<ILogger, T1, T2, T3, T4, T5, IDisposable> DefineScope<T1, T2,
113113
/// <typeparam name="T6">The type of the sixth parameter passed to the named format string.</typeparam>
114114
/// <param name="formatString">The named format string</param>
115115
/// <returns>A delegate which when invoked creates a log scope.</returns>
116-
public static Func<ILogger, T1, T2, T3, T4, T5, T6, IDisposable> DefineScope<T1, T2, T3, T4, T5, T6>(string formatString)
116+
public static Func<ILogger, T1, T2, T3, T4, T5, T6, IDisposable?> DefineScope<T1, T2, T3, T4, T5, T6>(string formatString)
117117
{
118118
LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 6);
119119

src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerT.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Logger(ILoggerFactory factory!!)
2525
}
2626

2727
/// <inheritdoc />
28-
IDisposable ILogger.BeginScope<TState>(TState state)
28+
IDisposable? ILogger.BeginScope<TState>(TState state)
2929
{
3030
return _logger.BeginScope(state);
3131
}

src/libraries/Microsoft.Extensions.Logging.Abstractions/src/NullLogger.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private NullLogger()
2020
}
2121

2222
/// <inheritdoc />
23-
public IDisposable BeginScope<TState>(TState state)
23+
public IDisposable BeginScope<TState>(TState state) where TState : notnull
2424
{
2525
return NullScope.Instance;
2626
}

src/libraries/Microsoft.Extensions.Logging.Abstractions/src/NullLoggerT.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class NullLogger<T> : ILogger<T>
1717
public static readonly NullLogger<T> Instance = new NullLogger<T>();
1818

1919
/// <inheritdoc />
20-
public IDisposable BeginScope<TState>(TState state)
20+
public IDisposable BeginScope<TState>(TState state) where TState : notnull
2121
{
2222
return NullScope.Instance;
2323
}

src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/MockLogger.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public MockLogger()
3535
Reset();
3636
}
3737

38-
public IDisposable BeginScope<TState>(TState state)
38+
public IDisposable BeginScope<TState>(TState state) where TState : notnull
3939
{
4040
return new Disposable();
4141
}

src/libraries/Microsoft.Extensions.Logging/src/Logger.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static bool LoggerIsEnabled(LogLevel logLevel, ILogger logger, ref List<Exceptio
111111
}
112112
}
113113

114-
public IDisposable BeginScope<TState>(TState state)
114+
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
115115
{
116116
ScopeLogger[]? loggers = ScopeLoggers;
117117

src/libraries/Microsoft.Extensions.Logging/src/LoggerInformation.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public ScopeLogger(ILogger? logger, IExternalScopeProvider? externalScopeProvide
5757

5858
public IExternalScopeProvider? ExternalScopeProvider { get; }
5959

60-
public IDisposable CreateScope<TState>(TState state)
60+
public IDisposable? CreateScope<TState>(TState state) where TState : notnull
6161
{
6262
if (ExternalScopeProvider != null)
6363
{

0 commit comments

Comments
 (0)