Skip to content

RotatingStreamWriter.Rotate: File.Move failure silently skipped — log grows unbounded #256

@Christophe-Rogiers

Description

@Christophe-Rogiers

Description

RotatingStreamWriter.cs lines 354–383 silently catch all exceptions on File.Move() during log rotation without retrying. If the move fails, the current log file continues growing past the rotation size limit.

Scenario

  1. Antivirus briefly locks the log file during a scan
  2. File.Move() throws IOException
  3. Exception is silently caught
  4. Log file continues growing past _rotationSizeInBytes
  5. No retry, no warning logged
  6. Eventually disk fills up

Suggested Fix

Add a short retry loop before giving up:

const int maxRetries = 3;
for (int attempt = 0; attempt < maxRetries; attempt++)
{
    try
    {
        File.Move(_file.FullName, rotatedPath);
        EnforceMaxRotations();
        return; // success
    }
    catch (IOException) when (attempt < maxRetries - 1)
    {
        Thread.Sleep(50 * (attempt + 1));
    }
    catch
    {
        break; // non-IO error, give up
    }
}
// If all retries fail, log a warning so the operator knows rotation is stuck
Logger.Warn($"Log rotation failed after {maxRetries} attempts: {_file.FullName}");

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions