Skip to content

Commit 15319c0

Browse files
nvborisenkodiemol
andauthored
[dotnet] Overwrite internal log file if it already exists (#13900)
* [dotnet] Optionally overwrite internal log file if it already exists * Overwrite log file by default --------- Co-authored-by: Diego Molina <[email protected]>
1 parent d556c8e commit 15319c0

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

dotnet/src/webdriver/Internal/Logging/FileLogHandler.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ public class FileLogHandler : ILogHandler, IDisposable
2222
/// </summary>
2323
/// <param name="filePath">The path of the log file.</param>
2424
public FileLogHandler(string filePath)
25+
: this(filePath, overwrite: true)
26+
{
27+
28+
}
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="FileLogHandler"/> class with the specified file path.
32+
/// </summary>
33+
/// <param name="filePath">The path of the log file.</param>
34+
/// <param name="overwrite">Specifies whether the file should be overwritten if it exists on the disk.</param>
35+
public FileLogHandler(string filePath, bool overwrite)
2536
{
2637
if (string.IsNullOrEmpty(filePath)) throw new ArgumentException("File log path cannot be null or empty.", nameof(filePath));
2738

@@ -31,8 +42,9 @@ public FileLogHandler(string filePath)
3142
Directory.CreateDirectory(directory);
3243
}
3344

34-
_fileStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
35-
_fileStream.Seek(0, SeekOrigin.End);
45+
var fileMode = overwrite ? FileMode.Create : FileMode.Append;
46+
47+
_fileStream = File.Open(filePath, fileMode, FileAccess.Write, FileShare.Read);
3648
_streamWriter = new StreamWriter(_fileStream, System.Text.Encoding.UTF8)
3749
{
3850
AutoFlush = true

dotnet/test/common/Internal/Logging/FileLogHandlerTest.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using NUnit.Framework;
22
using System;
33
using System.IO;
4+
using System.Text.RegularExpressions;
45

56
namespace OpenQA.Selenium.Internal.Logging
67
{
@@ -34,5 +35,95 @@ public void ShouldHandleLogEvent()
3435
File.Delete(tempFile);
3536
}
3637
}
38+
39+
[Test]
40+
public void ShouldCreateFileIfDoesNotExist()
41+
{
42+
var tempFile = Path.GetTempFileName();
43+
44+
try
45+
{
46+
using (var fileLogHandler = new FileLogHandler(tempFile))
47+
{
48+
fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
49+
}
50+
51+
using (var fileLogHandler2 = new FileLogHandler(tempFile))
52+
{
53+
fileLogHandler2.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
54+
}
55+
56+
Assert.That(Regex.Matches(File.ReadAllText(tempFile), "test message").Count, Is.EqualTo(1));
57+
}
58+
finally
59+
{
60+
File.Delete(tempFile);
61+
}
62+
}
63+
64+
[Test]
65+
public void ShouldAppendFileIfExists()
66+
{
67+
var tempFilePath = Path.GetTempPath() + "somefile.log";
68+
69+
try
70+
{
71+
using (var fileLogHandler = new FileLogHandler(tempFilePath))
72+
{
73+
fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
74+
}
75+
76+
using (var fileLogHandler2 = new FileLogHandler(tempFilePath, overwrite: false))
77+
{
78+
fileLogHandler2.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
79+
}
80+
81+
Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message").Count, Is.EqualTo(2));
82+
}
83+
finally
84+
{
85+
File.Delete(tempFilePath);
86+
}
87+
}
88+
89+
[Test]
90+
public void ShouldOverwriteFileIfExists()
91+
{
92+
var tempFile = Path.GetTempFileName();
93+
94+
try
95+
{
96+
using (var fileLogHandler = new FileLogHandler(tempFile, overwrite: true))
97+
{
98+
fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
99+
}
100+
101+
Assert.That(Regex.Matches(File.ReadAllText(tempFile), "test message").Count, Is.EqualTo(1));
102+
}
103+
finally
104+
{
105+
File.Delete(tempFile);
106+
}
107+
}
108+
109+
[Test]
110+
public void ShouldAppendFileIfDoesNotExist()
111+
{
112+
var tempFilePath = Path.GetTempPath() + "somefile.log";
113+
114+
try
115+
{
116+
using (var fileLogHandler = new FileLogHandler(tempFilePath, overwrite: true))
117+
{
118+
fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
119+
}
120+
121+
Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message").Count, Is.EqualTo(1));
122+
}
123+
finally
124+
{
125+
File.Delete(tempFilePath);
126+
}
127+
}
37128
}
38129
}

0 commit comments

Comments
 (0)