Sitelet https://github.com/NLog/NLog/pull/6174
Skip to content

Reduce AOT file-size by changing PropertyTypeConverter to avoid Dictionary for conversion mapping - #6174

Merged
snakefoot merged 1 commit into
NLog:devfrom
snakefoot:objectconverter
May 16, 2026
Merged

snakefoot merged 1 commit into
NLog:devfrom
snakefoot:objectconverter

Conversation

@snakefoot

@snakefoot snakefoot commented May 16, 2026 •

Copy link
Copy Markdown
Contributor

Reducing upfront type-dependency resolving until actually needed (entering mapping-method)

Parsing Enum from empty string (or null) now fails upfront, instead of scanning for other conversions and casting.

@coderabbitai

coderabbitai Bot commented May 16, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 349269cb-bc6f-45bd-bf88-13e5a97804f1

📥 Commits

Reviewing files that changed from the base of the PR and between 97865a6 and cdb55cb.

📒 Files selected for processing (4)
  • src/NLog/Config/LoggingConfiguration.cs
  • src/NLog/Config/PropertyTypeConverter.cs
  • src/NLog/Internal/PropertyHelper.cs
  • src/NLog/LogLevel.cs
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/NLog/Config/LoggingConfiguration.cs
  • src/NLog/Config/PropertyTypeConverter.cs
  • src/NLog/Internal/PropertyHelper.cs

Walkthrough

Refactors string-to-type conversion to explicit per-type logic (PropertyTypeConverter + PropertyHelper), normalizes LoggingConfiguration variable expansion for null/whitespace inputs, and switches LogLevel backing collections to arrays.

Changes

Type Conversion Refactoring + Variable Expansion

Layer / File(s) Summary
PropertyTypeConverter string conversion strategy
src/NLog/Config/PropertyTypeConverter.cs
Removes the _stringConverters dictionary and BuildStringConverterLookup. Adds HasConvertFromStringSupport(Type) and rewrites TryConvertFromString with explicit conditional conversions for Encoding, CultureInfo, Type, LineEndingMode, LogLevel, Uri, DateTime, DateTimeOffset, TimeSpan, Guid, and enums; enum parse failure now throws ArgumentException.
PropertyHelper conversion path adaptation
src/NLog/Internal/PropertyHelper.cs
Removes _propertyConversionMapper and its builder. Rewrites TryNLogSpecificConversion to explicit type checks (Layout/SimpleLayout, primitives, ConditionExpression, Layout<>), narrows TryImplicitConversion to non-IEnumerable reference types, adjusts SetPropertyFromString and TryFlatListConversion fallback ordering, and qualifies TypeDescriptor usage.
LoggingConfiguration variable expansion
src/NLog/Config/LoggingConfiguration.cs
Refactors ExpandSimpleVariables to treat null input as string.Empty, early-return when output is null/whitespace or contains no $, and set matchingVariableName only when a ${key} token exactly matches the trimmed original input for non-SimpleLayout variables.
LogLevel backing collection change
src/NLog/LogLevel.cs
Changes backing fields for AllLevels and AllLoggingLevels from read-only IList<LogLevel> to LogLevel[] arrays while preserving public IEnumerable<LogLevel> exposure.

🎯 4 (Complex) | ⏱️ ~45 minutes

"A rabbit refactors lines with care,
Caches fall, explicit checks now dare,
Variables expand from null to light,
Log levels hum in arrays tonight,
Small hops yield conversions done right." 🐰

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly addresses the main change of removing the cached dictionary lookup in PropertyTypeConverter to defer allocation until needed.
Description check ✅ Passed The description relates to the PR's core objective of deferring type-dependency resolving, which aligns with the changeset's refactoring of PropertyTypeConverter.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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)
src/NLog/Config/PropertyTypeConverter.cs (1)

96-117: ⚡ Quick win

Collapse the string-conversion allowlist into one source of truth.

HasConvertFromStringSupport(...) and TryConvertFromString(...) now have to stay manually aligned. If a future change adds support in only one place, IsComplexType(...) will disagree with the actual conversion path and you'll get subtle config parsing regressions.

Also applies to: 121-187

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/NLog/Config/PropertyTypeConverter.cs` around lines 96 - 117,
HasConvertFromStringSupport and TryConvertFromString (and IsComplexType) have
duplicated allowlists that must stay manually aligned; consolidate them into a
single source of truth (e.g. a private static readonly HashSet<Type> or a single
helper method) that lists the supported convertible types and is consulted by
HasConvertFromStringSupport, TryConvertFromString, and IsComplexType; ensure
enum support (type.IsEnum) remains handled consistently and update all three
callers (HasConvertFromStringSupport, TryConvertFromString, IsComplexType) to
reference that single collection/helper so future changes are made in one place.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/NLog/Internal/PropertyHelper.cs`:
- Around line 283-287: The Layout<T> branch in PropertyHelper (the block
checking propertyType.IsGenericType && propertyType.GetGenericTypeDefinition()
== typeof(Layout<>)) incorrectly maps an explicit empty value (value == "") to
SimpleLayout.Default; change it to use the same empty-layout sentinel used by
the untyped Layout/SimpleLayout path when value is empty, so explicit ""
produces the empty-layout sentinel rather than SimpleLayout.Default before
calling Activator.CreateInstance to set newValue.

---

Nitpick comments:
In `@src/NLog/Config/PropertyTypeConverter.cs`:
- Around line 96-117: HasConvertFromStringSupport and TryConvertFromString (and
IsComplexType) have duplicated allowlists that must stay manually aligned;
consolidate them into a single source of truth (e.g. a private static readonly
HashSet<Type> or a single helper method) that lists the supported convertible
types and is consulted by HasConvertFromStringSupport, TryConvertFromString, and
IsComplexType; ensure enum support (type.IsEnum) remains handled consistently
and update all three callers (HasConvertFromStringSupport, TryConvertFromString,
IsComplexType) to reference that single collection/helper so future changes are
made in one place.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 378d251b-a862-41d2-916f-d34452769196

📥 Commits

Reviewing files that changed from the base of the PR and between 7f18a90 and 174d561.

📒 Files selected for processing (3)
  • src/NLog/Config/LoggingConfiguration.cs
  • src/NLog/Config/PropertyTypeConverter.cs
  • src/NLog/Internal/PropertyHelper.cs

Comment thread src/NLog/Internal/PropertyHelper.cs
@sonarqubecloud

Copy link
Copy Markdown

@snakefoot
snakefoot merged commit 68aa2cc into NLog:dev May 16, 2026
5 of 6 checks passed
@snakefoot snakefoot added this to the 6.1.4 milestone Jul 8, 2026
@snakefoot snakefoot changed the title PropertyTypeConverter - Avoid allocating dictionary for conversion mapping until needed Reduce AOT file-size by changing PropertyTypeConverter to not use Dictionary for conversion mapping Jul 8, 2026
@snakefoot snakefoot changed the title Reduce AOT file-size by changing PropertyTypeConverter to not use Dictionary for conversion mapping Reduce AOT file-size by changing PropertyTypeConverter to avoid Dictionary for conversion mapping Jul 8, 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