Azure Function App(Python)频繁出现Blob存储相关日志是否正常?
Azure Function App 日志流异常日志疑问
背景
开发了一个简单的Python定时Azure Function App,函数每4小时运行一次,向队列发送消息。
函数代码
@app.function_name(name="Example") @app.schedule(schedule="0 0 */4 * * *", arg_name="myTimer") @app.queue_output(arg_name="msg", queue_name=queue_name, connection="AzureWebJobsStorage") def my_function(myTimer: func.TimerRequest, msg: func.Out[typing.List[str]]) -> None: msg.set('Example Message') logging.info(f'Function run')
host.json 配置
{ "version": "2.0", "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[4.*, 5.0.0)" }, "logging": { "fileLoggingMode": "always", "logLevel": { "default": "Warning", "Function": "Information", "Host": "Error", "Host.Aggregator": "Information", "Azure.Core": "Error" }, "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" } }, "console": { "isEnabled": true } } }
异常日志现象
在Function App的日志流(Log Stream)中发现两类异常日志:
- 每15分钟左右出现一次警告日志:
[Warning] Error response [.] 409 The specified container already exists. (00.0s)
- 大量每几秒出现一次的信息级日志:
[Information] Request [] PUT https://[storagename].blob.core.windows.net/azure-webjobs-hosts/locks/[appname]/host?comp=lease
已通过host规则过滤Application Insights中的这类日志,但不确定这些日志是否本就不该出现在日志流中,想咨询这种情况是否正常,还是应用配置有误?
解答
关于每几秒出现的lease请求日志
这类日志是Azure Functions宿主的正常行为:
- Functions宿主会定期向存储账户的
azure-webjobs-hosts/locks容器发送Blob租约请求,用来实现宿主实例之间的分布式锁,确保只有一个实例负责调度定时触发器等任务,避免重复执行。 - 当前
host.json中Host日志级别设为Error,但这类租约请求日志属于存储SDK相关日志,不在Host分类下,所以会以Information级别输出到日志流。
关于15分钟一次的容器已存在警告
这个警告也是宿主运行过程中的正常现象:
- Functions宿主会尝试确保
azure-webjobs-hosts等必要容器存在,当容器已经存在时,会返回409错误,宿主会忽略这个错误继续运行,所以这个警告不影响功能,属于预期内的日志。
如何过滤这些日志到日志流
如果不想让这些日志出现在日志流中,可以调整host.json的日志级别配置,增加对存储相关日志类别的过滤:
"logging": { "fileLoggingMode": "always", "logLevel": { "default": "Warning", "Function": "Information", "Host": "Error", "Host.Aggregator": "Information", "Azure.Core": "Error", "Azure.Storage.Blobs": "Error", // 过滤存储Blob相关的Info/Warning日志 "Microsoft.Azure.WebJobs.Host.Storage": "Error" // 过滤宿主存储操作的日志 }, // 其他配置保持不变 }
总结:这两类日志都是Azure Functions宿主的正常运行日志,不会影响定时函数的功能,属于预期行为。如果觉得日志冗余,可以通过调整host.json的日志级别来过滤。
内容的提问来源于stack exchange,提问作者Merrick
相关产品推荐
相关产品推荐

