Search code examples
c#azureconsole-applicationazure-application-insightsappinsights

Exceptions are no longer shown in StackTrace in Application Insights Azure after switching from Instrumentation Key to Connection String


I found today that using Instrumentation Key in Application Insights will be deprecated in future. Microsoft recommends switching to Connection String. The issue is that if I'm trying to switch to ConnectionString, the exception are no longer shown in Application Insights in Azure.

That was my code before switching that is working

private static IServiceCollection ConfigureServices()
{
    //here I just read app settings.json file
    IConfiguration config = LoadConfiguration();
    IServiceCollection services = new ServiceCollection();

    services.AddLogging(builder =>
        {
            builder.AddConfiguration(config);
            builder.AddConsole();
            builder.AddApplicationInsights(config.GetSection("InstrumentationKey").Value)
                .AddFilter("", LogLevel.Trace);
        })
        .AddSingleton<IExampleService, ExampleService>();
    
    return services;
}

And this is my code now, after switching to ConnectionString

private static IServiceCollection ConfigureServices()
{
    IConfiguration config = LoadConfiguration();
    IServiceCollection services = new ServiceCollection();

    services.AddLogging(builder =>
        {
            builder.AddConfiguration(config);
            builder.AddConsole();
            builder.AddApplicationInsights(
                telConfig => telConfig.ConnectionString = config.GetSection("AIConnectionString").Value),
                ailoptions => {}).AddFilter("", LogLevel.Trace);
        })
        .AddSingleton<IExampleService, ExampleService>();

    return services;
}

If I log a message with the ILogger, the trace appears in AppInsights, but if I throw an exception, then nothing is shown in Azure. Also I have LogLevel configured in appsettings.json. Also, tried multiple others methods and still doesn't work (used ApplicationInsightsTelemetryWorkerService, waiting a few seconds before flushing the TelemetryClient, etc)


Solution

  • Now everything is okay. I don't know what caused this issue. If you want to implement Logger with Application Insights using ConnectionString here is a code example. The Microsoft documentation is not explicit on this:

    private static IServiceCollection ConfigureServices()
    {
        IConfiguration config = LoadConfiguration();
        IServiceCollection services = new ServiceCollection();
    
        services.AddLogging(builder =>
            {
                builder.AddConfiguration(config);
                builder.AddConsole();
                builder.AddApplicationInsights(
                    telConfig => telConfig.ConnectionString = config.GetSection("AIConnectionString").Value),
                    ailoptions => {}).AddFilter("", LogLevel.Trace);
            })
            .AddSingleton<IExampleService, ExampleService>();
    
        return services;
    }