Search code examples
c#azure-functionsazure-application-insights

Azure Function app (.NET 8) not logging info to app insights


I have a .NET 8 Azure Function app which I have been working on, this is the first .NET 8 app that I have created, though I have worked on function apps since .NET Core 3.1.

I'm sure this is something really basic that I am missing, but I can't see what.

This is my host.json

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "default": "debug"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true
        "excludedTypes": "Request"
      }
    }
  }
}

This is the function:

[Function("MYFUNCNAME")]
public void Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer)
{
    _logger.LogError("LOCAL THIS IS AN ERROR MESSAGE WHICH IS LOGGED");
    
    _logger.LogInformation($"THIS WONT BE LOGGED IN AI - THOUGH IT DOES SHOW IN THE CONSOLE");

    _logger.LogDebug("THIS IS NOT LOGGED EITHER");
}

In the console I can see ALL of the messages.

However in app insights I can only see the error messages.

I want all of the messages in AI as well as in the console.

I am at a loss, I have another function app which has the same configuration and it works perfectly. I cant see any difference between configurations in the function app or app insights between the working and non working projects (except the working one is on .NET 6 and the non working one is on .NET 8).

What could I be missing here?


Solution

  • Had the same issue with .net7 Functions in Isolated Mode, I'd guess your old function app did not run in isolated mode and the new one does?

    If that is the case then you need configure the LogFilterOptions and remove the ApplicationInsightsLoggingProvider

    This is done in your hostBuilder (found in program.cs)

    You just need to add the following after the services.ConfigureFunctionsApplicationInsights() call.

    services.Configure<LoggerFilterOptions>(options =>
    {
        var appInsightsLoggerProvider = options.Rules.FirstOrDefault(rule => rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
    
        if (appInsightsLoggerProvider != default) options.Rules.Remove(appInsightsLoggerProvider);
    });
    

    Its not very well documented, at least I didn't think so, but if you google "azure functions dotnet isolated process guide" there is a microsoft learn page which talks about the isolated model - its not explicitly talked about in there BUT one of the code samples (in the startup and configuration section) includes something very similar to the code above.

    After that it should work.