1

I've found that in the application I've been set to work on, there's a TelemetryInitialier which is called at every web api call and adds some properties from the appsettings.json.

Here's the defined class

   public class AppInsightTelemetryInitializer : ITelemetryInitializer
    {
        private readonly AppInsightsMetrics _appInsightsMetrics;
        public AppInsightTelemetryInitializer(IOptions<AppInsightsMetrics> appInsightsMetrics)
        {
            _appInsightsMetrics = appInsightsMetrics.Value;
        }

        public void Initialize(ITelemetry telemetry)
        {
            var propTelemetry = (ISupportProperties)telemetry;

            propTelemetry.Properties["Application"] = _appInsightsMetrics.Application;
            propTelemetry.Properties["Version"] = _appInsightsMetrics.Version;
            propTelemetry.Properties["LatestCommit"] = _appInsightsMetrics.LatestCommit;
        }
    }

This class is registered this way

   appBuilder.Services.Configure<AppInsightsMetrics>(appBuilder.Configuration.GetSection(AppInsightsMetrics.InsightsMetrics));
            appBuilder.Services.AddSingleton<ITelemetryInitializer, AppInsightTelemetryInitializer>();

            if (env.IsDevelopment())
            {
                services.AddApplicationInsightsTelemetry(x =>
                {
                    x.EnableDebugLogger = false;
                    x.InstrumentationKey = "instrumentation key";
                });
            }
            else
            {
                services.AddApplicationInsightsTelemetry();
            }

And the data are loaded from the appsettings.json file as

  "AppInsightsMetrics": {
    "Application": "xxx.Api",
    "Version": "",
    "LatestCommit": ""
  },

Those data are replaced in production by CI/CD azure pipeline.

I was wondering, is there a way of defining them at configuration time and remove this middleware from each call?

Thanks in advance

advapi
  • 3,661
  • 4
  • 38
  • 73

0 Answers0