如何为Azure函数的Host.json设置正确的日志记录级别,以优化应用程序洞察的成本? [英] How to set correct logging levels to host.json of Azure Functions to optimize costs of Application Insight?
问题描述
如何优化Azure函数的应用程序洞察成本?
我的日志似乎太多,而Application Insight的成本也很高。 几乎所有的成本都来自报文。不是来自度量值。
我正在尝试修改Host.json,但在本地调试中,我的新Host.json没有提供调试所需的信息。
修改日志记录时,在命令行中看不到以下任何日志记录:
logging.info(f" Calling Activity Function")
我看到很多不相关的消息,比如(我正在开发Azure持久函数)
testhubname-control-03: Starting lease renewal with token 2c336948-1daa-49db-ae7e-9989cc340461
原始(对Application Insight记录的日志太多)
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
}
}
新建(我在命令行中看不到logging.info(f;调用活动函数&)结果)
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"logLevel": {
"default": "Information",
"Host.Results": "Error",
"Function": "Error",
"Host.Aggregator": "Information"
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
}
}
https://docs.microsoft.com/en-us/azure/azure-functions/configure-monitoring?tabs=v2
还尝试在ApplationInsight设置下添加LogLevel,但";LogLevel";未被接受。
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"LogLevel": {
"Default": "Warning"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
}
}
也尝试了,但没有成功:(出现属性不允许的错误)
{
"version": "2.0",
"ApplicationInsights": {
"InstrumentationKey": "my-instrumentation-key",
"EnableAdaptiveSampling": false,
"EnablePerformanceCounterCollectionModule": false
},
"Logging": {
"LogLevel": {
"Default": "Warning"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
}
}
推荐答案
解决方法之一是解决此问题:
- 在VS代码中创建了Azure函数(Python堆栈)。
- 将此代码
logging.info(f" Calling Activity Function")
添加到活动功能代码中,如下所示:
默认情况下,代码为host.json
:
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
}
当我使用本地存储仿真器运行此持久函数时,它记录信息级别:
如果我更改Host.json代码以包含问题中给出的代码,则无法在终端中获取Calling Activity Function logging information
:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"logLevel": {
"default": "Information",
"Host.Results": "Error",
"Function": "Error",
"Host.Aggregator": "Information"
}
}
修改host.json
信息级日志记录代码属性值,然后运行该函数,它将记录活动函数代码中指定的信息,如下所示:
host.json
日志代码中Host.Results
值为Error
且Function
值为Information
级别:
host.json
Like:
- 对于
Host.Results
或Function
的日志,仅记录Error
或更高级别的事件。 - 对于
Host.Aggregator
的日志,记录所有生成的指标(Trace
)。 - 对于包括用户日志在内的所有其他日志,仅记录
Information
级和更高级别的事件。
您的代码:
"logLevel": {
"default": "Information",
"Host.Results": "Error",
"Function": "Error",
"Host.Aggregator": "Information"
}
- 如果
Host.Results
类别设置为Error
日志级别,则它将仅收集表中requests
表中的主机执行遥测事件。 - 如果
Function
类别设置为Error
日志级别,它将停止收集所有函数的与dependencies
、customMetrics
和customEvents
相关的函数遥测数据,从而阻止在Application Insights中查看任何此类数据。它将只收集Error
级别的traces
日志。
若要优化应用程序洞察的成本,您可以使用为日志级别中定义的每个属性设置的不同值,将host.json
日志记录级别设置为收集的足够数据以了解您的函数行为。
Microsoft文档中提供了几种技术来控制监视功能应用程序的潜在成本的影响,如采样、每日上限设置、预聚合指标和自定义日志收集。
引用:Log Level Configuration and Potential Costs Impact Techniques in Azure Functions
更新答案:
如何配置Host.json,以便为AI和本地环境设置不同的日志级别?我需要优化AI中的日志数量。你能举个例子吗?
为了最大限度地减少日志数量,您可以在host.json
中使用更高级别的日志,如下面的截图所示:
正如您在这里看到的最小化日志,没有生成功能信息日志,输出日志显示在浏览器中,只有手动日志显示在日志/终端中。
这篇关于如何为Azure函数的Host.json设置正确的日志记录级别,以优化应用程序洞察的成本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!