如何让Azure Function的Information日志显示在Application Insights中?
问题解决:Azure Function Information级日志无法写入Application Insights
可能的原因及解决方案
1. 调整Application Insights采样设置
默认采样机制可能过滤大量Information级日志。你当前的samplingSettings仅排除了Request类型,但Trace(对应日志的Information/Warning等类型)可能被采样规则拦截。修改host.json的applicationInsights.samplingSettings:
"applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request", "includedTypes": "Trace", // 明确包含Trace类型 "maxTelemetryItemsPerSecond": 100 // 调高采样阈值,或设为0临时关闭采样测试 }, "enableLiveMetricsFilters": true }
测试阶段可先关闭采样(isEnabled: false),确认日志是否能正常写入AI后,再调整合适的采样策略。
2. 单独配置Application Insights的日志级别
全局logLevel配置可能无法完全覆盖Application Insights的日志过滤规则,需在applicationInsights节点下单独指定日志级别:
"logging": { "fileLoggingMode": "always", "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" }, "enableLiveMetricsFilters": true, "logLevel": { // 新增AI专属日志级别配置 "default": "Information", "Function": "Information" } }, "logLevel": { "default": "Information", "Function": "Information" } }
3. 检查日志类别的级别配置
你创建日志器时使用的是loggerFactory.CreateLogger<VisitGeocode>(),对应日志类别为你的命名空间.VisitGeocode。可在host.json的logLevel中明确指定该类别的级别,避免全局配置遗漏:
"logLevel": { "default": "Information", "Function": "Information", "YourNamespace.VisitGeocode": "Information" // 替换为实际的命名空间+类名 }
4. 验证Program.cs的Telemetry配置
确保Telemetry配置未过滤Information级日志,可在Program.cs中补充配置:
builder.Services.AddApplicationInsightsTelemetryWorkerService() .ConfigureFunctionsApplicationInsights(options => { // 确保Trace类型日志的级别限制未排除Information options.TelemetryProcessorChainBuilder.UseFilter<SeverityLevelFilter>( filter => filter.MinLevel = Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information); });
验证步骤
- 修改配置后重新部署Azure Function
- 触发函数,等待3-5分钟(AI日志存在延迟)
- 在Application Insights的
traces表中查询:traces | where severityLevel == 1(Information对应severityLevel=1)
内容的提问来源于stack exchange,提问作者Yndigo Dream
相关产品推荐
相关产品推荐

