使用 ILogger 进行 ApplicationInsights 日志记录会重复跟踪 - 日志出现两次

问题描述 投票:0回答:2

我有一个 .NET Core API,我正在使用

ILogger
DI 登录到 Azure AppInsights。 这是使用
Microsoft.ApplicationInsights.AspNetCore
nuget 包和文档:https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core

在我的 Program.cs 中,我像这样注册它:

builder.Services.AddApplicationInsightsTelemetry();

在我的 API 中,我像这样注入 ILogger:

public FileService(ILogger<FileService> logger)
{
    _logger = logger;
}

并像这样使用它:

_logger.LogInformation($"Starting upload");

这工作正常,我的日志在 AppInsights 中显示为跟踪,但每次我登录时,都会出现两条相同的跟踪:

请注意,对于每个重复项,

traceId
是相同的。另外,我的日志语句上的断点显示它们只被命中一次。

还有其他人遇到过这种情况吗?

--

更新:我尝试禁用各种选项,但这并没有停止重复。

ApplicationInsightsServiceOptions aiOptions = new();
aiOptions.EnableAdaptiveSampling = false;
aiOptions.EnableQuickPulseMetricStream = false;
aiOptions.EnableDebugLogger = false;
aiOptions.EnableDiagnosticsTelemetryModule = false;
builder.Services.AddApplicationInsightsTelemetry(aiOptions);
asp.net-core azure-application-insights ilogger appinsights
2个回答
1
投票

您遇到的问题(Azure Application Insights 中出现具有相同跟踪 ID 的重复跟踪)可能与应用程序中 ILogger 的配置方式有关。当 Application Insights 设置为记录 ILogger 输出及其内部遥测跟踪时,可能会出现此行为。


0
投票

使用这个包没有任何问题

 Microsoft.Extensions.Logging.ApplicationInsights

程序.cs

builder.Logging.AddApplicationInsights(
        configureTelemetryConfiguration: (config) =>
            config.ConnectionString = "InstrumentationKey=xxxxxx;IngestionEndpoint=https://xxx.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/",
            configureApplicationInsightsLoggerOptions: (options) => { }
    );
builder.Logging.AddFilter<ApplicationInsightsLoggerProvider>("provider1", LogLevel.Trace);

控制器

    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly ILogger<ValuesController> _logger;

        public ValuesController(ILogger<ValuesController> logger)
        {
            this._logger = logger;
        }
        [HttpGet]
        public void test()
        {
            _logger.LogInformation("A info ");
            _logger.LogWarning("A warn ");
        }
    }

© www.soinside.com 2019 - 2024. All rights reserved.