Skip to content

Commit bed8bc7

Browse files
[Group 7] Enable nullable annotations for Microsoft.Extensions.Logging.Console (#67459)
* First pass * DisallowNull * Update ctor for ConsoleLoggerProcessor * Update ctor for ConsoleLogger * formatters can be null * Update src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs Co-authored-by: Eric Erhardt <[email protected]> * Update src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs Co-authored-by: Eric Erhardt <[email protected]> * Remove `#nullable enable` Co-authored-by: Eric Erhardt <[email protected]>
1 parent 3b7303a commit bed8bc7

18 files changed

+97
-90
lines changed

src/libraries/Common/src/Extensions/Logging/NullExternalScopeProvider.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable enable
5-
64
using System;
75

86
namespace Microsoft.Extensions.Logging

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public readonly partial struct LogEntry<TState>
160160
public string Category { get { throw null; } }
161161
public Microsoft.Extensions.Logging.EventId EventId { get { throw null; } }
162162
public System.Exception? Exception { get { throw null; } }
163-
public System.Func<TState, System.Exception?, string>? Formatter { get { throw null; } }
163+
public System.Func<TState, System.Exception?, string> Formatter { get { throw null; } }
164164
public Microsoft.Extensions.Logging.LogLevel LogLevel { get { throw null; } }
165165
public TState State { get { throw null; } }
166166
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ public LogEntry(LogLevel logLevel, string category, EventId eventId, TState stat
5757
/// <summary>
5858
/// Gets the formatter
5959
/// </summary>
60-
public Func<TState, Exception?, string>? Formatter { get; }
60+
public Func<TState, Exception?, string> Formatter { get; }
6161
}
6262
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public abstract partial class ConsoleFormatter
2929
{
3030
protected ConsoleFormatter(string name) { }
3131
public string Name { get { throw null; } }
32-
public abstract void Write<TState>(in Microsoft.Extensions.Logging.Abstractions.LogEntry<TState> logEntry, Microsoft.Extensions.Logging.IExternalScopeProvider scopeProvider, System.IO.TextWriter textWriter);
32+
public abstract void Write<TState>(in Microsoft.Extensions.Logging.Abstractions.LogEntry<TState> logEntry, Microsoft.Extensions.Logging.IExternalScopeProvider? scopeProvider, System.IO.TextWriter textWriter);
3333
}
3434
public static partial class ConsoleFormatterNames
3535
{
@@ -42,7 +42,7 @@ public partial class ConsoleFormatterOptions
4242
public ConsoleFormatterOptions() { }
4343
public bool IncludeScopes { get { throw null; } set { } }
4444
[System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("DateTimeFormat")]
45-
public string TimestampFormat { get { throw null; } set { } }
45+
public string? TimestampFormat { get { throw null; } set { } }
4646
public bool UseUtcTimestamp { get { throw null; } set { } }
4747
}
4848
[System.ObsoleteAttribute("ConsoleLoggerFormat has been deprecated.")]
@@ -58,12 +58,12 @@ public ConsoleLoggerOptions() { }
5858
public bool DisableColors { get { throw null; } set { } }
5959
[System.ObsoleteAttribute("ConsoleLoggerOptions.Format has been deprecated. Use ConsoleLoggerOptions.FormatterName instead.")]
6060
public Microsoft.Extensions.Logging.Console.ConsoleLoggerFormat Format { get { throw null; } set { } }
61-
public string FormatterName { get { throw null; } set { } }
61+
public string? FormatterName { get { throw null; } set { } }
6262
[System.ObsoleteAttribute("ConsoleLoggerOptions.IncludeScopes has been deprecated. Use ConsoleFormatterOptions.IncludeScopes instead.")]
6363
public bool IncludeScopes { get { throw null; } set { } }
6464
public Microsoft.Extensions.Logging.LogLevel LogToStandardErrorThreshold { get { throw null; } set { } }
6565
[System.ObsoleteAttribute("ConsoleLoggerOptions.TimestampFormat has been deprecated. Use ConsoleFormatterOptions.TimestampFormat instead.")]
66-
public string TimestampFormat { get { throw null; } set { } }
66+
public string? TimestampFormat { get { throw null; } set { } }
6767
[System.ObsoleteAttribute("ConsoleLoggerOptions.UseUtcTimestamp has been deprecated. Use ConsoleFormatterOptions.UseUtcTimestamp instead.")]
6868
public bool UseUtcTimestamp { get { throw null; } set { } }
6969
}
@@ -72,7 +72,7 @@ public ConsoleLoggerOptions() { }
7272
public partial class ConsoleLoggerProvider : Microsoft.Extensions.Logging.ILoggerProvider, Microsoft.Extensions.Logging.ISupportExternalScope, System.IDisposable
7373
{
7474
public ConsoleLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions> options) { }
75-
public ConsoleLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions> options, System.Collections.Generic.IEnumerable<Microsoft.Extensions.Logging.Console.ConsoleFormatter> formatters) { }
75+
public ConsoleLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions> options, System.Collections.Generic.IEnumerable<Microsoft.Extensions.Logging.Console.ConsoleFormatter>? formatters) { }
7676
public Microsoft.Extensions.Logging.ILogger CreateLogger(string name) { throw null; }
7777
public void Dispose() { }
7878
public void SetScopeProvider(Microsoft.Extensions.Logging.IExternalScopeProvider scopeProvider) { }

src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
4+
<Nullable>enable</Nullable>
45
<IncludePlatformAttributes>true</IncludePlatformAttributes>
56
</PropertyGroup>
67

src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ protected ConsoleFormatter(string name!!)
3232
/// <param name="scopeProvider">The provider of scope data.</param>
3333
/// <param name="textWriter">The string writer embedding ansi code for colors.</param>
3434
/// <typeparam name="TState">The type of the object to be written.</typeparam>
35-
public abstract void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider scopeProvider, TextWriter textWriter);
35+
public abstract void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter);
3636
}
3737
}

src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatterOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public ConsoleFormatterOptions() { }
2323
/// Gets or sets format string used to format timestamp in logging messages. Defaults to <c>null</c>.
2424
/// </summary>
2525
[StringSyntax(StringSyntaxAttribute.DateTimeFormat)]
26-
public string TimestampFormat { get; set; }
26+
public string? TimestampFormat { get; set; }
2727

2828
/// <summary>
2929
/// Gets or sets indication whether or not UTC timezone should be used to format timestamps in logging messages. Defaults to <c>false</c>.

src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.IO;
67
using System.Runtime.Versioning;
78
using Microsoft.Extensions.Logging.Abstractions;
@@ -14,21 +15,28 @@ internal sealed class ConsoleLogger : ILogger
1415
private readonly string _name;
1516
private readonly ConsoleLoggerProcessor _queueProcessor;
1617

17-
internal ConsoleLogger(string name!!, ConsoleLoggerProcessor loggerProcessor)
18+
internal ConsoleLogger(
19+
string name!!,
20+
ConsoleLoggerProcessor loggerProcessor,
21+
ConsoleFormatter formatter,
22+
IExternalScopeProvider? scopeProvider,
23+
ConsoleLoggerOptions options)
1824
{
1925
_name = name;
2026
_queueProcessor = loggerProcessor;
27+
Formatter = formatter;
28+
ScopeProvider = scopeProvider;
29+
Options = options;
2130
}
2231

2332
internal ConsoleFormatter Formatter { get; set; }
24-
internal IExternalScopeProvider ScopeProvider { get; set; }
25-
33+
internal IExternalScopeProvider? ScopeProvider { get; set; }
2634
internal ConsoleLoggerOptions Options { get; set; }
2735

2836
[ThreadStatic]
29-
private static StringWriter t_stringWriter;
37+
private static StringWriter? t_stringWriter;
3038

31-
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
39+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
3240
{
3341
if (!IsEnabled(logLevel))
3442
{
@@ -61,6 +69,6 @@ public bool IsEnabled(LogLevel logLevel)
6169
return logLevel != LogLevel.None;
6270
}
6371

64-
public IDisposable BeginScope<TState>(TState state) => ScopeProvider?.Push(state) ?? NullScope.Instance;
72+
public IDisposable BeginScope<TState>(TState state) where TState : notnull => ScopeProvider?.Push(state) ?? NullScope.Instance;
6573
}
6674
}

src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public ConsoleLoggerFormat Format
3939
/// <summary>
4040
/// Name of the log message formatter to use. Defaults to "simple" />.
4141
/// </summary>
42-
public string FormatterName { get; set; }
42+
public string? FormatterName { get; set; }
4343

4444
/// <summary>
4545
/// Includes scopes when <see langword="true" />.
@@ -56,7 +56,7 @@ public ConsoleLoggerFormat Format
5656
/// Gets or sets format string used to format timestamp in logging messages. Defaults to <c>null</c>.
5757
/// </summary>
5858
[System.ObsoleteAttribute("ConsoleLoggerOptions.TimestampFormat has been deprecated. Use ConsoleFormatterOptions.TimestampFormat instead.")]
59-
public string TimestampFormat { get; set; }
59+
public string? TimestampFormat { get; set; }
6060

6161
/// <summary>
6262
/// Gets or sets indication whether or not UTC timezone should be used to format timestamps in logging messages. Defaults to <c>false</c>.

src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Concurrent;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Runtime.Versioning;
78
using System.Threading;
89

@@ -16,11 +17,13 @@ internal class ConsoleLoggerProcessor : IDisposable
1617
private readonly BlockingCollection<LogMessageEntry> _messageQueue = new BlockingCollection<LogMessageEntry>(_maxQueuedMessages);
1718
private readonly Thread _outputThread;
1819

19-
public IConsole Console;
20-
public IConsole ErrorConsole;
20+
public IConsole Console { get; }
21+
public IConsole ErrorConsole { get; }
2122

22-
public ConsoleLoggerProcessor()
23+
public ConsoleLoggerProcessor(IConsole console, IConsole errorConsole)
2324
{
25+
Console = console;
26+
ErrorConsole = errorConsole;
2427
// Start Console message queue processor
2528
_outputThread = new Thread(ProcessLogQueue)
2629
{

0 commit comments

Comments
 (0)