Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 12 additions & 3 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 All @@ -76,9 +80,14 @@ public FileLogHandler(string filePath, bool overwrite)
/// <param name="logEvent">The log event to handle.</param>
public void Handle(LogEvent logEvent)
{
if (_streamWriter is not { } writer)
{
throw new ObjectDisposedException(nameof(FileLogHandler));
}

lock (_lockObj)
{
_streamWriter.WriteLine($"{logEvent.Timestamp:yyyy-MM-dd HH:mm:ss.fff} {_levels[(int)logEvent.Level]} {logEvent.IssuedBy.Name}: {logEvent.Message}");
writer.WriteLine($"{logEvent.Timestamp:yyyy-MM-dd HH:mm:ss.fff} {_levels[(int)logEvent.Level]} {logEvent.IssuedBy.Name}: {logEvent.Message}");
}
}

Expand Down Expand Up @@ -112,9 +121,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
27 changes: 10 additions & 17 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 @@ -56,14 +58,11 @@ public LogContext(LogEventLevel level, ILogContext parentLogContext, ConcurrentD
}
}

public ILogContext CreateContext()
{
return CreateContext(_level);
}
public ILogContext CreateContext() => CreateContext(_level);

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

if (_loggers != null)
{
Expand All @@ -77,10 +76,7 @@ public ILogContext CreateContext(LogEventLevel minimumLevel)
return context;
}

public ILogger GetLogger<T>()
{
return GetLogger(typeof(T));
}
public ILogger GetLogger<T>() => GetLogger(typeof(T));

public ILogger GetLogger(Type type)
{
Expand All @@ -89,12 +85,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]
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;
}
}
}
7 changes: 5 additions & 2 deletions 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,12 +35,13 @@ 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"/> or <paramref name="message"/> are <see langword="null"/>.</exception>
public LogEvent(Type issuedBy, DateTimeOffset timestamp, LogEventLevel level, string message)
{
IssuedBy = issuedBy;
IssuedBy = issuedBy ?? throw new ArgumentNullException(nameof(issuedBy));
Timestamp = timestamp;
Level = level;
Message = message;
Message = message ?? throw new ArgumentNullException(nameof(message));
}

/// <summary>
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