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.
-
No, it is impossible. Host Configuration is not env variables. – Cindy Pau Mar 13 '20 at 01:00
-
Hi, If my answer answers your doubt, can you end it by [marking it as the answer](https://0730bowmanwindow.blob.core.windows.net/work/mark.png) to this question? Updating the host.json file is necessary because only the host information is read from it.:) – Cindy Pau Mar 16 '20 at 05:55
-
Done. Thank you. @BowmanZhu – Raffy Mar 16 '20 at 15:25
5 Answers
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

- 1,502
- 2
- 15
- 31
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"
}
}

- 13,703
- 2
- 11
- 26
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.
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.

- 13,085
- 1
- 15
- 27
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.

- 579
- 4
- 9
-
1This has no difference with changing host.json or function application `AzureFunctionsJobHost__logging__LogLevel__Default`: they all requires restart of function app. – binaryDi Dec 17 '20 at 05:11
-
@binaryDi the original question makes no mention or demand that restarts are to be avoided (and I already mentioned that caveat in my answer). Also, I did not claim this was the ONLY way to achieve the desired result...merely it was an option. So I do not find your comment helpful to the SO community at all. – Blair Allen Mar 03 '21 at 19:20