- Notifications
You must be signed in to change notification settings - Fork1.4k
LoggingConfigurationParser - Report unrecognized options in targets and rules section#6045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:dev
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
coderabbitaibot commentedNov 19, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
WalkthroughAdds validation in the configuration parser to detect unrecognized attribute key/value pairs on Changes
Sequence DiagramsequenceDiagram participant Loader as ConfigLoader participant Parser as LoggingConfigurationParser participant Element as Rules/Targets Element participant Validator as RemainingValuesChecker participant Error as ConfigException Loader->>Parser: Parse configuration XML Parser->>Element: Parse known attributes/flags (e.g., async, rule positions) Parser->>Validator: Inspect Element.Values for leftovers alt Throwing enabled and leftovers present Validator->>Error: Throw ConfigException for each unrecognized key/value Error-->>Parser: Exception propagated Parser-->>Loader: Parse fails else No leftovers or throwing disabled Validator-->>Parser: Validation OK / ignored Parser-->>Loader: Continue processing endEstimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
🧰 Additional context used🧬 Code graph analysis (1)tests/NLog.UnitTests/Config/XmlConfigTests.cs (2)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
🔇 Additional comments (3)
Thanks for usingCodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this 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 (2)
src/NLog/Config/LoggingConfigurationParser.cs (1)
550-558:Consider renaming variable for clarity.The variable
targetsOptionsis used within the rules validation context, which is confusing. Consider renaming it toruleOption,option, orruleAttributeto better reflect its purpose.Apply this diff:
if (rulesElement.Values.Count > 0) {- foreach (var targetsOptions in rulesElement.Values)+ foreach (var ruleOption in rulesElement.Values) {- var configException = new NLogConfigurationException($"Unrecognized value '{targetsOptions.Key}'='{targetsOptions.Value}' in section '{rulesElement.Name}'");+ var configException = new NLogConfigurationException($"Unrecognized value '{ruleOption.Key}'='{ruleOption.Value}' in section '{rulesElement.Name}'"); if (MustThrowConfigException(configException)) throw configException; } }tests/NLog.UnitTests/Config/XmlConfigTests.cs (1)
197-223:Good test coverage for graceful degradation.The test appropriately validates that unknown attributes on
<targets>and<rules>elements don't break logging functionality when exception throwing is disabled. This aligns well with the new validation logic inLoggingConfigurationParser.cs.Optional enhancement: Consider asserting that warnings about the unknown attributes (
option='unknown'andbingo='tada') are logged to the InternalLogger, similar to howXmlConfig_attributes_shouldNotLogWarningsToInternalLogcaptures internal log output. This would provide more complete validation that the configuration parser properly detects and reports these issues.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/NLog/Config/LoggingConfigurationParser.cs(2 hunks)tests/NLog.UnitTests/Config/XmlConfigTests.cs(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/NLog/Config/LoggingConfigurationParser.cs (1)
src/NLog/NLogConfigurationException.cs (4)
NLogConfigurationException(51-53)NLogConfigurationException(59-62)NLogConfigurationException(98-101)NLogConfigurationException(115-118)
🔇 Additional comments (1)
src/NLog/Config/LoggingConfigurationParser.cs (1)
856-867:LGTM!The validation logic correctly identifies and rejects unrecognized attributes in the targets section while properly excluding the allowed "async" attribute from validation.
cbe7b3d tof9c797cCompareThere was a problem hiding this 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
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
tests/NLog.UnitTests/Config/XmlConfigTests.cs (1)
215-215:Fix the logger name to match the test method.The logger name "InvalidInternalLogLevel_shouldNotBreakLogging" doesn't match the test method name "InvalidNLogAttributeValues_shouldNotBreakLogging", likely from a copy-paste oversight.
Apply this diff:
- var logger = logFactory.GetLogger("InvalidInternalLogLevel_shouldNotBreakLogging");+ var logger = logFactory.GetLogger("InvalidNLogAttributeValues_shouldNotBreakLogging");
🧹 Nitpick comments (1)
tests/NLog.UnitTests/Config/XmlConfigTests.cs (1)
197-223:Consider adding test coverage for the exception-throwing scenario.The current test validates that unknown attributes don't break logging when exceptions are suppressed. However, there's no corresponding test that verifies exceptions ARE thrown when
throwExceptions='true'orthrowConfigExceptions='true'is set.Following the pattern of
ShoudThrowExceptionOnDuplicateAttributeWhenOptionIsEnabledTest(line 433), consider adding a similar test for unknown attributes on targets and rules elements.Example test structure:
[Fact]publicvoidShouldThrowExceptionOnUnknownAttributeWhenOptionIsEnabledTest(){Assert.Throws<NLogConfigurationException>(()=>{newLogFactory().Setup().LoadConfigurationFromXml(@" <nlog throwExceptions='true'> <targets option='unknown'> <target name='debug' type='Debug' layout='${message}' /> </targets> <rules> <logger name='*' minlevel='info' appendto='debug' /> </rules> </nlog>");});Assert.Throws<NLogConfigurationException>(()=>{newLogFactory().Setup().LoadConfigurationFromXml(@" <nlog throwConfigExceptions='true'> <targets> <target name='debug' type='Debug' layout='${message}' /> </targets> <rules bingo='tada'> <logger name='*' minlevel='info' appendto='debug' /> </rules> </nlog>");});}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/NLog/Config/LoggingConfigurationParser.cs(2 hunks)tests/NLog.UnitTests/Config/XmlConfigTests.cs(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- src/NLog/Config/LoggingConfigurationParser.cs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: task-list-completed
🔇 Additional comments (1)
tests/NLog.UnitTests/Config/XmlConfigTests.cs (1)
204-204:LGTM! Good test coverage for unknown attribute validation.The addition of unknown attributes (
option='unknown'on targets andbingo='tada'on rules) appropriately exercises the new parser validation feature in the no-throw scenario.Also applies to: 207-207
f9c797c to173a9c0Compare


No description provided.