Skip to content

WhenEmpty - Optimize when IsFixedText#6074

Merged
snakefoot merged 1 commit intoNLog:devfrom
snakefoot:WhenEmptyFixedText
Jan 8, 2026
Merged

WhenEmpty - Optimize when IsFixedText#6074
snakefoot merged 1 commit intoNLog:devfrom
snakefoot:WhenEmptyFixedText

Conversation

@snakefoot
Copy link
Contributor

No description provided.

@coderabbitai
Copy link

coderabbitai bot commented Jan 8, 2026

Walkthrough

The WhenEmptyLayoutRendererWrapper initialization is refactored to split handling into two branches: when the inner layout is fixed text, a new ResolveFixedWhenEmpty helper determines the string renderer; otherwise, existing rendering logic is preserved for dynamic layouts.

Changes

Cohort / File(s) Summary
WhenEmptyLayoutRendererWrapper Refactoring
src/NLog/LayoutRenderers/Wrappers/WhenEmptyLayoutRendererWrapper.cs
Splits initialization condition into two branches: introduces ResolveFixedWhenEmpty helper for fixed-text inner layouts to resolve when-empty strings; preserves existing rendering logic for SimpleStringText layouts. Changes control flow for fixed-inner cases.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A wrapper wrapped in logic so neat,
Fixed and flexible, now they meet,
When empty falls to reason so keen,
A helper born, most clever I've seen!
NLog hops on, with rendering bright,
Control flows dance through refactored light.

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description check ⚠️ Warning No description was provided by the author; this is a very lenient check that should fail when the description is completely off-topic or unrelated. Add a brief description explaining the optimization and its impact on performance or behavior.
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title 'WhenEmpty - Optimize when IsFixedText' clearly describes the main optimization in the changeset, which refactors WhenEmptyLayoutRendererWrapper to optimize handling of fixed text scenarios.

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

✨ Finishing touches
  • 📝 Generate docstrings

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.

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/WhenEmptyLayoutRendererWrapper.cs (1)

88-104: Logic is correct and well-structured.

The method correctly handles all combinations:

  • Non-empty inner fixed text → always return inner (never empty)
  • Empty inner fixed text with fixed whenEmpty → return whenEmpty constant
  • Empty inner fixed text with dynamic whenEmpty → render whenEmpty at runtime
  • Other cases → return null to fall back to general rendering

The defensive null coalescing operators on lines 92 and 96 are good practice.

📝 Consider adding XML documentation

Adding XML comments would clarify the optimization strategy:

+/// <summary>
+/// Resolves the optimal string renderer when the inner layout is fixed text.
+/// Returns a pre-determined renderer if possible, or null to fall back to general rendering.
+/// </summary>
 private static Func<LogEventInfo, string>? ResolveFixedWhenEmpty(SimpleLayout innerLayout, SimpleLayout whenEmptyLayout)
📜 Review details

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fba8395 and ded66c1.

📒 Files selected for processing (1)
  • src/NLog/LayoutRenderers/Wrappers/WhenEmptyLayoutRendererWrapper.cs
🧰 Additional context used
🧬 Code graph analysis (1)
src/NLog/LayoutRenderers/Wrappers/WhenEmptyLayoutRendererWrapper.cs (1)
src/NLog/Layouts/SimpleLayout.cs (5)
  • SimpleLayout (70-73)
  • SimpleLayout (79-82)
  • SimpleLayout (89-92)
  • SimpleLayout (100-104)
  • SimpleLayout (106-140)
🔇 Additional comments (2)
src/NLog/LayoutRenderers/Wrappers/WhenEmptyLayoutRendererWrapper.cs (2)

73-76: LGTM! Smart optimization for fixed text scenarios.

The new branch correctly identifies when the inner layout produces fixed text and delegates to ResolveFixedWhenEmpty to determine the optimal renderer at initialization time. This eliminates runtime checks for scenarios where the inner layout result is predetermined.


77-84: Clean separation of concerns.

The refactored condition now specifically handles SimpleStringText inner layouts, with the fixed text case handled separately above. This improves code clarity while preserving the existing optimization for simple string text scenarios.

@snakefoot snakefoot closed this Jan 8, 2026
@snakefoot snakefoot reopened this Jan 8, 2026
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 8, 2026

Quality Gate Failed Quality Gate failed

Failed conditions
60.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@snakefoot snakefoot merged commit be28e5b into NLog:dev Jan 8, 2026
4 of 6 checks passed
@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