Overriding Log Level for Azure Functions

╄→гoц情女王★ 提交于 2020-04-16 02:27:12

问题


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.


回答1:


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.




回答2:


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"
    }
}



回答3:


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.



来源:https://stackoverflow.com/questions/60658607/overriding-log-level-for-azure-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!