Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure LogLevel of Azure Function using environment variables

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" 
like image 339
Alasdair Stark Avatar asked Nov 21 '18 03:11

Alasdair Stark


People also ask

How do I configure monitoring for Azure function?

In your function app, select Configuration under Settings, and then select Application settings. If you see a setting named APPINSIGHTS_INSTRUMENTATIONKEY , Application Insights integration is enabled for your function app running in Azure.

How do I set environment variables in Azure?

To set environment variables when you start a container in the Azure portal, specify them in the Advanced page when you create the container. Under Environment variables, enter NumWords with a value of 5 for the first variable, and enter MinLength with a value of 8 for the second variable.

How do I target Azure function runtime?

Azure Functions lets you target a specific version of the runtime on Windows by using the FUNCTIONS_EXTENSION_VERSION application setting in a function app. The function app is kept on the specified major version until you explicitly choose to move to a new version.

How to change the log level in Azure portal?

And if you want to change the log level in azure portal, please follow this: In Azure portal, navigate to your function app -> in the function app settings, make sure enable the Read/Write, then change log level to trace in the host.json.

How do I enable Scale Controller logs in Azure Functions?

You can have the Azure Functions scale controller emit logs to either Application Insights or to Blob storage to better understand the decisions the scale controller is making for your function app. To enable this feature, you add an application setting named SCALE_CONTROLLER_LOGGING_ENABLED to your function app settings.

How to enable read/write logging in Azure Functions?

In Azure portal, navigate to your function app -> in the function app settings, make sure enable the Read/Write, then change log level to trace in the host.json. To further add to @Ivan Yang's excellent answer, you can specify a minimum logging level per function in v2 of Azure Functions.

What are the different values of Azure_Functions_environment?

You can set AZURE_FUNCTIONS_ENVIRONMENT to any value, but three values are supported: Development, Staging, and Production. When AZURE_FUNCTIONS_ENVIRONMENT isn't set, it defaults to Development on a local environment and Production on Azure. This setting should be used instead of ASPNETCORE_ENVIRONMENT to set the runtime environment.


2 Answers

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.

like image 174
Peter Avatar answered Oct 11 '22 06:10

Peter


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
like image 22
PramodValavala-MSFT Avatar answered Oct 11 '22 08:10

PramodValavala-MSFT