JensB
JensB

Reputation: 6850

Adding custom property to all Application Insight traces

I want to add a custom property to all traces in Application Insights.

In Asp.Net Core I've added this code

internal class TelemetryProperties : ITelemetryInitializer
{
    private IConfiguration configuration;

    public TelemetryProperties(IConfiguration configuration)
    {
        this.configuration = configuration;
    }

    // TODO: Not being added to all traces.
    // http://apmtips.com/blog/2014/12/01/telemetry-initializers/
    public void Initialize(ITelemetry telemetry)
    {
        var applicationName = configuration["Application:Name"];
        telemetry.Context.Properties.Add("Application", applicationName);
    }
}

and in the configure method of Startup.cs I have added:

TelemetryConfiguration.Active.TelemetryInitializers.Add(new TelemetryProperties(Configuration));

The intent was to add "Application" to all traces, even ones made automatically by Application Insights, but the effect is that it is ONLY being added to my custom traces that Im calling through my code.

How do I add a property to ALL traces, even ones I do not create.

Edit: The purpose of this is that I want to have multiple APIs in the same application insights log, but I want to be able to partition when neccessary by adding a defining property such as application name.

Upvotes: 2

Views: 2946

Answers (2)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29940

I used your code(without change), and at my side, all the traces(the application insights adds, I didn't use any Trackxx methods) including the exception all have the property "Application". Please see the screenshots below: enter image description here

If it still occurs in your side, please provide the screenshots of the appInsights' logs.

Upvotes: 1

cijothomas
cijothomas

Reputation: 3126

Modifying TelemetryConfiguration.Active is not the recommended approach in Asp.Net Core apps. Can you add the telemetry initializer using the below code in ConfigureServices ?

services.AddSingleton<ITelemetryInitializer, TelemetryProperties>();

https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Custom-Configuration#adding-new-telemetryinitializer

Please let me know if this helps.

Upvotes: 2

Related Questions