使用ILogger向ApplicationInsights记录日志时出现重复痕迹问题排查
.NET Core API日志重复写入Azure ApplicationInsights问题
问题背景
我维护一个.NET Core API,通过依赖注入的ILogger向Azure ApplicationInsights记录日志,使用的NuGet包为Microsoft.ApplicationInsights.AspNetCore。
注册与使用代码
- Program.cs中的服务注册:
builder.Services.AddApplicationInsightsTelemetry();
- 服务中注入
ILogger:
public FileService(ILogger<FileService> logger) { _logger = logger; }
- 日志记录代码:
_logger.LogInformation("Starting upload");
问题现象
日志能正常在ApplicationInsights中以trace类型显示,但每次执行日志记录代码时,都会生成两条完全相同的trace记录:
关键细节:
- 两条重复记录的traceId完全一致
- 日志语句的断点仅触发一次,说明代码只执行了一次
已尝试的无效操作
我尝试禁用多个ApplicationInsights配置选项,但重复日志问题依然存在:
ApplicationInsightsServiceOptions aiOptions = new(); aiOptions.EnableAdaptiveSampling = false; aiOptions.EnableQuickPulseMetricStream = false; aiOptions.EnableDebugLogger = false; aiOptions.EnableDiagnosticsTelemetryModule = false; builder.Services.AddApplicationInsightsTelemetry(aiOptions);
内容的提问来源于stack exchange,提问作者James McCormack
相关产品推荐
相关产品推荐

