3
votes

I have a .net 5 web application that uses Application Insights. I try to log into AI trace by using ILogger<>. However: When analyzing the "traces" - Content in AI on Azure the logs are not shown.

Part of StartUp:

services.AddLogging(loggingbuilder =>
  {
    loggingbuilder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
    loggingbuilder.AddApplicationInsights();
  });
services.AddApplicationInsightsTelemetry();          

The constructor of the class that should do the logging injects ILogger and AppInsights via dependency injection:

public ImportService(ILogger<ImportService> log, TelemetryClient telemetryClient)
  {
    _log = log;
    _telemetryClient = telemetryClient;
  }

Inside the method I have the following two logging attempts:

public async Task<Customer> UpdateCustomerByEmail(string email)
{
  _telemetryClient.TrackTrace("From Telemetry");
  _log.LogWarning("From Log");
  [...]
}

While the first one ("from Telemetry") ends up correctly in AI-traces, the second one ("From Log") never shows up there.

The instrumentationkey is stored in the appsettings (and obviously correct because the telemetryClient-Track is working)

1

1 Answers

0
votes

Might this documentation be relevant for you? Adding the following code to program.cs worked for me:

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddApplicationInsights("<instrumentationKeyHere>");
                logging.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Information);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

The nuget package Microsoft.Extensions.Logging.ApplicationInsights must also be installed.

In your case it might be sufficient to simply provide the Instrumentation Key as a paramter to the AddApplicationInsights function.

As stated in the documentation "This code is required only when you use a standalone logging provider. For regular Application Insights monitoring, the instrumentation key is loaded automatically from the configuration path ApplicationInsights: Instrumentationkey."

This might explain why regular monitoring works, but not for logging.