Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 17, 2025

When event properties and context properties share the same name, NLog's GetAllProperties allows context properties to overwrite event properties instead of renaming the duplicate. This causes the test EventPropertyKeyNameIsAppendedWith_1_IfSameAsGlobalDiagnosticContextKeyName to fail.

// Test setup: event property "Name" = "Value", context property "Name" renders to "Global Value"
eventInfo.Properties["Name"] = "Value";
target.ContextProperties.Add(new TargetPropertyWithContext("Name", "${gdc:item=Name}"));

// Expected: Name=Value, Name_1=Global Value
// Actual (before fix): Name=Global Value, Name_1=Global Value

Changes

  • BuildPropertyBag: Manually collect properties instead of using GetAllProperties

    • Add event properties first (take precedence)
    • Add context properties second with duplicate detection
    • Helper method TryAddPropertyToPropertyBag appends _1, _2 suffixes for collisions
  • Test: Remove [Ignore] attribute from previously failing test

This ensures event properties always retain their original names and values when collisions occur with context properties.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/NLog/NLog/issues/4636
    • Triggering command: curl -s REDACTED (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Test EventPropertyKeyNameIsAppendedWith_1_IfSameAsGlobalDiagnosticContextKeyName fails</issue_title>
<issue_description>Currently, the test EventPropertyKeyNameIsAppendedWith_1_IfSameAsGlobalDiagnosticContextKeyName fails, because the properties "Name" and "Name_1" both contain the value "Global Value" instead of one of them being "Value". This might be due to how NLog processes properties internally. Check how other structured NLog targets work with the same configuration?

[TestMethod]
[Ignore("NLog behaviour seems to have changed, this test is no longer valid")]
[TestCategory("NLogTarget")]
#pragma warning disable CA1707 // Identifiers should not contain underscores
public void EventPropertyKeyNameIsAppendedWith_1_IfSameAsGlobalDiagnosticContextKeyName()
#pragma warning restore CA1707 // Identifiers should not contain underscores
{
using ApplicationInsightsTarget target = new()
{
IncludeGdc = true,
IncludeEventProperties = true,
IncludeScopeNested = true,
IncludeScopeProperties = true
};
target.ContextProperties.Add(new TargetPropertyWithContext("Name", "${gdc:item=Name}"));
var aiLogger = this.CreateTargetWithGivenConnectionString(target: target);
GlobalDiagnosticsContext.Set("Name", "Global Value");
var eventInfo = new LogEventInfo(LogLevel.Trace, "TestLogger", "Hello!");
eventInfo.Properties["Name"] = "Value";
aiLogger.Log(eventInfo);
var telemetry = this.adapterHelper.Channel.SentItems.FirstOrDefault() as TraceTelemetry;
Assert.IsTrue(telemetry.Properties.ContainsKey("Name"), "Key name altered");
Assert.AreEqual("Value", telemetry.Properties["Name"]);
Assert.IsTrue(telemetry.Properties.ContainsKey("Name_1"));
Assert.AreEqual("Global Value", telemetry.Properties["Name_1"]);
}
</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix EventPropertyKeyName test failure due to NLog processing Fix property collision handling: ensure event properties take precedence over context properties Nov 17, 2025
Copilot AI requested a review from hangy November 17, 2025 15:50
@hangy hangy changed the title Fix property collision handling: ensure event properties take precedence over context properties fix: Property collision handling: ensure event properties take precedence over context properties Nov 17, 2025
@codecov
Copy link

codecov bot commented Nov 17, 2025

Codecov Report

❌ Patch coverage is 77.14286% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.80%. Comparing base (6d183c7) to head (c5bf928).
⚠️ Report is 9 commits behind head on dev.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/NLogTarget/ApplicationInsightsTarget.cs 77.14% 3 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev      #75      +/-   ##
==========================================
- Coverage   62.76%   60.80%   -1.96%     
==========================================
  Files           3        3              
  Lines         239      273      +34     
  Branches       39       49      +10     
==========================================
+ Hits          150      166      +16     
- Misses         73       86      +13     
- Partials       16       21       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hangy hangy marked this pull request as ready for review November 17, 2025 18:32
@hangy hangy merged commit 211f262 into dev Nov 17, 2025
9 of 15 checks passed
@hangy hangy deleted the copilot/fix-event-property-key-test branch November 17, 2025 18:32
github-actions bot pushed a commit that referenced this pull request Jan 1, 2026
# [1.2.0-beta.7](v1.2.0-beta.6...v1.2.0-beta.7) (2026-01-01)

### Bug Fixes

* Property collision handling: ensure event properties take precedence over context properties ([#75](#75)) ([211f262](211f262))

### Features

* Serialize complex properties as JSON in Application Insights custom dimensions ([#76](#76)) ([4894621](4894621))
@github-actions
Copy link

github-actions bot commented Jan 1, 2026

🎉 This PR is included in version 1.2.0-beta.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

Test EventPropertyKeyNameIsAppendedWith_1_IfSameAsGlobalDiagnosticContextKeyName fails

2 participants