Skip to content

Conversation

@snakefoot
Copy link
Contributor

@snakefoot snakefoot commented Sep 5, 2025

Followup to #5987 by making the parsing of archive sequence number more strict (Avoid parsing ${processid} or ${shortdate} as archive sequence number)

@snakefoot snakefoot added this to the 6.0.4 milestone Sep 5, 2025
@snakefoot snakefoot added enhancement Improvement on existing feature file-target file-archiving Issues with archiving with the file target labels Sep 5, 2025
@coderabbitai
Copy link

coderabbitai bot commented Sep 5, 2025

Walkthrough

Introduces a new archive-suffix heuristic in RollingArchiveFileHandler: computes whether the archive suffix lacks digits and passes this as a new parameter to DeleteOldFilesBeforeArchive, which now conditions old-file deletion on this flag. Adds System.Linq for Any, and updates the method signature accordingly.

Changes

Cohort / File(s) Summary
Rolling archive deletion behavior update
src/NLog/Targets/FileArchiveHandlers/RollingArchiveFileHandler.cs
Added using System.Linq; in ArchiveBeforeOpenFile, compute archiveSuffixWithSeqNo = not Any digit in file-name stem; updated call to DeleteOldFilesBeforeArchive with named argument; changed method signature to include bool archiveSuffixWithSeqNo; adjusted old-archive deletion logic to branch based on the new flag.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor App as Application
  participant Logger as Logger
  participant Handler as RollingArchiveFileHandler
  participant FS as FileSystem

  App->>Logger: Write triggers file roll
  Logger->>Handler: ArchiveBeforeOpenFile(newFilePath, initialOpen)
  Note over Handler: Determine if archive suffix lacks digits
  Handler->>Handler: archiveSuffixWithSeqNo = !GetFileNameWithoutExt(newFilePath).Any(IsDigit)
  Handler->>Handler: DeleteOldFilesBeforeArchive(newFilePath, initialOpen, archiveSuffixWithSeqNo)

  alt archiveSuffixWithSeqNo = true
    Note over Handler,FS: Use sequence-numbered suffix matching
    Handler->>FS: Enumerate and delete old files matching seq pattern
  else archiveSuffixWithSeqNo = false
    Note over Handler,FS: Use timestamp/other suffix matching
    Handler->>FS: Enumerate and delete old files via default pattern
  end

  Handler-->>Logger: Archive pruning complete
  Logger-->>App: Continue with new file open
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I hop through logs with careful feet,
Rolling names in tidy beat.
If digits hide, I sniff the trail—
Old tails trimmed, fresh bytes set sail.
Ears up, I prune with subtle art,
Thump-thump—archive, then restart.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@snakefoot snakefoot enabled auto-merge (squash) September 5, 2025 21:13
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/NLog/Targets/FileArchiveHandlers/RollingArchiveFileHandler.cs (1)

38-38: Clarify intent and avoid LINQ allocation here (minor).

The logic is correct and aligns with the PR goal. Two small polish items:

  • Rename the local for readability (true means “base name has no digits; safe to parse a seq-no suffix”).
  • Drop LINQ’s Any to avoid the extra import and iterator allocation.

Apply:

-    using System.Linq;
@@
-                var archiveSuffixWithSeqNo = !Path.GetFileNameWithoutExtension(newFilePath).Any(c => char.IsDigit(c));
-                bool deletedOldFiles = DeleteOldFilesBeforeArchive(newFilePath, initialFileOpen, archiveSuffixWithSeqNo: archiveSuffixWithSeqNo);
+                // True when base filename has no digits; safe to assume archive suffix contains sequence-number
+                var baseName = Path.GetFileNameWithoutExtension(newFilePath);
+                bool assumeArchiveSuffixHasSeqNo = true;
+                for (int i = 0; i < baseName.Length; i++)
+                {
+                    char ch = baseName[i];
+                    if (ch >= '0' && ch <= '9') { assumeArchiveSuffixHasSeqNo = false; break; }
+                }
+                bool deletedOldFiles = DeleteOldFilesBeforeArchive(newFilePath, initialFileOpen, archiveSuffixWithSeqNo: assumeArchiveSuffixHasSeqNo);

Also applies to: 59-60

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 3b023a7 and 8161126.

📒 Files selected for processing (1)
  • src/NLog/Targets/FileArchiveHandlers/RollingArchiveFileHandler.cs (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/NLog/Targets/FileArchiveHandlers/RollingArchiveFileHandler.cs (1)
src/NLog/Targets/FileArchiveHandlers/BaseFileArchiveHandler.cs (2)
  • DeleteOldFilesBeforeArchive (52-60)
  • DeleteOldFilesBeforeArchive (62-114)

@sonarqubecloud
Copy link

sonarqubecloud bot commented Sep 5, 2025

This was referenced Sep 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement on existing feature file-archiving Issues with archiving with the file target file-target size/XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant