azure-functions-dotnet-worker: Net7 Isolated Functions Logging - Console not working, AppInsights almost working

We have just migrated from net6 in-process to net7 isolated and the logging is a proving extremely challenging. We have two use cases:

  1. Locally we want to log debug ILogger (DI injected to constructor) to the console
  2. Production we want to log to App Insights.

Our setup is as follows:

host.json:

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "default": "Warning",
      "Host.Aggregator": "None",
      "Host.Results": "None",
      "Microsoft": "None",
      "Microsoft.Hosting": "None",
      "Microsoft.Hosting.Lifetime": "None",
      "Microsoft.AspNetcore": "None",
      "Microsoft.AspNetCore.Mvc.Internal": "None",
      "Microsoft.AspNetCore.Authentication": "None",
      "Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware": "None",
      "Microsoft.Extensions.Http.DefaultHttpClientFactory": "None",
      "Microsoft.Extensions.Hosting": "None",
      "System.Net.Http.HttpClient": "None",
      "Worker": "None"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  }
}

Code:

public IHost Build(Action<HostBuilderContext, IServiceCollection> configureServices) => new HostBuilder()
            .ConfigureAppConfiguration(ConfigurationBuilder.Configure)
            .ConfigureLogging(ConfigurationBuilder.ConfigureLogging)
            .ConfigureFunctionsWorkerDefaults(ConfigurationBuilder.ConfigureFunctions)
            .ConfigureServices(configureServices)
            .Build();


public static class ConfigurationBuilder
{
    public static void Configure(HostBuilderContext hostContext, IConfigurationBuilder configurationBuilder)
    {
        if (hostContext.HostingEnvironment.IsDevelopment())
        {
            var entryAssembly = Assembly.GetEntryAssembly();
            if (entryAssembly != null)
            {
                configurationBuilder.AddUserSecrets(entryAssembly, optional: true, reloadOnChange: false);
            }
        }
        if (hostContext.HostingEnvironment.IsProduction())
        {
              // Key Vault in prod
        }
    }

    public static void ConfigureFunctions(HostBuilderContext hostContext, IFunctionsWorkerApplicationBuilder builder)
    {
        if (hostContext.HostingEnvironment.IsProduction())
        {
            // At the moment this stops all logging except at warning or above
            // https://github.com/devops-circle/Azure-Functions-Logging-Tests/blob/master/Func.Isolated.Net7.With.AI/Program.cs
            builder
                .AddApplicationInsights()
                .AddApplicationInsightsLogger();
        }
    }

    public static void ConfigureLogging(HostBuilderContext hostContext, ILoggingBuilder builder)
    {
        if (hostContext.HostingEnvironment.IsDevelopment())
        {
            builder.AddConsole();
        }
    }
}

I’ll start with AppInsights. We’ve managed to mostly get this working looking through various other issues that people have raised but we are seeing a lot of Dependency messages logged which we were not seeing before:

image

You can see when we first deployed ney7 Isolated and we started seeing thousands of dependency messages logged like so:

image

How do we stop these?

Locally we want to see debug messages but not the noise from the host/functions, just our DI injected ILogger messages, how do we see these as no matter what we try unless we change the host.json default to Debug we don’t see anything and if we do that we see everything, including all the noise.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 21 (10 by maintainers)

Most upvoted comments

You will want to set log levels in both host.json and appsettings.json. These are for the host and worker process respectively, so you want to configure log filters for them both.

For log levels when using the app insights package, they don’t override each other - they are separate packages.

Thanks @jviau

I will update the repo with the problem we are seeing.

If we swap to Host.CreateDefaultBuilder() then when deployed to Azure App Insights begins filling up with a lot of TRACE messages. We can stop these by setting host.json to:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true
      }
    },
    "logLevel": {
      "default": "Warning"
    }
  }
}

But doing this stops all logging below Warning locally when we are debugging unless we set:

"AzureFunctionsJobHost__logging__logLevel__Function": "Debug",

Our goal is that locally developers can turn logging on and off simply by changing their user secrets with no need to edit either host.json or appsettings.json.

I will try to get the repo updated this morning, failing that it will be early next week.

Many thanks again for all your help.

OK, I’ve managed to get this working again locally.

I have changed the local.settings.json to override the Function loglevel like so:

"AzureFunctionsJobHost__logging__logLevel__Function": "Debug",

I have also had to modify our local configuration loaded from user secrets to include a logging section like so:

"logging": {
   "logLevel": {
	"default": "Debug"
   }
},

I have also had to change our ConfigureLogging to look like this:

 public static void ConfigureLogging(HostBuilderContext hostContext, ILoggingBuilder builder)
    {
        if (hostContext.HostingEnvironment.IsDevelopment())
        {
            // This is needed as it isn't picking up the logging section from the user secrets
            builder.AddConfiguration(hostContext.Configuration.GetSection("logging"));
        }
}

Once all of this is done we are now seeing the console logging locally we were seeing before moving to .net7 Isolated.

@brettsam Still no luck, we upgraded to preview4 of the app insights and console logging still not working. When you said this is fixed was there any additional config changes needed?

I tried not registering app insights locally when running in development and just call builder.AddConsole() and still no debug messages will appear on the console. Only warning, error and critical. This was never a problem on in-process in net6, all worked without issue so something has changed.