I’m trying to control the log level for my C# Azure Function. The goal is to change the log level without having to redeploy the function. The idea is to change the Environment variables in order to achieve this.
I added an environment variable to control the log level:
It doesn’t seem to work, as I’m still seeing Debug and Information logging:
My host.json looks like this:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
},
"logLevel": {
"default": "Debug"
}
}
}
And Program.cs like this:
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureLogging(builder => builder.SetMinimumLevel(LogLevel.Debug))
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.Configure<LoggerFilterOptions>(options => options.Rules.Clear());
})
.Build();
host.Run();
When i run my function app locally it works as intended when i change my local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"AzureFunctionsJobHost__logging__LogLevel__default": "Warning"
}
}
My function:
public class Function1
{
private readonly ILogger<Function1> _logger;
public Function1(ILogger<Function1> logger)
{
_logger = logger;
}
[Function("LogDebug")]
public IActionResult RunDebug([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
{
_logger.LogDebug("***********DEBUG***********");
return new OkObjectResult("Welcome to Azure Functions!");
}
[Function("LogInformation")]
public IActionResult RunInformation([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
{
_logger.LogInformation("***********INFORMATION***********");
return new OkObjectResult("Welcome to Azure Functions!");
}
[Function("LogWarning")]
public IActionResult RunWarning([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
{
_logger.LogWarning("***********WARNING***********");
return new OkObjectResult("Welcome to Azure Functions!");
}
}
Can someone shed some light on what I’m missing? Thanks in advance.
2
Answers
With some help from a colleague I found a way to control the log level with the environment variables.
I changed Program.cs to the following:
This change allows logging to be configured by the environment variables.
My host.json looks like this:
I can set my environment variables according to the desired log level: Environment variables
And the result looks like this: Azure portal logs
you have set minimum level to Debug. This code with minimum level
Debug
is overriding for application insights. Its working fine locally but for portal its overriding.host.json
:Function.cs
:I deployed code with same and I was also getting all level logs in Application insight. but in
LogStream
I was getting aboveWarning
level.Program.cs
:you need to set minimum level to
Warning
. I am able to see in both places in application Insights and Log Stream bothProgram.cs
: