Skip to content

AssemblyExtensionTypes - Reduce code complexity#6071

Merged
snakefoot merged 1 commit intoNLog:devfrom
snakefoot:extension_registration
Jan 3, 2026
Merged

AssemblyExtensionTypes - Reduce code complexity#6071
snakefoot merged 1 commit intoNLog:devfrom
snakefoot:extension_registration

Conversation

@snakefoot
Copy link
Contributor

No description provided.

@coderabbitai
Copy link

coderabbitai bot commented Jan 3, 2026

Warning

Rate limit exceeded

@snakefoot has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 26 minutes and 54 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 3364b9e and e3efd12.

📒 Files selected for processing (7)
  • src/NLog/Config/AssemblyExtensionTypes.cs
  • src/NLog/Config/AssemblyExtensionTypes.tt
  • src/NLog/Config/ConfigurationItemFactory.cs
  • src/NLog/Config/Factory.cs
  • src/NLog/Config/MethodFactory.cs
  • src/NLog/LayoutRenderers/ExceptionDataLayoutRenderer.cs
  • tests/NLog.UnitTests/ApiTests.cs

Walkthrough

Public registration APIs were renamed from skipCheckExists to checkTypeExists; registration calls now pass an explicit existence-guard flag into localized factories. Core Factory registration methods gained an optional checkTypeExists parameter and early-return guards; MethodFactory.Initialize inverted the boolean sent to itemRegistration.

Changes

Cohort / File(s) Summary
Assembly Extension Type Registration
src/NLog/Config/AssemblyExtensionTypes.cs, src/NLog/Config/AssemblyExtensionTypes.tt
Renamed parameter skipCheckExistscheckTypeExists across public Register*Types methods. Introduced local factory variables (e.g., targetFactory, layoutFactory, conditionMethodFactory) and switched from per-type alias-existence checks to registrations that accept the checkTypeExists guard. NETFRAMEWORK branches routed through the new local factories.
Configuration Item Factory
src/NLog/Config/ConfigurationItemFactory.cs
Updated initialization lambdas to use checkTypeExists. Replaced SafeRegisterNamedType with direct RegisterNamedType(..., checkTypeExists) calls, removed/adjusted framework-specific code paths, and refactored internal helpers (RegisterAllTargets/Layouts/LayoutRenderers/Filters/ConditionMethods/TimeSources) to accept the flag.
Base Factory Registration
src/NLog/Config/Factory.cs
Added optional parameter bool checkTypeExists = false to RegisterType<TType>(...) and RegisterNamedType(...). These methods now early-return when checkTypeExists is true and the alias/name already exists. Removed the previous public existence-check API in favor of the guard parameter.
Method Factory
src/NLog/Config/MethodFactory.cs
Inverted the boolean argument passed to itemRegistration.Invoke(...) inside Initialize() so the flag is true when _nameToMethodDetails.Count is non-zero, changing the existence-signal semantics.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

A rabbit rewrote the registry dance,
Flags renamed to give each call a chance,
Factories funnel registrations neat,
Booleans flipped to steady the beat,
Hops of refactor — tidy and brisk 🐰✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.88% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Description check ❓ Inconclusive No description was provided by the author; however, the changeset is clearly related to AssemblyExtensionTypes refactoring as indicated by the title and file modifications. Add a description explaining the rationale behind the parameter rename and factory variable consolidation to help reviewers understand the refactoring motivation.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main refactoring: renaming parameters, introducing local factory variables, and restructuring registration logic to reduce complexity across AssemblyExtensionTypes.

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: 2

📜 Review details

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bb99454 and ca786ba.

📒 Files selected for processing (5)
  • src/NLog/Config/AssemblyExtensionTypes.cs
  • src/NLog/Config/AssemblyExtensionTypes.tt
  • src/NLog/Config/ConfigurationItemFactory.cs
  • src/NLog/Config/Factory.cs
  • src/NLog/Config/MethodFactory.cs
🧰 Additional context used
🧬 Code graph analysis (1)
src/NLog/Config/Factory.cs (2)
src/NLog/Config/ConfigurationItemFactory.cs (1)
  • RegisterType (355-366)
src/NLog/Config/IFactory.cs (1)
  • RegisterType (46-46)
🔇 Additional comments (8)
src/NLog/Config/MethodFactory.cs (1)

100-101: LGTM! Semantic inversion clarifies intent.

The boolean logic has been correctly inverted: checkTypeExists = true when items already exist, signaling that registrations should skip duplicates. This is clearer than the previous skipCheckExists semantics.

src/NLog/Config/ConfigurationItemFactory.cs (2)

144-250: LGTM! Consistent parameter renaming across all factory initializations.

The lambda parameter has been systematically renamed from skipCheckExists to checkTypeExists across all factory initialization calls, maintaining consistency with the new semantics throughout the initialization flow.


474-559: LGTM! Registration methods consistently use the new parameter.

All private registration methods now accept the checkTypeExists parameter and consistently pass it to the underlying factory registration calls, successfully consolidating the existence-check logic.

src/NLog/Config/Factory.cs (3)

75-76: LGTM! Boolean inversion correctly implements new semantics.

The inverted logic correctly sets checkTypeExists = true when items already exist, enabling duplicate prevention in subsequent registrations.


85-96: LGTM! Registration with optional existence check is correctly implemented.

The new checkTypeExists parameter (defaulting to false) allows callers to optionally prevent duplicate registrations. The early return logic correctly skips registration when both the flag is set and the type already exists, while maintaining backward compatibility.


134-162: LGTM! Consistent existence-check pattern for named type registration.

The RegisterNamedType method mirrors the same optional existence-check pattern as RegisterType<TType>, ensuring consistency across registration paths.

src/NLog/Config/AssemblyExtensionTypes.tt (1)

252-275: LGTM! Condition method guard logic correctly prevents duplicates.

The registration pattern if (!checkTypeExists || !conditionMethodFactory.CheckTypeAliasExists("...")) correctly ensures methods are registered when either existence checking is disabled or the method doesn't exist, effectively preventing duplicates while maintaining flexibility.

src/NLog/Config/AssemblyExtensionTypes.cs (1)

251-274: LGTM! Generated guard logic correctly implements duplicate prevention.

The condition method registration guards correctly implement the new existence-check pattern, preventing duplicates when checkTypeExists = true while allowing unconditional registration when false.

@snakefoot snakefoot force-pushed the extension_registration branch 2 times, most recently from 3364b9e to 6ef5420 Compare January 3, 2026 10:29
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: 1

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

103-103: Inconsistent parameter usage in NETFRAMEWORK block.

Line 103 doesn't pass the checkTypeExists parameter when registering AppSettingLayoutRenderer. Since this is generated code, the fix should be applied to the template file AssemblyExtensionTypes.tt at line 134.

src/NLog/Config/AssemblyExtensionTypes.tt (1)

134-134: Inconsistent parameter usage in NETFRAMEWORK block.

Line 134 doesn't pass the checkTypeExists parameter when registering AppSettingLayoutRenderer, while line 65 does pass it for EventLogTarget. This inconsistency should be fixed to prevent potential duplicate registrations.

🔎 Proposed fix
 #if NETFRAMEWORK
-            layoutRendererFactory.RegisterType<NLog.LayoutRenderers.AppSettingLayoutRenderer>("appsetting");
+            layoutRendererFactory.RegisterType<NLog.LayoutRenderers.AppSettingLayoutRenderer>("appsetting", checkTypeExists);
 #endif
📜 Review details

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ca786ba and 3364b9e.

📒 Files selected for processing (5)
  • src/NLog/Config/AssemblyExtensionTypes.cs
  • src/NLog/Config/AssemblyExtensionTypes.tt
  • src/NLog/Config/ConfigurationItemFactory.cs
  • src/NLog/Config/Factory.cs
  • src/NLog/Config/MethodFactory.cs
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/NLog/Config/Factory.cs
  • src/NLog/Config/MethodFactory.cs
🧰 Additional context used
🧬 Code graph analysis (2)
src/NLog/Config/ConfigurationItemFactory.cs (3)
src/NLog/Config/Factory.cs (1)
  • Initialize (66-83)
src/NLog/Config/MethodFactory.cs (1)
  • Initialize (91-108)
src/NLog/Config/AssemblyExtensionTypes.cs (7)
  • AssemblyExtensionTypes (39-277)
  • RegisterTargetTypes (42-77)
  • RegisterLayoutTypes (79-95)
  • RegisterLayoutRendererTypes (97-215)
  • RegisterFilterTypes (217-226)
  • RegisterConditionTypes (237-275)
  • RegisterTimeSourceTypes (228-235)
src/NLog/Config/AssemblyExtensionTypes.cs (4)
src/NLog/Config/ConfigurationItemFactory.cs (6)
  • ConfigurationItemFactory (55-560)
  • ConfigurationItemFactory (94-97)
  • ConfigurationItemFactory (99-120)
  • RegisterTypeProperties (368-380)
  • RegisterTypeProperties (382-394)
  • RegisterType (355-366)
src/NLog/Config/Factory.cs (3)
  • RegisterType (85-96)
  • RegisterType (98-107)
  • RegisterType (114-127)
src/NLog/Config/MethodFactory.cs (3)
  • RegisterOneParameter (312-320)
  • RegisterTwoParameters (322-330)
  • RegisterThreeParameters (332-340)
src/NLog/Config/LoggingRule.cs (1)
  • ToString (309-354)
⏰ 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 (11)
src/NLog/Config/ConfigurationItemFactory.cs (3)

144-249: LGTM! Consistent refactoring of initialization lambdas.

The initialization pattern has been cleanly updated across all factory types (TargetFactory, LayoutFactory, LayoutRendererFactory, AmbientRendererFactory, FilterFactory, TimeSourceFactory, ConditionMethodFactory) to use the new checkTypeExists parameter instead of skipCheckExists. The changes are uniform and maintain the dependency chain initialization logic.


474-508: LGTM! RegisterAllTargets refactored consistently.

The method signature and all internal registration calls have been updated to use the checkTypeExists parameter. All calls to RegisterNamedType for external package targets (DiagnosticSource, Database, WindowsEventLog, etc.) consistently pass the flag.


510-559: LGTM! All remaining RegisterAll methods refactored consistently.

The RegisterAllLayouts, RegisterAllLayoutRenderers, RegisterAllFilters, RegisterAllConditionMethods, and RegisterAllTimeSources methods all follow the same refactored pattern with the checkTypeExists parameter properly threaded through to AssemblyExtensionTypes and all RegisterNamedType calls.

src/NLog/Config/AssemblyExtensionTypes.cs (4)

42-77: LGTM! RegisterTargetTypes refactored cleanly.

The method now accepts the checkTypeExists parameter and uses a local targetFactory variable to centralize all registrations. All target types are registered consistently with the existence check flag, including the NETFRAMEWORK-specific EventLogTarget.


79-95: LGTM! RegisterLayoutTypes follows the refactored pattern.

The method uses a local layoutFactory variable and consistently passes the checkTypeExists parameter to all layout registrations.


217-235: LGTM! RegisterFilterTypes and RegisterTimeSourceTypes refactored correctly.

Both methods introduce local factory variables and consistently use the checkTypeExists parameter for all type registrations.


237-275: LGTM! RegisterConditionTypes correctly implements conditional registration.

The method uses conditionMethodFactory and implements proper guard logic with CheckTypeAliasExists to prevent duplicate registrations of condition methods (length, equals, strequals, contains, starts-with, ends-with). The pattern if (!checkTypeExists || !CheckTypeAliasExists(...)) correctly ensures registrations only occur when appropriate.

src/NLog/Config/AssemblyExtensionTypes.tt (4)

59-93: LGTM! RegisterTargetTypes template generates correct code.

The template properly introduces the local targetFactory variable and generates all registrations with the checkTypeExists parameter, including the NETFRAMEWORK-specific EventLogTarget.


95-126: LGTM! RegisterLayoutTypes template follows refactored pattern.

The template correctly generates the method with local layoutFactory variable and threads the checkTypeExists parameter through all layout registrations.


176-236: LGTM! RegisterFilterTypes and RegisterTimeSourceTypes templates are correct.

Both template sections properly introduce local factory variables and generate registrations with the checkTypeExists parameter consistently threaded through.


238-276: LGTM! RegisterConditionTypes template implements correct conditional logic.

The template generates proper guard conditions using CheckTypeAliasExists to prevent duplicate registrations of condition methods. The logic if (!checkTypeExists || !CheckTypeAliasExists(...)) is correctly applied to all parameterized condition method registrations.

@snakefoot snakefoot force-pushed the extension_registration branch 6 times, most recently from 4844858 to 55dc760 Compare January 3, 2026 10:47
@snakefoot snakefoot force-pushed the extension_registration branch from 55dc760 to e3efd12 Compare January 3, 2026 10:48
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 3, 2026

@snakefoot snakefoot merged commit 218aa12 into NLog:dev Jan 3, 2026
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