Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Overwrite internal log file if it already exists #13900

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
Loading