Skip to content

LoggingRule - Added WriteTo method to match NLog.config#6107

Merged
snakefoot merged 1 commit intoNLog:devfrom
snakefoot:LoggingRuleWriteTo
Mar 1, 2026
Merged

LoggingRule - Added WriteTo method to match NLog.config#6107
snakefoot merged 1 commit intoNLog:devfrom
snakefoot:LoggingRuleWriteTo

Conversation

@snakefoot
Copy link
Copy Markdown
Contributor

No description provided.

@snakefoot snakefoot added the enhancement Improvement on existing feature label Mar 1, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 1, 2026

Walkthrough

Two new public overloads of WriteTo were added to LoggingRule: one accepting a params array and one accepting IEnumerable<Target>. The IEnumerable overload validates input and replaces the internal targets collection under a lock. A unit test verifies adding multiple targets and ToString() output.

Changes

Cohort / File(s) Summary
Core API
src/NLog/Config/LoggingRule.cs
Added public void WriteTo(params Target[] targets) and public void WriteTo(IEnumerable<Target> targets). The params overload delegates to the IEnumerable overload, which validates non-null input and replaces the internal _targets collection under a lock when the reference differs.
Tests
tests/NLog.UnitTests/Config/ConfigApiTests.cs
Added LogRuleToStringTest_WriteToMultiple_targets to verify WriteTo can replace targets with multiple FileTarget instances and that ToString() reflects both targets.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I nibble code with care and cheer,
New WriteTo paths now hop near.
Targets swap with calm, locked grace,
Tests applaud the tidy place.

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive No pull request description was provided by the author, making it impossible to assess relevance to the changeset. Add a pull request description explaining the purpose of the WriteTo method and how it enhances the LoggingRule API.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding a WriteTo method to LoggingRule to match NLog.config functionality.

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

✨ Finishing Touches
🧪 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.

@snakefoot snakefoot force-pushed the LoggingRuleWriteTo branch 2 times, most recently from dfae188 to 8be5ee5 Compare March 1, 2026 11:57
Copy link
Copy Markdown

@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: 1

🧹 Nitpick comments (1)
tests/NLog.UnitTests/Config/ConfigApiTests.cs (1)

309-321: Add one regression case for IEnumerable<Target> aliasing.

This test is good for the params overload. Consider adding a case around Line 317 with loggingRule.WriteTo(loggingRule.Targets) to guard against accidental target loss when input is backed by the same list.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/NLog.UnitTests/Config/ConfigApiTests.cs` around lines 309 - 321, Add a
regression case to LogRuleToStringTest_WriteToMultiple_targets that exercises
aliasing in LoggingRule.WriteTo by calling
loggingRule.WriteTo(loggingRule.Targets) (the IEnumerable<Target> overload)
after the initial WriteTo(target, target2) and then assert the Targets
collection still has Count == 2, contains the same target and target2 instances
(use Assert.Same or equivalent), and that ToString() still matches the expected
string; this ensures WriteTo does not drop entries when the input enumerable is
the same backing list as LoggingRule.Targets.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/NLog/Config/LoggingRule.cs`:
- Around line 255-262: In WriteTo (class LoggingRule) materialize the incoming
targets enumerable before clearing or modifying the internal _targets to avoid
losing items when targets references the same list; e.g., after
Guard.ThrowIfNull(targets) copy targets into a new collection (e.g., a list) and
then lock(_targets) clear and AddRange from that materialized collection so the
enumeration does not see the cleared backing store.

---

Nitpick comments:
In `@tests/NLog.UnitTests/Config/ConfigApiTests.cs`:
- Around line 309-321: Add a regression case to
LogRuleToStringTest_WriteToMultiple_targets that exercises aliasing in
LoggingRule.WriteTo by calling loggingRule.WriteTo(loggingRule.Targets) (the
IEnumerable<Target> overload) after the initial WriteTo(target, target2) and
then assert the Targets collection still has Count == 2, contains the same
target and target2 instances (use Assert.Same or equivalent), and that
ToString() still matches the expected string; this ensures WriteTo does not drop
entries when the input enumerable is the same backing list as
LoggingRule.Targets.

ℹ️ Review info

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a385da2 and 8be5ee5.

📒 Files selected for processing (2)
  • src/NLog/Config/LoggingRule.cs
  • tests/NLog.UnitTests/Config/ConfigApiTests.cs

@snakefoot snakefoot force-pushed the LoggingRuleWriteTo branch from 8be5ee5 to bb507cb Compare March 1, 2026 12:02
Copy link
Copy Markdown

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

♻️ Duplicate comments (1)
src/NLog/Config/LoggingRule.cs (1)

255-264: ⚠️ Potential issue | 🟠 Major

Materialize targets before clearing _targets to avoid data loss.

ReferenceEquals does not protect deferred enumerables over _targets. At Line 262, _targets.Clear() can run before Line 263 enumerates targets, resulting in an empty replacement set.

💡 Proposed fix
 public void WriteTo(IEnumerable<Target> targets)
 {
     Guard.ThrowIfNull(targets);
     lock (_targets)
     {
-        if (!ReferenceEquals(targets, _targets))
-        {
-            _targets.Clear();
-            _targets.AddRange(targets);
-        }
+        if (ReferenceEquals(targets, _targets))
+            return;
+
+        var newTargets = new List<Target>(targets);
+        _targets.Clear();
+        _targets.AddRange(newTargets);
     }
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/NLog/Config/LoggingRule.cs` around lines 255 - 264, The WriteTo method
currently compares ReferenceEquals(targets, _targets) then clears _targets and
enumerates targets, which can drop items when targets is a deferred enumerable
over _targets; to fix, inside WriteTo (class LoggingRule) when the references
differ, first materialize the incoming targets into a concrete collection (e.g.,
call ToList() on targets) and then Clear and AddRange from that materialized
list so enumeration is safe and no data is lost; preserve the existing
ReferenceEquals check and perform materialization only when references are not
equal.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@src/NLog/Config/LoggingRule.cs`:
- Around line 255-264: The WriteTo method currently compares
ReferenceEquals(targets, _targets) then clears _targets and enumerates targets,
which can drop items when targets is a deferred enumerable over _targets; to
fix, inside WriteTo (class LoggingRule) when the references differ, first
materialize the incoming targets into a concrete collection (e.g., call ToList()
on targets) and then Clear and AddRange from that materialized list so
enumeration is safe and no data is lost; preserve the existing ReferenceEquals
check and perform materialization only when references are not equal.

ℹ️ Review info

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8be5ee5 and bb507cb.

📒 Files selected for processing (2)
  • src/NLog/Config/LoggingRule.cs
  • tests/NLog.UnitTests/Config/ConfigApiTests.cs
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/NLog.UnitTests/Config/ConfigApiTests.cs

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Mar 1, 2026

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Mar 1, 2026

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant