From 7274501b74cf0d6aac241f9dd02d5ce19f640ecc Mon Sep 17 00:00:00 2001
From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com>
Date: Thu, 2 May 2024 13:14:52 +0300
Subject: [PATCH 1/2] [dotnet] Optionally overwrite internal log file if it
already exists
---
.../Internal/Logging/FileLogHandler.cs | 16 +++-
.../Internal/Logging/FileLogHandlerTest.cs | 91 +++++++++++++++++++
2 files changed, 105 insertions(+), 2 deletions(-)
diff --git a/dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs b/dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs
index 5d8fc120a6746..774476f32710c 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: false)
+ {
+
+ }
+
+ ///
+ /// 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..716b78c1cda69 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 ShouldAppendFileIfDoesNotExist()
+ {
+ 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))
+ {
+ 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 ShouldAppendFileIfExists()
+ {
+ 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(2));
+ }
+ finally
+ {
+ File.Delete(tempFile);
+ }
+ }
+
+ [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 ShouldCreateFileIfDoesNotExist()
+ {
+ 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);
+ }
+ }
}
}
From 8e0574f3e31b903e690bfce5a519a3da580e4b60 Mon Sep 17 00:00:00 2001
From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com>
Date: Wed, 15 May 2024 01:19:32 +0300
Subject: [PATCH 2/2] Overwrite log file by default
---
.../Internal/Logging/FileLogHandler.cs | 2 +-
.../Internal/Logging/FileLogHandlerTest.cs | 24 +++++++++----------
2 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs b/dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs
index 774476f32710c..12794aea526b3 100644
--- a/dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs
+++ b/dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs
@@ -22,7 +22,7 @@ public class FileLogHandler : ILogHandler, IDisposable
///
/// The path of the log file.
public FileLogHandler(string filePath)
- : this(filePath, overwrite: false)
+ : this(filePath, overwrite: true)
{
}
diff --git a/dotnet/test/common/Internal/Logging/FileLogHandlerTest.cs b/dotnet/test/common/Internal/Logging/FileLogHandlerTest.cs
index 716b78c1cda69..dae6cccda01ac 100644
--- a/dotnet/test/common/Internal/Logging/FileLogHandlerTest.cs
+++ b/dotnet/test/common/Internal/Logging/FileLogHandlerTest.cs
@@ -37,52 +37,52 @@ public void ShouldHandleLogEvent()
}
[Test]
- public void ShouldAppendFileIfDoesNotExist()
+ public void ShouldCreateFileIfDoesNotExist()
{
- var tempFilePath = Path.GetTempPath() + "somefile.log";
+ var tempFile = Path.GetTempFileName();
try
{
- using (var fileLogHandler = new FileLogHandler(tempFilePath))
+ using (var fileLogHandler = new FileLogHandler(tempFile))
{
fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
}
- using (var fileLogHandler2 = new FileLogHandler(tempFilePath))
+ using (var fileLogHandler2 = new FileLogHandler(tempFile))
{
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));
+ Assert.That(Regex.Matches(File.ReadAllText(tempFile), "test message").Count, Is.EqualTo(1));
}
finally
{
- File.Delete(tempFilePath);
+ File.Delete(tempFile);
}
}
[Test]
public void ShouldAppendFileIfExists()
{
- var tempFile = Path.GetTempFileName();
+ var tempFilePath = Path.GetTempPath() + "somefile.log";
try
{
- using (var fileLogHandler = new FileLogHandler(tempFile))
+ using (var fileLogHandler = new FileLogHandler(tempFilePath))
{
fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
}
- using (var fileLogHandler2 = new FileLogHandler(tempFile))
+ 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(tempFile), "test message").Count, Is.EqualTo(2));
+ Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message").Count, Is.EqualTo(2));
}
finally
{
- File.Delete(tempFile);
+ File.Delete(tempFilePath);
}
}
@@ -107,7 +107,7 @@ public void ShouldOverwriteFileIfExists()
}
[Test]
- public void ShouldCreateFileIfDoesNotExist()
+ public void ShouldAppendFileIfDoesNotExist()
{
var tempFilePath = Path.GetTempPath() + "somefile.log";