Skip to content

Conversation

@snakefoot
Copy link
Contributor

No description provided.

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

coderabbitai bot commented Aug 31, 2025

Walkthrough

Adjusts wildcard index calculation and propagation for archive file operations so EndIndex is only non-negative for a single '*' wildcard; adds a guard to skip sequence scanning when no wildcard exists; simplifies a character check; and changes Rolling handler to use endIndex = -1 when no wildcard.

Changes

Cohort / File(s) Summary
Wildcard index handling in base archive cleanup
src/NLog/Targets/FileArchiveHandlers/BaseFileArchiveHandler.cs
Moves computation of wildcard start/end indices later; computes fileWildcardStartIndex via IndexOf('*') and sets fileWildcardEndIndex to fileWildcard.Length - fileWildcardStartIndex only when exactly one * is present; passes these indices to CleanupFiles.
Preceding character classification refactor
src/NLog/Targets/FileArchiveHandlers/LegacyArchiveFileNameHandler.cs
Replaces separate !IsLetter and !IsDigit checks with a single !IsLetterOrDigit check in ResolveNextArchiveSequenceNo; behavior unchanged.
Archive sequence range adjustment without wildcard
src/NLog/Targets/FileArchiveHandlers/RollingArchiveFileHandler.cs
Logs and exits early if archive directory missing; unifies sequence-number discovery path and passes endIndex = -1 (instead of filename.Length) to GetMaxArchiveSequenceNo when no wildcard is present; increments returned sequence when ArchiveOldFileOnStartup is true.
Sequence scanning guard
src/NLog/Targets/FileArchiveHandlers/FileInfoDateTime.cs
ScanFileNamesForMaxSequenceNo now returns default(int?) immediately if fileWildcardStartIndex < 0, short-circuiting sequence extraction when no * exists.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Logger
  participant BaseHandler as BaseFileArchiveHandler
  participant Exclude as ExcludeFileName
  participant Cleanup as CleanupFiles

  Logger->>BaseHandler: DeleteOldFilesBeforeArchive(fileWildcard)
  Note over BaseHandler: Determine archiveCleanupReason\nThen compute fileWildcardStartIndex\nCompute fileWildcardEndIndex only if exactly one '*'
  alt Single "*" wildcard
    BaseHandler->>Exclude: call with startIndex and EndIndex
    BaseHandler->>Cleanup: call with startIndex and EndIndex
  else No or multiple "*" wildcards
    BaseHandler->>Exclude: call with startIndex and endIndex = -1
    BaseHandler->>Cleanup: call with startIndex and endIndex = -1
  end
Loading
sequenceDiagram
  autonumber
  participant Rolling as RollingArchiveFileHandler
  participant Seq as GetMaxArchiveSequenceNo

  Rolling->>Rolling: RollToInitialSequenceNumber(archivePattern)
  Note over Rolling: compute wildcard indices after path/dir checks
  alt Wildcard present (single '*')
    Rolling->>Seq: startIndex, endIndex (positive)
  else No wildcard or multiple '*'
    Note over Rolling: endIndex = -1
    Rolling->>Seq: startIndex, endIndex = -1
  end
  Seq-->>Rolling: maxSequenceNo (or null)
  Rolling->>Rolling: increment if ArchiveOldFileOnStartup
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I hop through filenames under moonlit code,
One star I count, the rest I bode.
EndIndex sings only when single and bright,
Else -1 guides my cleanup night.
I nibble sequences, tidy the lair—archive neat, I care. 🐇✨


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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@snakefoot snakefoot force-pushed the filetarget_wildcard_exclude branch from 3b6ec8f to c7d7f67 Compare August 31, 2025 08:19
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 (2)
src/NLog/Targets/FileArchiveHandlers/RollingArchiveFileHandler.cs (1)

130-134: Align wildcard index semantics with Base handler and avoid path-level Replace.

For consistency with BaseFileArchiveHandler (single '' strictness) and to avoid accidental Replace in directory segments, compute indices from the file name and use LastIndexOf('').

-                var archivePathWildCard = _fileTarget.BuildFullFilePath(newFilePath, int.MaxValue, DateTime.MinValue).Replace(int.MaxValue.ToString(), "*");
-                var archiveFileName = Path.GetFileName(archivePathWildCard);
-                var fileWildcardStartIndex = archiveFileName.IndexOf('*');
-                var fileWildcardEndIndex = fileWildcardStartIndex >= 0 ? archiveFileName.Length - fileWildcardStartIndex : -1;
+                var archiveTestPath = _fileTarget.BuildFullFilePath(newFilePath, int.MaxValue, DateTime.MinValue);
+                var archiveFileName = Path.GetFileName(archiveTestPath).Replace(int.MaxValue.ToString(), "*");
+                var fileWildcardStartIndex = archiveFileName.IndexOf('*');
+                var lastStarIndex = archiveFileName.LastIndexOf('*');
+                var fileWildcardEndIndex = (fileWildcardStartIndex >= 0 && fileWildcardStartIndex == lastStarIndex)
+                    ? archiveFileName.Length - fileWildcardStartIndex
+                    : -1;
src/NLog/Targets/FileArchiveHandlers/BaseFileArchiveHandler.cs (1)

94-96: Single-wildcard strictness: good guard, consider centralizing helper.

This matches the “strict only with single '*'” intent. To keep semantics consistent across callers (e.g., RollingArchiveFileHandler), consider extracting a small helper to compute (startIndex, endIndex) to avoid divergence.

Example helper (internal):

internal static (int Start, int End) ComputeSingleStarIndices(string pattern)
{
    var s = pattern.IndexOf('*');
    return (s >= 0 && s == pattern.LastIndexOf('*'))
        ? (s, pattern.Length - s)
        : (s, -1);
}
📜 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 3b6ec8f and c7d7f67.

📒 Files selected for processing (3)
  • src/NLog/Targets/FileArchiveHandlers/BaseFileArchiveHandler.cs (2 hunks)
  • src/NLog/Targets/FileArchiveHandlers/LegacyArchiveFileNameHandler.cs (1 hunks)
  • src/NLog/Targets/FileArchiveHandlers/RollingArchiveFileHandler.cs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/NLog/Targets/FileArchiveHandlers/LegacyArchiveFileNameHandler.cs
🧰 Additional context used
🧬 Code graph analysis (1)
src/NLog/Targets/FileArchiveHandlers/RollingArchiveFileHandler.cs (2)
src/NLog/Targets/FileTarget.cs (3)
  • BuildFullFilePath (1090-1116)
  • DateTime (837-855)
  • DateTime (865-873)
src/NLog/Targets/FileArchiveHandlers/BaseFileArchiveHandler.cs (1)
  • GetMaxArchiveSequenceNo (116-119)
🔇 Additional comments (3)
src/NLog/Targets/FileArchiveHandlers/RollingArchiveFileHandler.cs (2)

106-110: Good early-exit with debug when directory is missing.

Prevents needless work and noisy warnings.


138-145: Sequence increment on ArchiveOldFileOnStartup looks correct.

The new unified flow is clearer and avoids nested returns.

src/NLog/Targets/FileArchiveHandlers/BaseFileArchiveHandler.cs (1)

159-164: Early return when no wildcard: correct and cheap.

Prevents unnecessary scanning; aligns with new EndIndex semantics.

@sonarqubecloud
Copy link

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/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant