CodexBloom - Programming Q&A Platform

implementing telemetry aggregation in .NET Core 7 with Application Insights - unexpected data duplication

πŸ‘€ Views: 96 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-01
application-insights dotnet-core telemetry C#

I tried several approaches but none seem to work. I'm experimenting with I'm working on a project and hit a roadblock....... I'm working with an scenario where my telemetry data is being duplicated when using Application Insights in a .NET Core 7 application. I've set up the SDK and configured it in `Startup.cs` as follows: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddApplicationInsightsTelemetry(config => { config.InstrumentationKey = "your_instrumentation_key"; }); } ``` Despite this, I've noticed that some custom events I'm tracking are recorded multiple times. For example, when I log an event like this: ```csharp var telemetryClient = new TelemetryClient(); telemetryClient.TrackEvent("UserLoggedIn", new Dictionary<string, string> { { "UserId", userId } }); ``` If the user logs in multiple times during a session, the same event for the same user ID appears multiple times in the Application Insights dashboard, leading to inflated metrics. I've tried implementing a simple check to prevent duplicate logging by keeping track of the logged user IDs in a HashSet, but it doesn't seem to help. Here’s what my implementation looks like: ```csharp private HashSet<string> loggedInUsers = new HashSet<string>(); private void LogUserLogin(string userId) { if (loggedInUsers.Add(userId)) { telemetryClient.TrackEvent("UserLoggedIn", new Dictionary<string, string> { { "UserId", userId } }); } } ``` This should prevent duplicates for the same session, but the telemetry still shows multiple entries after some testing. I suspect the scenario might be related to how the telemetry client is instantiated or how the events are being batched and sent, but I’m not sure. I've also checked for any middleware that might be causing the event to trigger multiple times, and everything seems fine there. Has anyone experienced similar issues with Application Insights in .NET Core and found a solution? What best practices should I follow for managing telemetry events to ensure they don’t duplicate? Any insights would be greatly appreciated! This is part of a larger API I'm building. Is there a better approach? I recently upgraded to C# stable. What's the best practice here? I'm on Windows 11 using the latest version of C#. Hoping someone can shed some light on this. How would you solve this?