15

As per the Azure documentation, Functions V2 uses the .NET Core logging filter hierarchy for configuration.

In the following example, an instance of ILogger is injected into the Run method of the function.

[FunctionName("MyFunction")]
public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger logger, ExecutionContext executionContext)
{
    logger.LogInformation("I don't want to see this in production!"));
}

When inspecting the ILogger object, each LoggerInformation element has MinLevel of null which seems to log all levels.

In production, I only want to log at the Error level. I would like to be able to configure this using an environment variable but I cannot find any documentation which explains how to achieve this. I have tried adding the following environment variable to no effect:

"logging__logLevel__Default: "Error" 
Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
Alasdair Stark
  • 1,227
  • 11
  • 32
  • Have you checked logging configuration in [host.json](https://learn.microsoft.com/en-us/azure/azure-functions/functions-host-json#logging) or you only want to work with environment variable? – Jerry Liu Nov 21 '18 at 03:56
  • I would like to see if it's possible to control LogLevel using environment variables but I wasn't familiar with the host.json file. When debugging the Function locally using the emulator, I can see that a host.json file is automatically generated but does not contain any logging config. When I edit the file manually I can successfully set the log level. Can I control the generation of this file when developing locally? – Alasdair Stark Nov 21 '18 at 04:06
  • Manual edit is the only way, generation is controlled by Azure function template engine for VS. – Jerry Liu Nov 21 '18 at 04:14
  • 1
    How can you change that value (host file or other) live? I don't want verbose to be enabled by default, but I might want to enable it for a few minutes without having to redeploy... Is that possible? – Stephane Feb 20 '19 at 17:54

2 Answers2

23

Looking at the code (1, 2, 3) for azure functions runtime, this appears to be possible. You have to set it as

export AzureFunctionsJobHost__logging__logLevel__default=Error

This works for other host.json settings too

export AzureFunctionsJobHost__extensions__http__routePrefix=just-another-prefix
PramodValavala
  • 6,026
  • 1
  • 11
  • 30
  • Here's the specific [line of code](https://github.com/Azure/azure-functions-host/blob/6fc1a164e086029acecd4aa208a0ec3f4d1dea1f/src/WebJobs.Script/Config/ScriptEnvironmentVariablesConfigurationSource.cs#L44) with the double underscore – gabe Jul 21 '20 at 00:07
  • 5
    This is now covered in the [Azure Function Docs](https://learn.microsoft.com/azure/azure-functions/functions-host-json#override-hostjson-values) – PramodValavala Nov 23 '20 at 06:12
10

Extending the answer given by Pramod

In the portal, navigate to the Azure Function, goto the Configuration blade and press 'New application setting' :

  • Name: AzureFunctionsJobHost__logging__logLevel__Default

  • Value: Error

NB. Don't forget you need to press OK, then also press Save on the Configuration blade.

Peter
  • 971
  • 11
  • 12