0

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)

Aamon
  • 11
  • 4
  • The only thing I can do it right now, when I'm throwing an exception, is to log it using ILogger, but I cannot see the stack trace in Azure. – Aamon Jul 23 '22 at 16:29
  • I think the recommended approach is different: https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core#enable-application-insights-server-side-telemetry-no-visual-studio. Can you please try it and see whether it addresses the issue? – ZakiMa Jul 24 '22 at 02:04
  • You also set "ailoptions to empty config. I wonder whether this is expected and whether this is the one which drops exceptions. – ZakiMa Jul 24 '22 at 02:05
  • Hey @ZakiMa, I tried before I asked both ways. I tried to set the ailoptions properties and I got the same result (there are only 3 properties). Also tried to use `ApplicationInsightsTelemetry` and `ApplicationInsightsTelemetryWorkerService`, with the same result – Aamon Jul 24 '22 at 15:57

1 Answers1

1

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;
}
Aamon
  • 11
  • 4