Skip to content

Conversation

@VanshikaNavya
Copy link

@VanshikaNavya VanshikaNavya commented Feb 6, 2026

The TryGetOriginalFormatFromAttributes method only checked the last position for the {OriginalFormat} attribute. This fails when using Microsoft.Extensions.Telemetry.Abstractions logging source generator, which places the attribute at the first position.

Fixes #
This resolves the issue where LogRecord.Body contained the formatted message instead of the template when using the MS Telemetry Abstractions generator.

Design discussion issue #
#5983

Changes

  • Modified the TryGetOriginalFormatFromAttributes to first check the last position (most common - built-in MS logger)
  • If not found, traverses from the beginning through remaining positions
  • Ensures O(1) performance for 99% of cases while maintaining correctness for all scenarios
  • Handles edge cases where {OriginalFormat} appears at any position in the attribute list

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

@VanshikaNavya VanshikaNavya requested a review from a team as a code owner February 6, 2026 20:50
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Feb 6, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: VanshikaNavya / name: Vanshika Navya (18521a9)

@github-actions github-actions bot added the pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package label Feb 6, 2026
@VanshikaNavya VanshikaNavya marked this pull request as draft February 6, 2026 21:04
@VanshikaNavya VanshikaNavya force-pushed the fix/originalformat-attribute-search branch 2 times, most recently from 9d8d296 to 529f930 Compare February 9, 2026 11:45
Copy link
Member

@martincostello martincostello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test that would fail without the fix so we know it is indeed fixed?

@@ -215,14 +215,25 @@ private static bool TryGetOriginalFormatFromAttributes(
[NotNullWhen(true)] out string? originalFormat)
{
if (attributes != null && attributes.Count > 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be simplified by just using having the for loop, but going backwards?

for (int i = attributes.Count - 1; i > -1; i--)
{
    var attribute = attributes[i];
    if (attribute.Key == "{OriginalFormat}" && attribute.Value is string value)
    {
        originalFormat = value;
        return true;
    }
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the backwards loop is cleaner and still checks the most common position (last index) first. Updated!

…mplementation only checked the last position, which failed when MS Telemetry Abstractions generator places it at the first position. Now searches all positions. Fixes open-telemetry#5983
@VanshikaNavya VanshikaNavya force-pushed the fix/originalformat-attribute-search branch from 529f930 to 18521a9 Compare February 9, 2026 11:56
var lastAttribute = attributes[attributes.Count - 1];
if (lastAttribute.Key == "{OriginalFormat}"
&& lastAttribute.Value is string tempOriginalFormat)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{
{

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants