Configure Host's log level for an Azure Function outside host.json

44 Views Asked by At

Azure function host's logging level can be configured in the host.json file, e.g.

{
  "logging": {
    "logLevel": {
      "default": "Information"
    }
  }
}

This specifies the log level at which the host logs to the console, e.g. messages such as

Executed 'Functions.HandleHttpRequest' (Succeeded, Id=1239d35b-b794-45bd-b1c9-fce59b401234, Duration=123ms)

I would like to create a library used by multiple functions, and the library contains a method to set up the host. In this method I would like to set up the host's log level, instead of doing it separately in each function's host.json.

I'm thinking of something like this:

Function's Program.cs

using MyLibrary; 

public static void Main()
{
   var host = MyLibrary.CreateFunctionHost(); //The library method
   host.RunAsync();
}

Method in MyLibrary

public static IHost CreateFunctionHost()
{
   var builder = new HostBuilder();
   builder.AddHostConfigurationFromFile("additional_host_configuration.json") //this should add configuration to the host, just like the host.json does
   return builder.Build();
}

Is something like this possible?

I've read this guide about host.json but I feel like it doesn't answer my question.

0

There are 0 best solutions below