Override an application settings within host.json of Azure Functions on your local computer

TOC

Summary

How to override application settings of Azure Functions on my local computer?

The Azure Functions runtime v2 and later can override application settings that was wrote in host.json with local.settings.json. It will be using AzureFunctionsJobHost__* .

In version 2.x and later versions of the Functions runtime, application settings can override host.json settings in the current environment. These overrides are expressed as application settings named AzureFunctionsJobHost__path__to__setting.

https://docs.microsoft.com/en-us/azure/azure-functions/functions-app-settings#azurefunctionsjobhost__

Let’s try it.

Prerequisites

  • .NET Core v3.1
  • Azure Functions v3
  • Visual Studio for Mac v8.10
    • Azure Functions Core Tools v3.0.3873
  • C# v8

Steps

Use-Case 1: Disable sending a log to Application Insights

An example is here:

In host.json is the following:

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingExcludedTypes": "Request",
            "samplingSettings": {
                "isEnabled": true
            }
        }
    }
}

And, local.settings.json is below:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "AzureFunctionsJobHost__logging__applicationInsights__samplingSettings__isEnabled":"false"
    }
}

With the above settings, the Application Insights sampling is disabled when running locally.

Use-Case 2: Change a log-level

In Azure Functions, it can configure log level.
Detail is here:

For example, the following function can output only Warning log that is set by host.json.

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

Sample Function Function1 is here:

namespace YourNameSpace
{
    public static class HogeApi
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            return new OkObjectResult("Hello");
        }
    }

In this situation, the “information” log is not output, but it can be output by changing the setting in local.settings.json. A local.settings.json as example is here:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "AzureFunctionsJobHost__logging__logLevel__Default": "Information"
    }
}

Use-Case 3: Printing log to your console

Azure Functions Core Tools v3.0.3873, it require verbose flag in console when locally debugging:

Azure Functions Core Tools
Core Tools Version:       3.0.3873 Commit hash: c546cd456f0e58058cf6c37365eb5ccc5bb0840b  (64-bit)
Function Runtime Version: 3.3.1.0

[2021-10-23T05:44:31.730Z] Found /Users/hiroakit/Sample/sample.csproj. Using for user secrets file configuration.
[2021-10-23T05:44:32.741Z] Cannot create directory for shared memory usage: /dev/shm/AzureFunctions
[2021-10-23T05:44:32.741Z] System.IO.FileSystem: Access to the path '/dev/shm/AzureFunctions' is denied. Operation not permitted.

Functions:

        Function1: [GET] http://localhost:7071/api/Function1

For detailed output, run func with --verbose flag.

So, adding consoleLevel to local.settings.json. You will get verbose log on your console.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AzureFunctionsJobHost__logging__tracing__consoleLevel": "verbose"
  }
}

More information of consoleLevel is here:

References

Let's share this post!
TOC
閉じる