Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Annotate nullable reference types in internal logging #14819

Merged
merged 3 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using System;

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
/// <summary>
Expand Down
8 changes: 6 additions & 2 deletions dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using System;
using System.IO;

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
/// <summary>
Expand All @@ -40,6 +42,7 @@ public class FileLogHandler : ILogHandler, IDisposable
/// Initializes a new instance of the <see cref="FileLogHandler"/> class with the specified file path.
/// </summary>
/// <param name="filePath">The path of the log file.</param>
/// <exception cref="ArgumentException">If <paramref name="filePath"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
public FileLogHandler(string filePath)
: this(filePath, overwrite: true)
{
Expand All @@ -51,6 +54,7 @@ public FileLogHandler(string filePath)
/// </summary>
/// <param name="filePath">The path of the log file.</param>
/// <param name="overwrite">Specifies whether the file should be overwritten if it exists on the disk.</param>
/// <exception cref="ArgumentException">If <paramref name="filePath"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
public FileLogHandler(string filePath, bool overwrite)
{
if (string.IsNullOrEmpty(filePath)) throw new ArgumentException("File log path cannot be null or empty.", nameof(filePath));
Expand Down Expand Up @@ -112,9 +116,9 @@ protected virtual void Dispose(bool disposing)
if (disposing)
{
_streamWriter?.Dispose();
_streamWriter = null;
_streamWriter = null!;
_fileStream?.Dispose();
_fileStream = null;
_fileStream = null!;
}

_isDisposed = true;
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/Internal/Logging/ILogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using System;

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/Internal/Logging/ILogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/Internal/Logging/ILogHandlerList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using System.Collections.Generic;

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/Internal/Logging/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using System;

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
/// <summary>
Expand Down
14 changes: 6 additions & 8 deletions dotnet/src/webdriver/Internal/Logging/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
// </copyright>

using System;
using System.Diagnostics.CodeAnalysis;

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
Expand Down Expand Up @@ -65,16 +68,11 @@ public static ILogContext CreateContext(LogEventLevel minimumLevel)
/// <summary>
/// Gets or sets the current log context.
/// </summary>
[AllowNull]
internal static ILogContext CurrentContext
{
get
{
return _logContextManager.CurrentContext;
}
set
{
_logContextManager.CurrentContext = value;
}
get => _logContextManager.CurrentContext;
set => _logContextManager.CurrentContext = value;
}

/// <summary>
Expand Down
17 changes: 8 additions & 9 deletions dotnet/src/webdriver/Internal/Logging/LogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using System.Collections.Generic;
using System.Linq;

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
/// <summary>
Expand All @@ -30,15 +32,15 @@ namespace OpenQA.Selenium.Internal.Logging
/// <inheritdoc cref="ILogContext"/>
internal class LogContext : ILogContext
{
private ConcurrentDictionary<Type, ILogger> _loggers;
private ConcurrentDictionary<Type, ILogger>? _loggers;

private LogEventLevel _level;

private readonly ILogContext _parentLogContext;
private readonly ILogContext? _parentLogContext;

private readonly Lazy<LogHandlerList> _lazyLogHandlerList;

public LogContext(LogEventLevel level, ILogContext parentLogContext, ConcurrentDictionary<Type, ILogger> loggers, IEnumerable<ILogHandler> handlers)
public LogContext(LogEventLevel level, ILogContext? parentLogContext, ConcurrentDictionary<Type, ILogger>? loggers, IEnumerable<ILogHandler>? handlers)
{
_level = level;

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

public ILogContext CreateContext(LogEventLevel minimumLevel)
{
ConcurrentDictionary<Type, ILogger> loggers = null;
ConcurrentDictionary<Type, ILogger>? loggers = null;

if (_loggers != null)
{
Expand All @@ -89,12 +91,9 @@ public ILogger GetLogger(Type type)
throw new ArgumentNullException(nameof(type));
}

if (_loggers is null)
{
_loggers = new ConcurrentDictionary<Type, ILogger>();
}
_loggers ??= new ConcurrentDictionary<Type, ILogger>();

return _loggers.GetOrAdd(type, _ => new Logger(type, _level));
return _loggers.GetOrAdd(type, type => new Logger(type, _level));
}

public bool IsEnabled(ILogger logger, LogEventLevel level)
Expand Down
32 changes: 9 additions & 23 deletions dotnet/src/webdriver/Internal/Logging/LogContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,31 @@
// under the License.
// </copyright>

using System.Diagnostics.CodeAnalysis;
using System.Threading;

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
internal class LogContextManager
{
private readonly ILogContext _globalLogContext;

private readonly AsyncLocal<ILogContext> _currentAmbientLogContext = new AsyncLocal<ILogContext>();
private readonly AsyncLocal<ILogContext?> _currentAmbientLogContext = new AsyncLocal<ILogContext?>();

public LogContextManager()
{
var defaulConsoleLogHandler = new ConsoleLogHandler();

_globalLogContext = new LogContext(LogEventLevel.Info, null, null, new[] { defaulConsoleLogHandler });
GlobalContext = new LogContext(LogEventLevel.Info, null, null, new[] { defaulConsoleLogHandler });
}

public ILogContext GlobalContext
{
get { return _globalLogContext; }
}
public ILogContext GlobalContext { get; }

[AllowNull]
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
public ILogContext CurrentContext
{
get
{
if (_currentAmbientLogContext.Value is null)
{
return _globalLogContext;
}
else
{
return _currentAmbientLogContext.Value;
}
}
set
{
_currentAmbientLogContext.Value = value;
}
get => _currentAmbientLogContext.Value ?? GlobalContext;
set => _currentAmbientLogContext.Value = value;
}
}
}
5 changes: 4 additions & 1 deletion dotnet/src/webdriver/Internal/Logging/LogEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using System;

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
/// <summary>
Expand All @@ -33,9 +35,10 @@ public sealed class LogEvent
/// <param name="timestamp">The timestamp of the log event.</param>
/// <param name="level">The level of the log event.</param>
/// <param name="message">The message of the log event.</param>
/// <exception cref="ArgumentNullException">If <paramref name="issuedBy"/> is <see langword="null"/>.</exception>
public LogEvent(Type issuedBy, DateTimeOffset timestamp, LogEventLevel level, string message)
{
IssuedBy = issuedBy;
IssuedBy = issuedBy ?? throw new ArgumentNullException(nameof(issuedBy));
RenderMichael marked this conversation as resolved.
Show resolved Hide resolved
Timestamp = timestamp;
Level = level;
Message = message;
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/Internal/Logging/LogEventLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion dotnet/src/webdriver/Internal/Logging/LogHandlerList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

using System.Collections.Generic;

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
/// <summary>
/// Represents a list of log handlers.
/// </summary>
/// <inheritdoc cref="ILogHandlerList"/>
internal class LogHandlerList : List<ILogHandler>, ILogHandlerList
internal sealed class LogHandlerList : List<ILogHandler>, ILogHandlerList
{
private readonly ILogContext _logContext;

Expand Down
4 changes: 3 additions & 1 deletion dotnet/src/webdriver/Internal/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

using System;

#nullable enable

namespace OpenQA.Selenium.Internal.Logging
{
/// <summary>
/// The implementation of the <see cref="ILogger"/> interface through which log messages are emitted.
/// </summary>
/// <inheritdoc cref="ILogger"/>
internal class Logger : ILogger
internal sealed class Logger : ILogger
{
public Logger(Type issuer, LogEventLevel level)
{
Expand Down