@@ -64,32 +64,40 @@ public void Dispose()
6464
6565 public async IAsyncEnumerable < string > LoadAsync ( )
6666 {
67- // If the log file doesn't exist yet (fresh install, or before first write),
68- // yield no lines instead of throwing FileNotFoundException.
69- if ( ! File . Exists ( _fileLocationOptions . LoggingPath ) )
70- {
71- yield break ;
72- }
73-
7467 // Read directly from the source file. Writers use File.AppendText which opens with
75- // FileShare.Read, allowing concurrent readers. We open with FileShare.ReadWrite to
76- // allow concurrent writers. This avoids the overhead of copying to a temp file and
77- // eliminates lock contention between readers and writers .
68+ // FileShare.Read, allowing concurrent readers. We open with FileShare.ReadWrite | Delete
69+ // to allow concurrent writers and log rotation/deletion (e.g., InitTracing deletes oversized logs).
70+ // This avoids the overhead of copying to a temp file and eliminates lock contention .
7871 var options = new FileStreamOptions
7972 {
8073 Mode = FileMode . Open ,
8174 Access = FileAccess . Read ,
82- Share = FileShare . ReadWrite ,
75+ Share = FileShare . ReadWrite | FileShare . Delete ,
8376 Options = FileOptions . Asynchronous | FileOptions . SequentialScan ,
8477 BufferSize = 4096
8578 } ;
8679
87- await using var stream = new FileStream ( _fileLocationOptions . LoggingPath , options ) ;
88- using var reader = new StreamReader ( stream ) ;
80+ FileStream stream ;
81+
82+ try
83+ {
84+ stream = new FileStream ( _fileLocationOptions . LoggingPath , options ) ;
85+ }
86+ catch ( Exception ex ) when ( ex is FileNotFoundException or DirectoryNotFoundException )
87+ {
88+ // The log file doesn't exist yet (fresh install, before first write),
89+ // or was deleted between check and open (e.g., InitTracing deletes oversized logs).
90+ yield break ;
91+ }
8992
90- while ( await reader . ReadLineAsync ( ) is { } line )
93+ await using ( stream )
9194 {
92- yield return line ;
95+ using var reader = new StreamReader ( stream ) ;
96+
97+ while ( await reader . ReadLineAsync ( ) is { } line )
98+ {
99+ yield return line ;
100+ }
93101 }
94102 }
95103
0 commit comments