LogEventBuilder - Properties with IReadOnlyCollection for initialCapacity#6059
LogEventBuilder - Properties with IReadOnlyCollection for initialCapacity#6059
Conversation
WalkthroughAdds a new public overload Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 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: Repository UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
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 |
4586098 to
74ca3fc
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/NLog/LogEventBuilder.cs (1)
123-139: Use the returned dictionary instead of_logEvent.Propertiesto avoid unused-local and match the span overload
dictionaryis currently assigned but never used, which will raise an “assigned but never used” warning and is inconsistent with theReadOnlySpan<(string, object?)>overload below that writes through the returned dictionary. You can both remove the warning and be consistent by writing throughdictionary:- if (_logEvent != null) - { - var dictionary = _logEvent.CreatePropertiesInternal(initialCapacity: properties.Count); - foreach (var property in properties) - _logEvent.Properties[property.Key] = property.Value; - } + if (_logEvent != null) + { + var dictionary = _logEvent.CreatePropertiesInternal(initialCapacity: properties.Count); + foreach (var property in properties) + dictionary[property.Key] = property.Value; + }(Optionally, the existing
IEnumerable<KeyValuePair<string, object?>>overload could detectIReadOnlyCollection<>and delegate to this overload for the same pre-sizing benefits, but that’s not required for this change.)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/NLog/LogEventBuilder.cs(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/NLog/LogEventBuilder.cs (2)
src/NLog/ILoggerExtensions.cs (8)
LogEventBuilder(57-60)LogEventBuilder(67-70)LogEventBuilder(77-80)LogEventBuilder(87-90)LogEventBuilder(97-100)LogEventBuilder(107-110)LogEventBuilder(117-120)LogEventBuilder(129-132)src/NLog/Internal/Guard.cs (1)
Guard(56-78)
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/NLog/LogEventBuilder.cs (1)
123-139: Tighten null/Count check and actually use the preallocated dictionaryNice addition for pre-sizing based on
IReadOnlyCollection.Count. Two small tweaks would make this consistent with the other overload and avoid an “assigned but never used” local:
- After
Guard.ThrowIfNull(properties);, theproperties?.Countnull-conditional is redundant and inconsistent with theIEnumerableoverload.- You allocate
dictionaryviaCreatePropertiesInternalbut then write through_logEvent.Properties; prefer using the local as in the span-based overload.Suggested change:
- public LogEventBuilder Properties(IReadOnlyCollection<KeyValuePair<string, object?>> properties) - { - Guard.ThrowIfNull(properties); - if (_logEvent != null && properties?.Count > 0) - { - var dictionary = _logEvent.CreatePropertiesInternal(initialCapacity: properties.Count); - foreach (var property in properties) - _logEvent.Properties[property.Key] = property.Value; - } - return this; - } + public LogEventBuilder Properties(IReadOnlyCollection<KeyValuePair<string, object?>> properties) + { + Guard.ThrowIfNull(properties); + if (_logEvent != null && properties.Count > 0) + { + var dictionary = _logEvent.CreatePropertiesInternal(initialCapacity: properties.Count); + foreach (var property in properties) + dictionary[property.Key] = property.Value; + } + return this; + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/NLog/LogEventBuilder.cs(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/NLog/LogEventBuilder.cs (2)
src/NLog/ILoggerExtensions.cs (8)
LogEventBuilder(57-60)LogEventBuilder(67-70)LogEventBuilder(77-80)LogEventBuilder(87-90)LogEventBuilder(97-100)LogEventBuilder(107-110)LogEventBuilder(117-120)LogEventBuilder(129-132)src/NLog/Internal/Guard.cs (1)
Guard(56-78)
⏰ 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). (3)
- GitHub Check: task-list-completed
- GitHub Check: task-list-completed
- GitHub Check: task-list-completed
🔇 Additional comments (1)
src/NLog/LogEventBuilder.cs (1)
148-153: Early-exit on empty span looks good and avoids unnecessary allocationsThe added
!properties.IsEmptyguard is a clean micro-optimization and keeps behavior in line with the enumerable overload (no properties dictionary created when there are no properties). The rest of the method remains consistent with the new IReadOnlyCollection overload.
74ca3fc to
d3a9f5b
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/NLog/LogEventBuilder.cs (1)
123-139: Good optimization with pre-allocation!The new overload provides a nice performance improvement by pre-sizing the internal dictionary. The implementation is correct and consistent with the
ReadOnlySpanoverload.One minor simplification: Line 131 uses
properties?.Countwith a null-conditional operator, but sinceGuard.ThrowIfNull(properties)on line 130 guarantees thatpropertiesis not null, you can simplify it toproperties.Count.Apply this diff to remove the redundant null check:
- if (_logEvent != null && properties?.Count > 0) + if (_logEvent != null && properties.Count > 0)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/NLog/LogEventBuilder.cs(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/NLog/LogEventBuilder.cs (1)
src/NLog/Internal/Guard.cs (1)
Guard(56-78)
⏰ 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)
src/NLog/LogEventBuilder.cs (1)
148-148: LGTM! Good optimization.Adding the
!properties.IsEmptyguard prevents unnecessary dictionary allocation when there are no properties to add. This is consistent with the newIReadOnlyCollectionoverload's count check.
d3a9f5b to
6a4f6a5
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/NLog/LogEventBuilder.cs (1)
123-139: IReadOnlyCollection overload looks correct and efficiently uses Count for pre-allocationThe new
LogEventBuilder.Properties(IReadOnlyCollection<KeyValuePair<string, object?>> properties)overload is consistent with the existing API, usesGuard.ThrowIfNull, and safely short-circuits on_logEvent != null && properties.Count > 0soCountis only accessed when needed. Pre-sizing viaCreatePropertiesInternal(initialCapacity: properties.Count)plus a single enumeration is a nice optimization, and overload resolution will naturally route IReadOnlyCollection-typed callers here without affecting existing IEnumerable-based call sites.As a minor, optional enhancement, the
IEnumerableoverload could forward to this one whenproperties is IReadOnlyCollection<KeyValuePair<string, object?>> roto reuse the pre-allocation path, but that’s not required for this PR.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/NLog/LogEventBuilder.cs(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/NLog/LogEventBuilder.cs (2)
src/NLog/ILoggerExtensions.cs (8)
LogEventBuilder(57-60)LogEventBuilder(67-70)LogEventBuilder(77-80)LogEventBuilder(87-90)LogEventBuilder(97-100)LogEventBuilder(107-110)LogEventBuilder(117-120)LogEventBuilder(129-132)src/NLog/Internal/Guard.cs (1)
Guard(56-78)
⏰ 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)
src/NLog/LogEventBuilder.cs (1)
148-153: Empty-span guard avoids unnecessary dictionary allocationThe added
!properties.IsEmptycheck in the ReadOnlySpan overload correctly prevents creating a properties dictionary when there are no items, aligning behavior with the new IReadOnlyCollection overload. This is a pure allocation optimization; the observable semantics for callers (no properties added when the span is empty) remain the same.
9a46951 to
0dd9315
Compare
0dd9315 to
1bbaa24
Compare
|



No description provided.