You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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记录:
两条完全相同的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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 06:48:25