Skip to content

Conversation

@oikku
Copy link
Contributor

@oikku oikku commented Jul 3, 2025

Windows uses \r\n, Linux and new Macs use \n. Older Macs use \r. Unicode adds more line endings. Old implementation didn't even replace single \r characters. Text editors like notepad++ treat single \r as new line character and log files aren't formatted as expected when using newline replacements.

Line ending characters replaced are those defined in unicode 16.0.0 specification section 5.8 Newline guidelines in table 5-1.

  • \r // CARRIAGE RETURN
  • \n // LINE FEED
  • \f // FORM FEED (FF)
  • \u0085 // NEXT LINE (NEL)
  • \u2028 // LINE SEPARATOR
  • \u2029 // PARAGRAPH SEPARATOR

…ters

Windows uses \r\n, Linux and new Macs use \n. Older Macs use \r. Unicode
adds more line endings. Old implementation didn't even replace single \r
characters. Text editors like notepad++ treat single \r as new line
character and log files aren't formatted as expected when using newline
replacements.

Line ending characters replaced are those defined in unicode 16.0.0
specification section 5.8 Newline guidelines in table 5-1.
@welcome
Copy link

welcome bot commented Jul 3, 2025

Thanks for opening this pull request!
We will try to review this soon! Please note that pull requests with unit tests are earlier accepted 👼

@coderabbitai
Copy link

coderabbitai bot commented Jul 3, 2025

"""

Walkthrough

The changes update the newline replacement logic in the ReplaceNewLinesLayoutRendererWrapper to explicitly handle a wider range of newline and line separator characters. The implementation now processes newlines using a character-by-character approach. Corresponding unit tests are added to verify behavior with various newline types, multicharacter replacements, consecutive newlines, and default replacement scenarios.

Changes

File(s) Change Summary
src/NLog/LayoutRenderers/Wrappers/ReplaceNewLinesLayoutRendererWrapper.cs Refactored newline replacement logic to handle multiple Unicode line separators and improved internal processing flow.
tests/NLog.UnitTests/LayoutRenderers/Wrappers/ReplaceNewLinesTests.cs Added eight new unit tests covering carriage return, Unicode newlines, multicharacter separators, consecutive newlines, end-of-text cases, and default replacements.

Poem

In the code where newlines used to hide,
Now every break is caught with pride.
Unicode or classic, all are seen,
Replaced with care, the logs stay clean.
With tests to check each hidden trace,
The rabbits hop—no lines misplaced!
🐇✨
"""


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b637da7 and 217f785.

📒 Files selected for processing (1)
  • src/NLog/LayoutRenderers/Wrappers/ReplaceNewLinesLayoutRendererWrapper.cs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/NLog/LayoutRenderers/Wrappers/ReplaceNewLinesLayoutRendererWrapper.cs

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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai 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

Documentation and Community

  • 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.

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/LayoutRenderers/Wrappers/ReplaceNewLinesLayoutRendererWrapper.cs (1)

75-75: Consider using HashSet for better performance.

The LineEndCharacters array contains the correct Unicode newline characters as per the Unicode 16.0.0 specification. However, using char[].Contains() in the processing loop results in O(n) lookups for each character.

Consider replacing the array with a HashSet for O(1) lookups:

-private static readonly char[] LineEndCharacters = new char[] { '\u000D', '\u000A', '\u0085', '\u2028', '\u000C', '\u2029' };
+private static readonly HashSet<char> LineEndCharacters = new HashSet<char> { '\u000D', '\u000A', '\u0085', '\u2028', '\u000C', '\u2029' };
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 274a809 and 3293429.

📒 Files selected for processing (2)
  • src/NLog/LayoutRenderers/Wrappers/ReplaceNewLinesLayoutRendererWrapper.cs (2 hunks)
  • tests/NLog.UnitTests/LayoutRenderers/Wrappers/ReplaceNewLinesTests.cs (1 hunks)
🔇 Additional comments (6)
src/NLog/LayoutRenderers/Wrappers/ReplaceNewLinesLayoutRendererWrapper.cs (3)

37-37: LGTM - Import added for Contains() method.

The System.Linq import is correctly added to support the Contains() method used in the new implementation.


82-82: LGTM - Efficient early detection of newline characters.

Using IndexOfAny is an efficient way to detect if any newline characters are present before processing the string character by character.


85-109: LGTM - Correct implementation of Unicode newline replacement.

The new character-by-character processing logic correctly handles:

  • Windows newline sequence \r\n as a single replacement (lines 93-97)
  • Other Unicode newline characters individually (lines 98-101)
  • Non-newline characters are preserved (lines 104-106)

The StringBuilder capacity calculation (line 87) is a good optimization to reduce memory allocations.

tests/NLog.UnitTests/LayoutRenderers/Wrappers/ReplaceNewLinesTests.cs (3)

139-149: LGTM - Test covers standalone carriage return replacement.

This test correctly verifies that standalone \r characters are now properly replaced, addressing the core issue mentioned in the PR description where single \r characters were not handled by the previous implementation.


151-161: LGTM - Comprehensive test for all Unicode newline characters.

This test thoroughly verifies that all Unicode newline characters defined in the implementation are correctly replaced with a custom replacement string. The test string includes:

  • \n (LF)
  • \r\n (CRLF)
  • \r (CR)
  • \u0085 (NEL)
  • \u2028 (LS)
  • \u000C (FF)
  • \u2029 (PS)

163-173: LGTM - Test verifies default replacement behavior.

This test ensures that all Unicode newline characters are correctly replaced with the default space character, providing comprehensive coverage of the enhanced functionality.

/// <docgen category='Layout Options' order='50' />
public string ReplaceNewLines { get => Replacement; set => Replacement = value; }

private static readonly char[] LineEndCharacters = new char[] { '\u000D', '\u000A', '\u0085', '\u2028', '\u000C', '\u2029' };
Copy link
Contributor

@snakefoot snakefoot Jul 5, 2025

Choose a reason for hiding this comment

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

Instead of using unicode-numbers directly, then I prefer the more human-readable standard chars when possible. Ex \n + \r + etc.

@pull-request-size pull-request-size bot added size/L and removed size/M labels Jul 5, 2025
Copy link
Contributor

@snakefoot snakefoot left a comment

Choose a reason for hiding this comment

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

Thank you so much for being patient with me and all my comments.

Very happy about the result, and think it is a good improvement together with the new unit-tests.

@snakefoot snakefoot enabled auto-merge (squash) July 5, 2025 15:52
@snakefoot snakefoot added the enhancement Improvement on existing feature label Jul 5, 2025
@snakefoot
Copy link
Contributor

Closing an re-opening the pull-request to tickle the build-servers.

@snakefoot snakefoot closed this Jul 5, 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 size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants