Azure Function集成Application Insights无法显示Information级日志
解决Azure Function中Information级日志显示在Application Insights的方法
1. 正确配置host.json的日志级别
你使用的是**.NET隔离模式(Isolated Worker)**的Azure Function,需在host.json中设置正确的日志过滤规则,确保Information级别日志被收集并发送到Application Insights。以下是标准配置示例:
{ "version": "2.0", "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" } }, "logLevel": { "default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } } }
logLevel.default设为Information,确保默认日志类别输出Information及以上级别日志Microsoft类别设为Warning可减少框架冗余日志,可根据实际需求调整
2. 排查采样配置干扰
如果Application Insights的采样规则过于严格,可能会过滤掉Information级日志。若需临时验证,可将samplingSettings.isEnabled设为false(生产环境不建议禁用采样),确认日志是否能正常显示。
3. 验证Program.cs配置有效性
你的Program.cs配置已包含Application Insights的必要服务,无需修改,当前配置是正确的:
var host = new HostBuilder() .ConfigureFunctionsWorkerDefaults() .ConfigureServices(services => { services.AddApplicationInsightsTelemetryWorkerService(); services.ConfigureFunctionsApplicationInsights(); }) .Build(); host.Run();
4. 用正确的Kusto查询验证日志
在Application Insights的日志界面,使用以下查询精准定位Information级日志:
traces | where severityLevel == 1 // Information级别对应severityLevel值为1 | order by timestamp desc
注意:日志同步存在延迟,部署变更后需等待3-5分钟再查询。
5. 检查应用设置的覆盖项
进入Azure门户的函数应用,打开配置->应用程序设置,排查是否存在AzureFunctionsJobHost__logging__logLevel__default这类配置项,若有则其值会覆盖host.json的设置,需确保该值为Information。
内容的提问来源于stack exchange,提问作者TimBunting
相关产品推荐
相关产品推荐

