.NET 8升级后Azure Function无LogInformation日志仅错误日志显示
解决Azure Functions .NET8下信息日志无法显示的问题
排查步骤与解决方案
1. 适配.NET8 Isolated Worker模型的host.json配置
.NET8 Azure Functions默认使用Isolated Worker模型,日志配置与In-Process模型存在差异。需在host.json中补充applicationInsights节点,确保信息级别日志被推送至监控服务:
{ "version": "2.0", "logging": { "fileLoggingMode": "always", "logLevel": { "default": "Information", "Function.IngressWebContent": "Information", "Host.Aggregator": "Information", "Host.Results": "Information" }, "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" }, "logLevel": { "default": "Information" } } } }
2. 确认Program.cs中的日志配置
Isolated模型需手动注册Application Insights日志提供者,确保在Program.cs中添加以下配置:
var host = new HostBuilder() .ConfigureFunctionsWorkerDefaults() .ConfigureLogging(logging => { logging.AddApplicationInsights(); logging.AddApplicationInsightsWebJobs(); logging.SetMinimumLevel(LogLevel.Information); }) .Build(); host.Run();
3. 验证应用设置中的监控连接信息
确保函数应用的应用设置中已正确配置Application Insights连接信息:
- 检查是否存在
APPLICATIONINSIGHTS_CONNECTION_STRING(推荐)或APPINSIGHTS_INSTRUMENTATIONKEY,值需与对应Application Insights资源的连接信息一致。
4. 调整日志查询方式
在Azure Portal中查看日志时,需使用正确的Kusto查询筛选信息级别日志:
- 进入函数应用的监控 > 日志页面
- 执行以下查询语句:
traces | where severityLevel == 2 // 对应Information级别 | where message contains "I cannot see the message in the portal" | order by timestamp desc
5. 排除采样过滤影响
若启用了Application Insights采样,可能会过滤部分信息日志。可临时调整采样设置验证:
"applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request,Trace", "fixedRate": 1.0 } }
6. 确认host.json已正确部署
检查Azure函数应用的site/wwwroot目录下的host.json是否与本地修改一致,避免部署时遗漏配置文件。
内容的提问来源于stack exchange,提问作者Tony
相关产品推荐
相关产品推荐

