azure-functions-dotnet-worker: Can't see logs from `ILogger.LogDebug` when running Functions locally

I use a dependency injected ILogger<T>, as such:

public class MyFunction {
    private readonly ILogger<MyFunction> _logger;

    public MyFunction(ILogger<MyFunction> logger) {
        _logger = logger;
    }

    [Function(nameof(MyFunction))]
    public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "api")] HttpRequestData req)
    {
        _logger.LogDebug("debug");
        _logger.LogInformation("information");
    }
}

Running my Functions locally (func start --csharp), I am unable to see any logs with log levels below Information. In the example above, I can see “information”, but not “debug”.

I have tried many different permutations of my host.json file, including:

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "default": "Debug"
    }
  },
  ...
}

but it has no effect. The only way I’ve been able to see these logs is by changing the log level to something higher than Debug, which is a workaround but I have to remember to reduce the log level before deploying my code, which is annoying.

I’ve also tried running func start --verbose but it didn’t change anything.

Environment

FUNCTIONS_WORKER_RUNTIME: dotnet-isolated func --version = 4.0.3971 .csproj file:

<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.10.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.0" />

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 15 (2 by maintainers)

Most upvoted comments

The largely undocument MinLevel of ILoggingBuilder seems to be the magic sauce needed to see LogDebug and LogTrace (which you have also asked to appear in the configuration. MinLevel seems to act as a filter which doesn’t permit logs less than that level to pass, even if your config has asked for said logs to be logged.

I couldn’t figure out where (but I suspect ConfigureFunctionsWorkerDefaults) MinLevel is being set to Information by default, which means all attempts to LogDebug and LogTrace are doomed to not do anything.

      var host = new HostBuilder()
      // mad about default logging settings? This is to blame - probably
      .ConfigureFunctionsWorkerDefaults(...)
      .ConfigureLogging((HostBuilderContext hostingContext, ILoggingBuilder logging) => {
         // something (I suspect the call to "ConfigureFunctionsWorkerDefaults" sets MinLevel to "Information" by default
         // which overrides any configuration you pass, and means only logs of MinLevel or higher are logged.
         options.MinLevel = LogLevel.Trace;
})

And assuming you are using Function v4 and also want to use AppInsights. Many of the problems with App Insights running with isolated functions can be solved be using App Insights in the worker directly. But… that means the logging and App Insights configuration from host.json doesn’t apply. You can, however, pass in the logging configuration from App Configuration (as you would for any other .NET app).

var host = new HostBuilder()
      // mad about default logging settings? This is to blame
      .ConfigureFunctionsWorkerDefaults(...)
.ConfigureServices((worker, services) => {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
}
.ConfigureLogging((HostBuilderContext hostingContext, ILoggingBuilder logging) => {
        // should only be used locally - and probably shouldn't be needed (but seems to help console output)
        // https://github.com/Azure/azure-functions-dotnet-worker/issues/1140
        logging.AddConsole();
        // Turn off AppInsights auto filtering of >= Log.Warning (the config file doesn't impact this... probably)
         logging.Services.Configure<LoggerFilterOptions>(options => {
          LoggerFilterRule defaultRule = options.Rules.FirstOrDefault(rule => rule.ProviderName
              == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
          if (defaultRule is not null) {
            options.Rules.Remove(defaultRule);
          }
         // something (I suspect the call to "ConfigureFunctionsWorkerDefaults" sets MinLevel to "Information" by default
         // which overrides any configuration you pass, and means only logs of MinLevel or higher are logged.
         options.MinLevel = LogLevel.Trace;

          // Make sure the configuration of the app configuration is picked up. 
          /// When using AppInsights directly from the worker, host.json settings aren't used
          ///   so we should/need to use "appsettings.json" or equivalent
          /// As per .NET6 standard: Logging:AppInsights:LogLevel controls loglevels in AppInsights
          ///                        Logging:Loglevel controls logging in General (but doesn't touch appInsights)
          //
          // Note: ILoggerBuiilder.MinLevel sets a minimum level, under which no logging happens. Overriding your conifg.
          logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
})

I suspect that for .Net functions the main tricky point here is that, opposed to most modern .Net application templates, the host builder code for functions doesn’t automatically load the “Logging” section from application configuration system (appsettings.json in particular). In contrast, the WebApplicationBuilder modern ASP.NET apps start with, loads logging configuration internally using logging.AddConfiguration(...).

So basically all you need is:

  1. Have logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); in ConfigureLogging, or logging.SetMinimumLevel(...) call if you don’t use .net configuration, as shown above by @bdarlt. The appsettings.json approach also allows configuring AI logging.
  2. Have “AzureFunctionsJobHost__logging__LogLevel__Function”: “Debug” in the local.settings.json, or corresponding setting in host.json.