Skip to content

Commit 12c5c7c

Browse files
RenderMichaelsandeepsuryaprasad
authored andcommitted
[dotnet] Annotate nullable reference types in internal logging (SeleniumHQ#14819)
1 parent 8009d93 commit 12c5c7c

File tree

13 files changed

+51
-45
lines changed

13 files changed

+51
-45
lines changed

dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
using System;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.Internal.Logging
2325
{
2426
/// <summary>

dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using System;
2121
using System.IO;
2222

23+
#nullable enable
24+
2325
namespace OpenQA.Selenium.Internal.Logging
2426
{
2527
/// <summary>
@@ -40,6 +42,7 @@ public class FileLogHandler : ILogHandler, IDisposable
4042
/// Initializes a new instance of the <see cref="FileLogHandler"/> class with the specified file path.
4143
/// </summary>
4244
/// <param name="filePath">The path of the log file.</param>
45+
/// <exception cref="ArgumentException">If <paramref name="filePath"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
4346
public FileLogHandler(string filePath)
4447
: this(filePath, overwrite: true)
4548
{
@@ -51,6 +54,7 @@ public FileLogHandler(string filePath)
5154
/// </summary>
5255
/// <param name="filePath">The path of the log file.</param>
5356
/// <param name="overwrite">Specifies whether the file should be overwritten if it exists on the disk.</param>
57+
/// <exception cref="ArgumentException">If <paramref name="filePath"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
5458
public FileLogHandler(string filePath, bool overwrite)
5559
{
5660
if (string.IsNullOrEmpty(filePath)) throw new ArgumentException("File log path cannot be null or empty.", nameof(filePath));
@@ -112,9 +116,9 @@ protected virtual void Dispose(bool disposing)
112116
if (disposing)
113117
{
114118
_streamWriter?.Dispose();
115-
_streamWriter = null;
119+
_streamWriter = null!;
116120
_fileStream?.Dispose();
117-
_fileStream = null;
121+
_fileStream = null!;
118122
}
119123

120124
_isDisposed = true;

dotnet/src/webdriver/Internal/Logging/ILogContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
using System;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.Internal.Logging
2325
{
2426
/// <summary>

dotnet/src/webdriver/Internal/Logging/ILogHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// under the License.
1818
// </copyright>
1919

20+
#nullable enable
21+
2022
namespace OpenQA.Selenium.Internal.Logging
2123
{
2224
/// <summary>

dotnet/src/webdriver/Internal/Logging/ILogHandlerList.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
using System.Collections.Generic;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.Internal.Logging
2325
{
2426
/// <summary>

dotnet/src/webdriver/Internal/Logging/ILogger.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
using System;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.Internal.Logging
2325
{
2426
/// <summary>

dotnet/src/webdriver/Internal/Logging/Log.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
// </copyright>
1919

2020
using System;
21+
using System.Diagnostics.CodeAnalysis;
22+
23+
#nullable enable
2124

2225
namespace OpenQA.Selenium.Internal.Logging
2326
{
@@ -65,16 +68,11 @@ public static ILogContext CreateContext(LogEventLevel minimumLevel)
6568
/// <summary>
6669
/// Gets or sets the current log context.
6770
/// </summary>
71+
[AllowNull]
6872
internal static ILogContext CurrentContext
6973
{
70-
get
71-
{
72-
return _logContextManager.CurrentContext;
73-
}
74-
set
75-
{
76-
_logContextManager.CurrentContext = value;
77-
}
74+
get => _logContextManager.CurrentContext;
75+
set => _logContextManager.CurrentContext = value;
7876
}
7977

8078
/// <summary>

dotnet/src/webdriver/Internal/Logging/LogContext.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
using System.Collections.Generic;
2323
using System.Linq;
2424

25+
#nullable enable
26+
2527
namespace OpenQA.Selenium.Internal.Logging
2628
{
2729
/// <summary>
@@ -30,15 +32,15 @@ namespace OpenQA.Selenium.Internal.Logging
3032
/// <inheritdoc cref="ILogContext"/>
3133
internal class LogContext : ILogContext
3234
{
33-
private ConcurrentDictionary<Type, ILogger> _loggers;
35+
private ConcurrentDictionary<Type, ILogger>? _loggers;
3436

3537
private LogEventLevel _level;
3638

37-
private readonly ILogContext _parentLogContext;
39+
private readonly ILogContext? _parentLogContext;
3840

3941
private readonly Lazy<LogHandlerList> _lazyLogHandlerList;
4042

41-
public LogContext(LogEventLevel level, ILogContext parentLogContext, ConcurrentDictionary<Type, ILogger> loggers, IEnumerable<ILogHandler> handlers)
43+
public LogContext(LogEventLevel level, ILogContext? parentLogContext, ConcurrentDictionary<Type, ILogger>? loggers, IEnumerable<ILogHandler>? handlers)
4244
{
4345
_level = level;
4446

@@ -63,7 +65,7 @@ public ILogContext CreateContext()
6365

6466
public ILogContext CreateContext(LogEventLevel minimumLevel)
6567
{
66-
ConcurrentDictionary<Type, ILogger> loggers = null;
68+
ConcurrentDictionary<Type, ILogger>? loggers = null;
6769

6870
if (_loggers != null)
6971
{
@@ -89,12 +91,9 @@ public ILogger GetLogger(Type type)
8991
throw new ArgumentNullException(nameof(type));
9092
}
9193

92-
if (_loggers is null)
93-
{
94-
_loggers = new ConcurrentDictionary<Type, ILogger>();
95-
}
94+
_loggers ??= new ConcurrentDictionary<Type, ILogger>();
9695

97-
return _loggers.GetOrAdd(type, _ => new Logger(type, _level));
96+
return _loggers.GetOrAdd(type, type => new Logger(type, _level));
9897
}
9998

10099
public bool IsEnabled(ILogger logger, LogEventLevel level)

dotnet/src/webdriver/Internal/Logging/LogContextManager.cs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,31 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using System.Diagnostics.CodeAnalysis;
2021
using System.Threading;
2122

23+
#nullable enable
24+
2225
namespace OpenQA.Selenium.Internal.Logging
2326
{
2427
internal class LogContextManager
2528
{
26-
private readonly ILogContext _globalLogContext;
27-
28-
private readonly AsyncLocal<ILogContext> _currentAmbientLogContext = new AsyncLocal<ILogContext>();
29+
private readonly AsyncLocal<ILogContext?> _currentAmbientLogContext = new AsyncLocal<ILogContext?>();
2930

3031
public LogContextManager()
3132
{
3233
var defaulConsoleLogHandler = new ConsoleLogHandler();
3334

34-
_globalLogContext = new LogContext(LogEventLevel.Info, null, null, new[] { defaulConsoleLogHandler });
35+
GlobalContext = new LogContext(LogEventLevel.Info, null, null, new[] { defaulConsoleLogHandler });
3536
}
3637

37-
public ILogContext GlobalContext
38-
{
39-
get { return _globalLogContext; }
40-
}
38+
public ILogContext GlobalContext { get; }
4139

40+
[AllowNull]
4241
public ILogContext CurrentContext
4342
{
44-
get
45-
{
46-
if (_currentAmbientLogContext.Value is null)
47-
{
48-
return _globalLogContext;
49-
}
50-
else
51-
{
52-
return _currentAmbientLogContext.Value;
53-
}
54-
}
55-
set
56-
{
57-
_currentAmbientLogContext.Value = value;
58-
}
43+
get => _currentAmbientLogContext.Value ?? GlobalContext;
44+
set => _currentAmbientLogContext.Value = value;
5945
}
6046
}
6147
}

dotnet/src/webdriver/Internal/Logging/LogEvent.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
using System;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.Internal.Logging
2325
{
2426
/// <summary>
@@ -33,9 +35,10 @@ public sealed class LogEvent
3335
/// <param name="timestamp">The timestamp of the log event.</param>
3436
/// <param name="level">The level of the log event.</param>
3537
/// <param name="message">The message of the log event.</param>
38+
/// <exception cref="ArgumentNullException">If <paramref name="issuedBy"/> is <see langword="null"/>.</exception>
3639
public LogEvent(Type issuedBy, DateTimeOffset timestamp, LogEventLevel level, string message)
3740
{
38-
IssuedBy = issuedBy;
41+
IssuedBy = issuedBy ?? throw new ArgumentNullException(nameof(issuedBy));
3942
Timestamp = timestamp;
4043
Level = level;
4144
Message = message;

0 commit comments

Comments
 (0)