|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | + |
| 4 | +namespace OpenQA.Selenium.Internal.Logging |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Represents a log handler that writes log events to a file. |
| 8 | + /// </summary> |
| 9 | + public class FileLogHandler : ILogHandler, IDisposable |
| 10 | + { |
| 11 | + // performance trick to avoid expensive Enum.ToString() with fixed length |
| 12 | + private static readonly string[] _levels = { "TRACE", "DEBUG", " INFO", " WARN", "ERROR" }; |
| 13 | + |
| 14 | + private FileStream _fileStream; |
| 15 | + private StreamWriter _streamWriter; |
| 16 | + |
| 17 | + private readonly object _lockObj = new object(); |
| 18 | + private bool _isDisposed; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Initializes a new instance of the <see cref="FileLogHandler"/> class with the specified file path. |
| 22 | + /// </summary> |
| 23 | + /// <param name="path">The path of the log file.</param> |
| 24 | + public FileLogHandler(string path) |
| 25 | + { |
| 26 | + if (string.IsNullOrEmpty(path)) throw new ArgumentException("File log path cannot be null or empty.", nameof(path)); |
| 27 | + |
| 28 | + var directory = Path.GetDirectoryName(path); |
| 29 | + if (!string.IsNullOrWhiteSpace(directory) && !Directory.Exists(directory)) |
| 30 | + { |
| 31 | + Directory.CreateDirectory(directory); |
| 32 | + } |
| 33 | + |
| 34 | + _fileStream = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); |
| 35 | + _fileStream.Seek(0, SeekOrigin.End); |
| 36 | + _streamWriter = new StreamWriter(_fileStream, System.Text.Encoding.UTF8) |
| 37 | + { |
| 38 | + AutoFlush = true |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Handles a log event by writing it to the log file. |
| 44 | + /// </summary> |
| 45 | + /// <param name="logEvent">The log event to handle.</param> |
| 46 | + public void Handle(LogEvent logEvent) |
| 47 | + { |
| 48 | + lock (_lockObj) |
| 49 | + { |
| 50 | + _streamWriter.WriteLine($"{logEvent.Timestamp:r} {_levels[(int)logEvent.Level]} {logEvent.IssuedBy.Name}: {logEvent.Message}"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Disposes the file log handler and releases any resources used. |
| 56 | + /// </summary> |
| 57 | + public void Dispose() |
| 58 | + { |
| 59 | + Dispose(true); |
| 60 | + GC.SuppressFinalize(this); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Finalizes the file log handler instance. |
| 65 | + /// </summary> |
| 66 | + ~FileLogHandler() |
| 67 | + { |
| 68 | + Dispose(false); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Disposes the file log handler and releases any resources used. |
| 73 | + /// </summary> |
| 74 | + /// <param name="disposing">A flag indicating whether to dispose managed resources.</param> |
| 75 | + protected virtual void Dispose(bool disposing) |
| 76 | + { |
| 77 | + lock (_lockObj) |
| 78 | + { |
| 79 | + if (!_isDisposed) |
| 80 | + { |
| 81 | + if (disposing) |
| 82 | + { |
| 83 | + _streamWriter?.Dispose(); |
| 84 | + _streamWriter = null; |
| 85 | + _fileStream?.Dispose(); |
| 86 | + _fileStream = null; |
| 87 | + } |
| 88 | + |
| 89 | + _isDisposed = true; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments