Search code examples
azure-application-insights

Automatically add a tag for all Application Insights telemetry from .Net Applications


We have several .Net 5 projects / applications that are logically grouped together and I'd like to globally apply a tag to each telemetry sent from each application depending on its logical group identifier into a specific tag.

Is there any way to do this on a per-project level and if so, how?


Solution

  • You can add a TelemetryInitializer similar to this:

    internal class MyTelemetryInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {           
            telemetry.Context.GlobalProperties.Add("mytagname", "mytagvalue");
        }
    }
    

    Register it:

    using Microsoft.ApplicationInsights.Extensibility;
    using CustomInitializer.Telemetry;
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
    }
    

    This will ensure that every telemetry item emitted by this app will have above tag.