diff --git a/dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs b/dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs
index 5d8fc120a6746..12794aea526b3 100644
--- a/dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs
+++ b/dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs
@@ -22,6 +22,17 @@ public class FileLogHandler : ILogHandler, IDisposable
///
/// The path of the log file.
public FileLogHandler(string filePath)
+ : this(filePath, overwrite: true)
+ {
+
+ }
+
+ ///
+ /// Initializes a new instance of the class with the specified file path.
+ ///
+ /// The path of the log file.
+ /// Specifies whether the file should be overwritten if it exists on the disk.
+ public FileLogHandler(string filePath, bool overwrite)
{
if (string.IsNullOrEmpty(filePath)) throw new ArgumentException("File log path cannot be null or empty.", nameof(filePath));
@@ -31,8 +42,9 @@ public FileLogHandler(string filePath)
Directory.CreateDirectory(directory);
}
- _fileStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
- _fileStream.Seek(0, SeekOrigin.End);
+ var fileMode = overwrite ? FileMode.Create : FileMode.Append;
+
+ _fileStream = File.Open(filePath, fileMode, FileAccess.Write, FileShare.Read);
_streamWriter = new StreamWriter(_fileStream, System.Text.Encoding.UTF8)
{
AutoFlush = true
diff --git a/dotnet/test/common/Internal/Logging/FileLogHandlerTest.cs b/dotnet/test/common/Internal/Logging/FileLogHandlerTest.cs
index 790f8ce0cb997..dae6cccda01ac 100644
--- a/dotnet/test/common/Internal/Logging/FileLogHandlerTest.cs
+++ b/dotnet/test/common/Internal/Logging/FileLogHandlerTest.cs
@@ -1,6 +1,7 @@
using NUnit.Framework;
using System;
using System.IO;
+using System.Text.RegularExpressions;
namespace OpenQA.Selenium.Internal.Logging
{
@@ -34,5 +35,95 @@ public void ShouldHandleLogEvent()
File.Delete(tempFile);
}
}
+
+ [Test]
+ public void ShouldCreateFileIfDoesNotExist()
+ {
+ var tempFile = Path.GetTempFileName();
+
+ try
+ {
+ using (var fileLogHandler = new FileLogHandler(tempFile))
+ {
+ fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
+ }
+
+ using (var fileLogHandler2 = new FileLogHandler(tempFile))
+ {
+ fileLogHandler2.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
+ }
+
+ Assert.That(Regex.Matches(File.ReadAllText(tempFile), "test message").Count, Is.EqualTo(1));
+ }
+ finally
+ {
+ File.Delete(tempFile);
+ }
+ }
+
+ [Test]
+ public void ShouldAppendFileIfExists()
+ {
+ var tempFilePath = Path.GetTempPath() + "somefile.log";
+
+ try
+ {
+ using (var fileLogHandler = new FileLogHandler(tempFilePath))
+ {
+ fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
+ }
+
+ using (var fileLogHandler2 = new FileLogHandler(tempFilePath, overwrite: false))
+ {
+ fileLogHandler2.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
+ }
+
+ Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message").Count, Is.EqualTo(2));
+ }
+ finally
+ {
+ File.Delete(tempFilePath);
+ }
+ }
+
+ [Test]
+ public void ShouldOverwriteFileIfExists()
+ {
+ var tempFile = Path.GetTempFileName();
+
+ try
+ {
+ using (var fileLogHandler = new FileLogHandler(tempFile, overwrite: true))
+ {
+ fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
+ }
+
+ Assert.That(Regex.Matches(File.ReadAllText(tempFile), "test message").Count, Is.EqualTo(1));
+ }
+ finally
+ {
+ File.Delete(tempFile);
+ }
+ }
+
+ [Test]
+ public void ShouldAppendFileIfDoesNotExist()
+ {
+ var tempFilePath = Path.GetTempPath() + "somefile.log";
+
+ try
+ {
+ using (var fileLogHandler = new FileLogHandler(tempFilePath, overwrite: true))
+ {
+ fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
+ }
+
+ Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message").Count, Is.EqualTo(1));
+ }
+ finally
+ {
+ File.Delete(tempFilePath);
+ }
+ }
}
}