Search code examples
c#asp.netazure-application-insights

Inject configuration for Application Insights in ASP.NET Core


I have an application that uses Azure App Configuration to provide its configuration values. I've set it up to read these as part of an ASP.NET Core application startup process and it works just fine. As part of the setup it also injects a configuration object singleton into the service provider like this:

serviceCollection.AddSingleton<MyAppConfig>();

Now I want to use a value from this config object to set up Application Insights, but there does not seem to be a way for me to get the service MyAppConfig from the service provider during setup. I found this link that seems to indicate I can use an IConfiguration instance to handle this, but I'd prefer to use my actual object to set the connection string and other values directly. It also does not seem to explain exactly what keys I need to set on the IConfiguration object to provide my options.

How can I use a custom service (a simple class instance) as the source of configurations for Application Insights in ASP.NET core?

Edit: For clarity I've added a simple code example of what I wish to achieve:

// MyAppConfig.cs
class MyAppConfig {
    protected IConfiguration config { get; set; }
    public MyAppConfig(IConfiguration config)
    {
        this.config = config;
    }

    public string AppInsightsConnectionString
    {
        get
        {
            return config["appInsights-connectionString"];
        }
    }
}

// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureAppConfiguration((_, confBuilder) => {
    builder.AddAzureAppConfiguration(/*init Azure App Configuration*/);
    confBuilder.AddSingleton<MyAppConfig>();
});
builder.Services.AddApplicationInsightsTelemetry(); // This should use MyAppConfig.AppInsightsConnectionString from singleton instance

Solution

  • After some more research I've managed to couple together a workaround. It does not use the MyAppConfig class directly, but it allows me to get the Application Insights connection string from Azure App Configuration.

    I would prefer a cleaner approach if one exists. Ideally one where I can set the Connection String directly on the TelemetryConfiguration object to be as precise as possible as I'm not a fan of the "magic" configuration value "ApplicationInsights:ConnectionString". But at least this works for now.

    // MyAppConfig.cs
    class MyAppConfig {
        protected IConfiguration config { get; set; }
        public MyAppConfig(IConfiguration config)
        {
            this.config = config;
        }
    
        public string AppInsightsConnectionString
        {
            get
            {
                return config["appInsights-connectionString"];
            }
        }
    }
    
    // Program.cs
    var builder = WebApplication.CreateBuilder(args);
    builder.Host.ConfigureAppConfiguration((_, confBuilder) => {
        builder.AddAzureAppConfiguration(/*init Azure App Configuration*/);
        var configuration = builder.Build();
        var staticConfig = new Dictionary<string, string>
        {
            // Settting this explicitly allows the default Application Insights client to read a proper connection string from where it expects to find it
            // Ref: https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core?tabs=netcore6#enable-application-insights-server-side-telemetry-no-visual-studio
            { "ApplicationInsights:ConnectionString", configuration["appInsights-connectionString"] }
        };
        builder.AddInMemoryCollection(staticConfig);
        confBuilder.AddSingleton<MyAppConfig>();
    });
    builder.Services.AddApplicationInsightsTelemetry(); // This should use MyAppConfig.AppInsightsConnectionString from singleton instance