Search code examples
c#azure-functionsazure-application-insightsazure-log-analytics

logs not appearing in Application Insights for Azure Functions v4 with .NET 8


I have functions being called, and I do logging as such:

_logger.LogInformation(...);

These do show in the live Log Stream on the portal. However, when querying the logs in Application Insights (traces), they do not show. whereas all sorts of other info is available, such as

Executed 'Functions.<MyFunctionName>' (Succeeded, Id=a23d6397-3a9f-4663-8f26-1b2693a362d5, Duration=14523ms)

In my hosts.json file I have:

  "version": "2.0",
  "logging": {
    "logLevel": {
      "Default": "Information",
     ...

I also used the telemetryClient, and those items do appear (customEvents) in the logs. However, as I said, the items from ILogger do not appear.

tech In App Insights In Live Log Stream
ILogger<> NO YES
TelemetryClient YES NO

This is happening with .NET 8 -- I did not have this issue using earlier .NET core versions; items logged via ILogger<> were showing up as expected in the traces table of Application Insights.

Any thoughts?

Additional info... ILogger info

This says that the config must look something like this:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

Yet, the schema here contradicts that!

And even when I change my config to this, I cannot get anything lower than warnings to appear in Application Insights.


Solution

  • The answer is here.

    A filter, which is, unfortunately and bizarrely, automatically added, must be explicitly removed. Here's an example:

    var host = new HostBuilder()
        .ConfigureFunctionsWorkerDefaults()
        .ConfigureServices(s =>
        {
            s.AddApplicationInsightsTelemetryWorkerService();
            s.ConfigureFunctionsApplicationInsights();
            s.AddSingleton<IHttpResponderService, DefaultHttpResponderService>();
            s.Configure<LoggerFilterOptions>(options =>
            {
                // The Application Insights SDK adds a default logging filter that instructs ILogger to capture only Warning and more severe logs. Application Insights requires an explicit override.
                // Log levels can also be configured using appsettings.json. For more information, see https://learn.microsoft.com/en-us/azure/azure-monitor/app/worker-service#ilogger-logs
                LoggerFilterRule toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
                    == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
    
                if (toRemove is not null)
                {
                    options.Rules.Remove(toRemove);
                }
            });
        })
        .Build();
    

    Thanks to this comment and this answer.