Skip to content

Commit 7dd86b5

Browse files
BertkCopilot
andauthored
Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 4e7127a commit 7dd86b5

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

src/coverlet.console/Logging/ConsoleLogger.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ class ConsoleLogger : ILogger
1616
public LogLevel Level { get; set; } = LogLevel.Normal;
1717

1818
/// <summary>
19-
/// Enables writing all trace-level diagnostics to the specified file, regardless of the
20-
/// configured console <see cref="Level"/>. Intended for CI troubleshooting (e.g. --diag).
19+
/// Enables writing diagnostics to the specified file. All log messages (including trace-level
20+
/// messages that may be suppressed from the console by <see cref="Level"/>) are written to the file.
21+
/// Intended for CI troubleshooting (e.g. --diag).
2122
/// </summary>
2223
public void EnableDiagnosticFile(string path)
2324
{
2425
ArgumentNullException.ThrowIfNull(path);
2526

27+
StreamWriter newWriter = null;
2628
try
2729
{
2830
string directory = Path.GetDirectoryName(Path.GetFullPath(path));
@@ -31,11 +33,22 @@ public void EnableDiagnosticFile(string path)
3133
Directory.CreateDirectory(directory);
3234
}
3335

34-
_diagWriter = new StreamWriter(path, append: false) { AutoFlush = true };
36+
newWriter = new StreamWriter(path, append: false) { AutoFlush = true };
37+
38+
lock (s_sync)
39+
{
40+
_diagWriter?.Dispose();
41+
_diagWriter = newWriter;
42+
}
3543
}
3644
catch (Exception ex)
3745
{
38-
_diagWriter = null;
46+
newWriter?.Dispose();
47+
lock (s_sync)
48+
{
49+
_diagWriter = null;
50+
}
51+
3952
LogWarning($"Unable to create diagnostic file '{path}': {ex.Message}");
4053
}
4154
}

0 commit comments

Comments
 (0)