diff --git a/dotnet/src/webdriver/ILogs.cs b/dotnet/src/webdriver/ILogs.cs index 1224f6d4f3666..d3f236f36c05e 100644 --- a/dotnet/src/webdriver/ILogs.cs +++ b/dotnet/src/webdriver/ILogs.cs @@ -17,8 +17,11 @@ // under the License. // +using System; using System.Collections.ObjectModel; +#nullable enable + namespace OpenQA.Selenium { /// @@ -37,6 +40,7 @@ public interface ILogs /// The log for which to retrieve the log entries. /// Log types can be found in the class. /// The list of objects for the specified log. + /// If is . ReadOnlyCollection GetLog(string logKind); } } diff --git a/dotnet/src/webdriver/LogEntry.cs b/dotnet/src/webdriver/LogEntry.cs index 1f43ac5df8a7e..d10a2f3492930 100644 --- a/dotnet/src/webdriver/LogEntry.cs +++ b/dotnet/src/webdriver/LogEntry.cs @@ -21,6 +21,8 @@ using System.Collections.Generic; using System.Globalization; +#nullable enable + namespace OpenQA.Selenium { /// @@ -28,9 +30,6 @@ namespace OpenQA.Selenium /// public class LogEntry { - private LogLevel level = LogLevel.All; - private DateTime timestamp = DateTime.MinValue; - private string message = string.Empty; /// /// Initializes a new instance of the class. @@ -42,26 +41,19 @@ private LogEntry() /// /// Gets the timestamp value of the log entry. /// - public DateTime Timestamp - { - get { return this.timestamp; } - } + public DateTime Timestamp { get; private set; } = DateTime.MinValue; /// /// Gets the logging level of the log entry. /// - public LogLevel Level - { - get { return this.level; } - } + public LogLevel Level { get; private set; } = LogLevel.All; /// /// Gets the message of the log entry. /// - public string Message - { - get { return this.message; } - } + public string Message { get; private set; } = string.Empty; + + private static readonly DateTime UnixEpoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); /// /// Returns a string that represents the current . @@ -69,7 +61,7 @@ public string Message /// A string that represents the current . public override string ToString() { - return string.Format(CultureInfo.InvariantCulture, "[{0:yyyy-MM-ddTHH:mm:ssZ}] [{1}] {2}", this.timestamp, this.level, this.message); + return string.Format(CultureInfo.InvariantCulture, "[{0:yyyy-MM-ddTHH:mm:ssZ}] [{1}] {2}", this.Timestamp, this.Level, this.Message); } /// @@ -78,32 +70,31 @@ public override string ToString() /// The from /// which to create the . /// A with the values in the dictionary. - internal static LogEntry FromDictionary(Dictionary entryDictionary) + internal static LogEntry FromDictionary(Dictionary entryDictionary) { LogEntry entry = new LogEntry(); - if (entryDictionary.ContainsKey("message")) + if (entryDictionary.TryGetValue("message", out object? message)) { - entry.message = entryDictionary["message"].ToString(); + entry.Message = message?.ToString() ?? string.Empty; } - if (entryDictionary.ContainsKey("timestamp")) + if (entryDictionary.TryGetValue("timestamp", out object? timestamp)) { - DateTime zeroDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); - double timestampValue = Convert.ToDouble(entryDictionary["timestamp"], CultureInfo.InvariantCulture); - entry.timestamp = zeroDate.AddMilliseconds(timestampValue); + double timestampValue = Convert.ToDouble(timestamp, CultureInfo.InvariantCulture); + entry.Timestamp = UnixEpoch.AddMilliseconds(timestampValue); } - if (entryDictionary.ContainsKey("level")) + if (entryDictionary.TryGetValue("level", out object? level)) { - string levelValue = entryDictionary["level"].ToString(); - try + if (Enum.TryParse(level?.ToString(), ignoreCase: true, out LogLevel result)) { - entry.level = (LogLevel)Enum.Parse(typeof(LogLevel), levelValue, true); + entry.Level = result; } - catch (ArgumentException) + else { // If the requested log level string is not a valid log level, // ignore it and use LogLevel.All. + entry.Level = LogLevel.All; } } diff --git a/dotnet/src/webdriver/LogLevel.cs b/dotnet/src/webdriver/LogLevel.cs index 962d32cae682e..fd5c291c3a165 100644 --- a/dotnet/src/webdriver/LogLevel.cs +++ b/dotnet/src/webdriver/LogLevel.cs @@ -17,6 +17,8 @@ // under the License. // +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/Logs.cs b/dotnet/src/webdriver/Logs.cs index 74fa3102977d5..91afcc03eb6cd 100644 --- a/dotnet/src/webdriver/Logs.cs +++ b/dotnet/src/webdriver/Logs.cs @@ -21,6 +21,8 @@ using System.Collections.Generic; using System.Collections.ObjectModel; +#nullable enable + namespace OpenQA.Selenium { /// @@ -28,15 +30,16 @@ namespace OpenQA.Selenium /// public class Logs : ILogs { - private WebDriver driver; + private readonly WebDriver driver; /// /// Initializes a new instance of the class. /// /// Instance of the driver currently in use + /// If is . public Logs(WebDriver driver) { - this.driver = driver; + this.driver = driver ?? throw new ArgumentNullException(nameof(driver)); } /// @@ -50,12 +53,11 @@ public ReadOnlyCollection AvailableLogTypes try { Response commandResponse = this.driver.InternalExecute(DriverCommand.GetAvailableLogTypes, null); - object[] responseValue = commandResponse.Value as object[]; - if (responseValue != null) + if (commandResponse.Value is object[] responseValue) { foreach (object logKind in responseValue) { - availableLogTypes.Add(logKind.ToString()); + availableLogTypes.Add(logKind.ToString()!); } } } @@ -74,21 +76,25 @@ public ReadOnlyCollection AvailableLogTypes /// The log for which to retrieve the log entries. /// Log types can be found in the class. /// The list of objects for the specified log. + /// If is . public ReadOnlyCollection GetLog(string logKind) { + if (logKind is null) + { + throw new ArgumentNullException(nameof(logKind)); + } + List entries = new List(); Dictionary parameters = new Dictionary(); parameters.Add("type", logKind); Response commandResponse = this.driver.InternalExecute(DriverCommand.GetLog, parameters); - object[] responseValue = commandResponse.Value as object[]; - if (responseValue != null) + if (commandResponse.Value is object?[] responseValue) { - foreach (object rawEntry in responseValue) + foreach (object? rawEntry in responseValue) { - Dictionary entryDictionary = rawEntry as Dictionary; - if (entryDictionary != null) + if (rawEntry is Dictionary entryDictionary) { entries.Add(LogEntry.FromDictionary(entryDictionary)); }