LoggingRule - Added WriteTo method to match NLog.config#6107
LoggingRule - Added WriteTo method to match NLog.config#6107
Conversation
WalkthroughTwo 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
dfae188 to
8be5ee5
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
tests/NLog.UnitTests/Config/ConfigApiTests.cs (1)
309-321: Add one regression case forIEnumerable<Target>aliasing.This test is good for the
paramsoverload. Consider adding a case around Line 317 withloggingRule.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.
8be5ee5 to
bb507cb
Compare
There was a problem hiding this comment.
♻️ Duplicate comments (1)
src/NLog/Config/LoggingRule.cs (1)
255-264:⚠️ Potential issue | 🟠 MajorMaterialize
targetsbefore clearing_targetsto avoid data loss.
ReferenceEqualsdoes not protect deferred enumerables over_targets. At Line 262,_targets.Clear()can run before Line 263 enumeratestargets, 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
📒 Files selected for processing (2)
src/NLog/Config/LoggingRule.cstests/NLog.UnitTests/Config/ConfigApiTests.cs
🚧 Files skipped from review as they are similar to previous changes (1)
- tests/NLog.UnitTests/Config/ConfigApiTests.cs
|
|



No description provided.