Skip to content

Commit

Permalink
[dotnet] Overwrite internal log file if it already exists (#13900)
Browse files Browse the repository at this point in the history
* [dotnet] Optionally overwrite internal log file if it already exists

* Overwrite log file by default

---------

Co-authored-by: Diego Molina <[email protected]>
  • Loading branch information
nvborisenko and diemol authored May 15, 2024
1 parent d556c8e commit 15319c0
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
16 changes: 14 additions & 2 deletions dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ public class FileLogHandler : ILogHandler, IDisposable
/// </summary>
/// <param name="filePath">The path of the log file.</param>
public FileLogHandler(string filePath)
: this(filePath, overwrite: true)
{

}

/// <summary>
/// Initializes a new instance of the <see cref="FileLogHandler"/> class with the specified file path.
/// </summary>
/// <param name="filePath">The path of the log file.</param>
/// <param name="overwrite">Specifies whether the file should be overwritten if it exists on the disk.</param>
public FileLogHandler(string filePath, bool overwrite)
{
if (string.IsNullOrEmpty(filePath)) throw new ArgumentException("File log path cannot be null or empty.", nameof(filePath));

Expand All @@ -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
Expand Down
91 changes: 91 additions & 0 deletions dotnet/test/common/Internal/Logging/FileLogHandlerTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NUnit.Framework;
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace OpenQA.Selenium.Internal.Logging
{
Expand Down Expand Up @@ -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);
}
}
}
}

0 comments on commit 15319c0

Please sign in to comment.