.NET Core 3.1 Azure Function的Debug与Trace日志未在Application Insights显示
解决方案
你的问题核心是Azure Function运行时的日志过滤优先级规则导致低于Information级别的日志被拦截,加上重复注册Application Insights服务引发的配置冲突,可按以下步骤修复:
步骤1:调整host.json配置
Azure Function中host.json的日志规则优先级高于appsettings.json,默认未配置时会自动拦截Trace、Debug级别的函数日志,替换为以下配置:
{ "version": "2.0", "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request;Exception;Trace" }, "logLevel": { "Function.AIDemo": "Trace", "Default": "Debug" } }, "logLevel": { "Function.AIDemo": "Trace", "Default": "Debug", "Microsoft": "Warning", "System": "Warning" } } }
提示:如果修改后仍看不到低级别日志,可临时将
samplingSettings.isEnabled设为false,排除采样策略拦截的可能。
步骤2:简化Startup配置
.NET Core 3.1 进程内模型的Azure Function会默认读取APPINSIGHTS_INSTRUMENTATIONKEY环境变量自动初始化Application Insights,不需要手动重复注册相关服务,避免配置冲突,调整后的Configure方法参考:
public override void Configure(IFunctionsHostBuilder builder) { // 移除手动注册Application Insights的逻辑,由运行时自动初始化 builder.Services.AddMvcCore(); builder.Services.AddOptions(); }
步骤3:确认环境配置
- 本地测试时,
local.settings.json的Values节点下需配置正确的APPINSIGHTS_INSTRUMENTATIONKEY - 部署到Azure后,需在Function App的「配置」-「应用程序设置」中确认
APPINSIGHTS_INSTRUMENTATIONKEY配置正确,同时在「日志」选项卡中确认应用日志级别设置为Trace或Debug
内容的提问来源于stack exchange,提问作者Mike
相关产品推荐
相关产品推荐

