Skip to content

Commit 8421de7

Browse files
committed
Changed logger to use a temp file instead of loading in memory
1 parent 29789a1 commit 8421de7

1 file changed

Lines changed: 29 additions & 11 deletions

File tree

src/EventLogExpert.UI/Services/DebugLogService.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,41 @@ public void Dispose()
6363

6464
public async IAsyncEnumerable<string> LoadAsync()
6565
{
66-
string[] lines;
67-
68-
await s_loggingFileLock.WaitAsync();
66+
// Copy to a temp file under the lock so writers are blocked only briefly
67+
string tempPath = Path.GetTempFileName();
6968

7069
try
7170
{
72-
lines = await File.ReadAllLinesAsync(_fileLocationOptions.LoggingPath);
71+
await s_loggingFileLock.WaitAsync();
72+
73+
try
74+
{
75+
File.Copy(_fileLocationOptions.LoggingPath, tempPath, overwrite: true);
76+
}
77+
finally
78+
{
79+
s_loggingFileLock.Release();
80+
}
81+
82+
// Stream lines from the snapshot so memory stays bounded and writers aren't blocked
83+
await using var stream = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true);
84+
using var reader = new StreamReader(stream);
85+
86+
while (await reader.ReadLineAsync() is { } line)
87+
{
88+
yield return line;
89+
}
7390
}
7491
finally
7592
{
76-
s_loggingFileLock.Release();
77-
}
78-
79-
// Yield outside the lock so writers aren't blocked during iteration
80-
foreach (var line in lines)
81-
{
82-
yield return line;
93+
try
94+
{
95+
File.Delete(tempPath);
96+
}
97+
catch
98+
{
99+
// Best effort cleanup
100+
}
83101
}
84102
}
85103

0 commit comments

Comments
 (0)