2121using System . Collections . Generic ;
2222using System . Globalization ;
2323
24+ #nullable enable
25+
2426namespace OpenQA . Selenium
2527{
2628 /// <summary>
2729 /// Represents an entry in a log from a driver instance.
2830 /// </summary>
2931 public class LogEntry
3032 {
31- private LogLevel level = LogLevel . All ;
32- private DateTime timestamp = DateTime . MinValue ;
33- private string message = string . Empty ;
3433
3534 /// <summary>
3635 /// Initializes a new instance of the <see cref="LogEntry"/> class.
@@ -42,34 +41,27 @@ private LogEntry()
4241 /// <summary>
4342 /// Gets the timestamp value of the log entry.
4443 /// </summary>
45- public DateTime Timestamp
46- {
47- get { return this . timestamp ; }
48- }
44+ public DateTime Timestamp { get ; private set ; } = DateTime . MinValue ;
4945
5046 /// <summary>
5147 /// Gets the logging level of the log entry.
5248 /// </summary>
53- public LogLevel Level
54- {
55- get { return this . level ; }
56- }
49+ public LogLevel Level { get ; private set ; } = LogLevel . All ;
5750
5851 /// <summary>
5952 /// Gets the message of the log entry.
6053 /// </summary>
61- public string Message
62- {
63- get { return this . message ; }
64- }
54+ public string Message { get ; private set ; } = string . Empty ;
55+
56+ private static readonly DateTime UnixEpoch = new ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
6557
6658 /// <summary>
6759 /// Returns a string that represents the current <see cref="LogEntry"/>.
6860 /// </summary>
6961 /// <returns>A string that represents the current <see cref="LogEntry"/>.</returns>
7062 public override string ToString ( )
7163 {
72- return string . Format ( CultureInfo . InvariantCulture , "[{0:yyyy-MM-ddTHH:mm:ssZ}] [{1}] {2}" , this . timestamp , this . level , this . message ) ;
64+ return string . Format ( CultureInfo . InvariantCulture , "[{0:yyyy-MM-ddTHH:mm:ssZ}] [{1}] {2}" , this . Timestamp , this . Level , this . Message ) ;
7365 }
7466
7567 /// <summary>
@@ -78,32 +70,31 @@ public override string ToString()
7870 /// <param name="entryDictionary">The <see cref="Dictionary{TKey, TValue}"/> from
7971 /// which to create the <see cref="LogEntry"/>.</param>
8072 /// <returns>A <see cref="LogEntry"/> with the values in the dictionary.</returns>
81- internal static LogEntry FromDictionary ( Dictionary < string , object > entryDictionary )
73+ internal static LogEntry FromDictionary ( Dictionary < string , object ? > entryDictionary )
8274 {
8375 LogEntry entry = new LogEntry ( ) ;
84- if ( entryDictionary . ContainsKey ( "message" ) )
76+ if ( entryDictionary . TryGetValue ( "message" , out object ? message ) )
8577 {
86- entry . message = entryDictionary [ " message" ] . ToString ( ) ;
78+ entry . Message = message ? . ToString ( ) ?? string . Empty ;
8779 }
8880
89- if ( entryDictionary . ContainsKey ( "timestamp" ) )
81+ if ( entryDictionary . TryGetValue ( "timestamp" , out object ? timestamp ) )
9082 {
91- DateTime zeroDate = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
92- double timestampValue = Convert . ToDouble ( entryDictionary [ "timestamp" ] , CultureInfo . InvariantCulture ) ;
93- entry . timestamp = zeroDate . AddMilliseconds ( timestampValue ) ;
83+ double timestampValue = Convert . ToDouble ( timestamp , CultureInfo . InvariantCulture ) ;
84+ entry . Timestamp = UnixEpoch . AddMilliseconds ( timestampValue ) ;
9485 }
9586
96- if ( entryDictionary . ContainsKey ( "level" ) )
87+ if ( entryDictionary . TryGetValue ( "level" , out object ? level ) )
9788 {
98- string levelValue = entryDictionary [ "level" ] . ToString ( ) ;
99- try
89+ if ( Enum . TryParse ( level ? . ToString ( ) , ignoreCase : true , out LogLevel result ) )
10090 {
101- entry . level = ( LogLevel ) Enum . Parse ( typeof ( LogLevel ) , levelValue , true ) ;
91+ entry . Level = result ;
10292 }
103- catch ( ArgumentException )
93+ else
10494 {
10595 // If the requested log level string is not a valid log level,
10696 // ignore it and use LogLevel.All.
97+ entry . Level = LogLevel . All ;
10798 }
10899 }
109100
0 commit comments