OpenTelemetry 跟踪未发送到 Azure

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

我使用 OpenTelemetry 来检测 ASP.NET Core 项目并收集日志和跟踪。收集后,两者都应发送到 Azure Application Insights 资源以进行存储和查询。问题是,虽然日志和跟踪都成功捕获并显示在控制台上,但只有日志被发送到 Application Insights。无论我对配置进行什么更改,我似乎都无法在 Azure 中显示痕迹。我很想在跟踪表中看到哪怕是一条带有关联 ID 的跟踪。有谁知道为什么跟踪没有发送到 Azure?

请参阅下面的 OpenTelemetry 配置。

private void ConfigureOpenTelemetry(IServiceCollection services)
{
    var applicationInsightsConnectionString = "valid-connection-string";
    var serviceName = "backend-service-name"
    services.AddOpenTelemetry()
        .ConfigureResource(resource => resource.AddService(serviceName))
        .UseAzureMonitor(options =>
        {
            options.ConnectionString = applicationInsightsConnectionString;
        })
        .WithTracing(builder =>
        {
            builder.AddSource("ActivitySourceName");
            builder.AddAspNetCoreInstrumentation();
            builder.AddConsoleExporter();
            builder.AddAzureMonitorTraceExporter(o => o.ConnectionString = applicationInsightsConnectionString);
        });

    services.AddLogging(logging =>
    {
        logging.AddOpenTelemetry(options =>
        {
            options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName));
            options.AddConsoleExporter();
            options.AddAzureMonitorLogExporter(o => o.ConnectionString = applicationInsightsConnectionString);
        });
    });
}

我尝试了无数种配置 OpenTelemetry 的变体,并进行了三次检查,看看我的配置是否与文档和示例应用程序中的相同

c# azure azure-application-insights open-telemetry
1个回答
0
投票

我尝试过使用 .NET Core 8,并且能够使用您的代码和以下配置在 Application Insights 中查看跟踪、依赖项和请求。

我的

appsettings.json
文件:

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  }
}

本地控制台中显示的日志在 Application Insights 中显示为不同的参数。

本地:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5161
LogRecord.Timestamp:               2025-01-10T10:28:30.0781350Z
LogRecord.CategoryName:            Microsoft.Hosting.Lifetime
LogRecord.Severity:                Info
LogRecord.SeverityText:            Information
LogRecord.FormattedMessage:        Now listening on: http://localhost:5161
LogRecord.Body:                    Now listening on: {address}
LogRecord.Attributes (Key:Value):
    address: http://localhost:5161
    OriginalFormat (a.k.a Body): Now listening on: {address}
LogRecord.EventId:                 14
LogRecord.EventName:               ListeningOnAddress

Resource associated with LogRecord:
service.name: New Service Name
service.instance.id: b7bffadb-3981-4b27-bb69-e95644b8321a
telemetry.sdk.name: opentelemetry
telemetry.sdk.language: dotnet
telemetry.sdk.version: 1.10.0

Transaction Search
enter image description here

enter image description here

  • Application Insights 中显示的日志与 Trace 相同。

服务实例 ID 显示为

RoleInstance

  • 我的
    .csproj.cs
    文件:
 <ItemGroup>
   <PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.2.0" />
   <PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.3.0" />
   <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0" />
   <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.10.0" />
   <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" />
   <PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.10.1" />
   <PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.10.0" />
 </ItemGroup>

另请参阅此 MSDoc 了解更多详细信息。

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