Skip to content

Commit 3c89930

Browse files
committed
[dotnet] Annotate nullable reference types in OpenQA.Selenium.Internal.Logging
1 parent cfaa8c4 commit 3c89930

File tree

13 files changed

+60
-55
lines changed

13 files changed

+60
-55
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: 12 additions & 3 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));
@@ -76,9 +80,14 @@ public FileLogHandler(string filePath, bool overwrite)
7680
/// <param name="logEvent">The log event to handle.</param>
7781
public void Handle(LogEvent logEvent)
7882
{
83+
if (_streamWriter is not { } writer)
84+
{
85+
throw new ObjectDisposedException(nameof(FileLogHandler));
86+
}
87+
7988
lock (_lockObj)
8089
{
81-
_streamWriter.WriteLine($"{logEvent.Timestamp:yyyy-MM-dd HH:mm:ss.fff} {_levels[(int)logEvent.Level]} {logEvent.IssuedBy.Name}: {logEvent.Message}");
90+
writer.WriteLine($"{logEvent.Timestamp:yyyy-MM-dd HH:mm:ss.fff} {_levels[(int)logEvent.Level]} {logEvent.IssuedBy.Name}: {logEvent.Message}");
8291
}
8392
}
8493

@@ -112,9 +121,9 @@ protected virtual void Dispose(bool disposing)
112121
if (disposing)
113122
{
114123
_streamWriter?.Dispose();
115-
_streamWriter = null;
124+
_streamWriter = null!;
116125
_fileStream?.Dispose();
117-
_fileStream = null;
126+
_fileStream = null!;
118127
}
119128

120129
_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: 10 additions & 17 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

@@ -56,14 +58,11 @@ public LogContext(LogEventLevel level, ILogContext parentLogContext, ConcurrentD
5658
}
5759
}
5860

59-
public ILogContext CreateContext()
60-
{
61-
return CreateContext(_level);
62-
}
61+
public ILogContext CreateContext() => CreateContext(_level);
6362

6463
public ILogContext CreateContext(LogEventLevel minimumLevel)
6564
{
66-
ConcurrentDictionary<Type, ILogger> loggers = null;
65+
ConcurrentDictionary<Type, ILogger>? loggers = null;
6766

6867
if (_loggers != null)
6968
{
@@ -77,10 +76,7 @@ public ILogContext CreateContext(LogEventLevel minimumLevel)
7776
return context;
7877
}
7978

80-
public ILogger GetLogger<T>()
81-
{
82-
return GetLogger(typeof(T));
83-
}
79+
public ILogger GetLogger<T>() => GetLogger(typeof(T));
8480

8581
public ILogger GetLogger(Type type)
8682
{
@@ -89,12 +85,9 @@ public ILogger GetLogger(Type type)
8985
throw new ArgumentNullException(nameof(type));
9086
}
9187

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

97-
return _loggers.GetOrAdd(type, _ => new Logger(type, _level));
90+
return _loggers.GetOrAdd(type, type => new Logger(type, _level));
9891
}
9992

10093
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: 5 additions & 2 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>
@@ -33,12 +35,13 @@ 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"/> or <paramref name="message"/> are <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;
41-
Message = message;
44+
Message = message ?? throw new ArgumentNullException(nameof(message));
4245
}
4346

4447
/// <summary>

0 commit comments

Comments
 (0)