Reputation: 605
Is there a way to override the default log level of an Azure Function app without updating the host.json file? I want my function to pass trace logs to application insights only on dev environment. I'm thinking if an environment variable can just be set and the function will know when to pass the logs.
Upvotes: 7
Views: 7451
Reputation: 2131
from my perspective, normally you cannot change the logger level without restarting the Azure Function.
BUT, if you are writing a .NET core function app, you can try the dynamic configuration feature: Tutorial: Use dynamic configuration in an Azure Functions app. Updating host.json or Function configuration on portal and then restarting function app is static configuration. Using the dynamic configuration method does not require restart of function app.
Upvotes: 2
Reputation: 1712
Yes this is possible. To replace default log level set key AzureFunctionsJobHost__logging__LogLevel__Default
and value Trace
/Debug
/Information
or whatever you want. Like for me in my host.json Default
is Trace
but in azure it is Information
. So just add new environment variable(Application settings) and prefix with AzureFunctionsJobHost__logging__LogLevel__
for all the keys of loglevel and set the desired value and your host settings will be overridden.
You can read more Here
Upvotes: 15
Reputation: 635
A possible workaround would be to create an app setting of your own to define which log level you want to run at. Then, in your code, load the app setting in the function and use it to control whether or not you call the logger method. For example:
bool shouldDebug = // obtain the app setting, or environment variable here
bool shouldInformation = // same for here
bool shouldTrace = // same for here
if (shouldDebug) { logger.LogDebug("Log debug!"); }
if (shouldInformation) { logger.LogInformation("Log information!"); }
if (shouldTrace) { logger.LogTrace("Log trace!"); }
Your code will become more bloated, but it will give you the flexibility to change logging level more easily. It should be noted, however, that changing an app setting would cause your app to restart anyways, so it wouldn't be much different than changing host.json and restarting the app.
Upvotes: -2
Reputation: 14324
For now I suppose we can't configure the log level using environment variables. And I don't think you need to get there. I think you just want specify different level for different function and the log level supports to specify the function.
log configuration in host.json.
"logging": {
"logLevel": {
// For specific function
"Function.MyFunction1": "Information",
// For all functions
"Function":"Error",
// Default settings, e.g. for host
"default": "None"
}
}
Upvotes: 2
Reputation: 14088
we can't configure the log level using environment variables. When function host starts, it reads log level from host.json and inject ILogger instance with corresponding filter rules. Host configurations are not env variables.
Upvotes: -1