Azure Functions自定义跟踪维度未被记录,求排查指引
问题排查与解决
1. 未配置日志收集规则
Azure Functions默认不会自动将BeginScope中的变量或日志模板占位符转为自定义维度输出到Application Insights。需修改host.json的日志配置,明确开启自定义维度收集:
{ "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" }, "customDimensions": { "IncludeScopes": true, "Test": "{Test}", "dim": "{dim}" } } } }
IncludeScopes: true:开启后会自动收集BeginScope内的所有自定义属性。- 显式声明
Test和dim:确保这两个字段被提取为独立的自定义维度。
2. 日志占位符未按结构化方式传递
原代码中{dim}仅会替换日志文本内容,不会自动成为维度。可改用结构化日志传递属性:
_logger.LogInformation("Attempting to log some extra dimensions", new { dim = "custom dim" });
结合IncludeScopes: true的配置,dim会被自动捕获为自定义维度。
3. Application Insights连接配置错误
确认函数应用的应用设置中已正确配置APPINSIGHTS_CONNECTION_STRING,未关联AI的情况下,自定义维度不会被写入跟踪日志。
4. 采样策略过滤了日志
若AI开启了采样,部分日志可能被过滤导致看不到自定义维度。可临时关闭采样测试:
"samplingSettings": { "isEnabled": false }
测试正常后再调整采样规则。
内容的提问来源于stack exchange,提问作者MBender
相关产品推荐
相关产品推荐

