Skip to content

CsvLayout - Faster CSV encoding with INoAllocationStringValueRenderer#6083

Merged
snakefoot merged 3 commits intoNLog:devfrom
snakefoot:CsvLayoutStringValue
Jan 31, 2026
Merged

CsvLayout - Faster CSV encoding with INoAllocationStringValueRenderer#6083
snakefoot merged 3 commits intoNLog:devfrom
snakefoot:CsvLayoutStringValue

Conversation

@snakefoot
Copy link
Contributor

@snakefoot snakefoot commented Jan 31, 2026

Extending #6082 to also apply for CsvLayout

@coderabbitai
Copy link

coderabbitai bot commented Jan 31, 2026

Walkthrough

Introduces a new internal interface INoAllocationStringValueRenderer that extends IStringValueRenderer with a GetFormattedStringNoAllocation method. Multiple layout renderers are updated to implement this interface, and the CSV, JSON, and typed layout systems are optimized to use the no-allocation path when available. The existing IStringValueRenderer interface is made internal.

Changes

Cohort / File(s) Summary
Interface Definitions
src/NLog/Internal/INoAllocationStringValueRenderer.cs, src/NLog/Internal/IStringValueRenderer.cs
New internal interface INoAllocationStringValueRenderer introduced with GetFormattedStringNoAllocation method. Existing IStringValueRenderer is now internal; documentation refined from "must not" to "should not" regarding AppDomainFixedOutput attribute.
Layout Renderers - Interface Adoption
src/NLog/LayoutRenderers/LiteralLayoutRenderer.cs, src/NLog/LayoutRenderers/LoggerNameLayoutRenderer.cs, src/NLog/LayoutRenderers/MessageLayoutRenderer.cs, src/NLog/LayoutRenderers/GdcLayoutRenderer.cs, src/NLog/LayoutRenderers/EventPropertiesLayoutRenderer.cs
Multiple renderers switch from IStringValueRenderer to INoAllocationStringValueRenderer with explicit interface implementations providing no-allocation formatting paths. Each returns formatted string when formatting is simple, or null when complex formatting is required.
Layout Renderers - Major Refactor
src/NLog/LayoutRenderers/LevelLayoutRenderer.cs
Significant restructuring: replaces IStringValueRenderer with INoAllocationStringValueRenderer, introduces centralized GetLogLevelStringValue method, adds GenerateLevelNames and level formatting helpers, and implements internal caching for format state and level names.
Layout Renderers - Null Safety
src/NLog/LayoutRenderers/AppSettingLayoutRenderer.cs
When Item is null or empty, GetStringValue returns Default only if non-null; otherwise returns empty string instead of potentially null Default.
CSV Layout System
src/NLog/Layouts/CSV/CsvColumn.cs, src/NLog/Layouts/CSV/CsvLayout.cs
CsvColumn adds SimpleStringValue caching via internal ResolveStringValueMethod for simple layout renderers. CsvLayout integrates no-allocation path: RenderColumnLayout accepts optional INoAllocationStringValueRenderer parameter and routes through AppendStringValue helpers when available.
Typed Layout System
src/NLog/Layouts/Typed/ValueTypeLayoutInfo.cs
Adds internal SimpleStringValue property with ResolveStringValueMethod helper to cache no-allocation string renderer for simple layout scenarios.
JSON Layout System
src/NLog/Layouts/JSON/JsonAttribute.cs
Introduces RenderAppendJsonStringValue helper and short-circuit optimization in RenderAppendJsonValue to render simple string values directly when Encode is true, avoiding generic rendering path.
Serialization & Tests
src/NLog/Targets/DefaultJsonSerializer.cs, tests/NLog.UnitTests/ApiTests.cs
DefaultJsonSerializer adds null-safeguarding with ?? string.Empty for IFormattable formatting on NETFRAMEWORK. ApiTests excludes LiteralLayoutRenderer from IStringValueRenderer AppDomainFixedOutput attribute test.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A new interface hops into view,
INoAllocationStringValueRenderer's debut!
Renderers switch paths with grace so fine,
CSV and JSON in harmony align,
No extra allocations—just clean design! 🎉

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 17.95% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The pull request has no description provided by the author, making it impossible to assess whether there is any explanation of the changes. Add a descriptive pull request description explaining the changes, objectives, and rationale for the CsvLayout optimization and interface refactoring.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: introducing faster CSV encoding for simple log event messages by leveraging no-allocation string rendering.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@sonarqubecloud
Copy link

@snakefoot snakefoot merged commit b922e0c into NLog:dev Jan 31, 2026
5 of 6 checks passed
@snakefoot snakefoot changed the title CsvLayout - Faster CSV encoding for simple logevent messages CsvLayout - Faster CSV encoding with INoAllocationStringValueRenderer Jan 31, 2026
@snakefoot snakefoot added this to the 6.1.0 milestone Jan 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant