admin管理员组

文章数量:1125885

I have used OpenTelemetry to instrument an ASP.NET Core project and gather logs and traces. After collection both should be sent to an Azure Application Insights resource for storage and querying. The issue is that while both logs and traces are succesfully captured and displayed on the console, only the logs are being sent to Application Insights. No matter what changes I make to the configuration I can't seem to get the traces to show up in Azure. I would love to see even a single trace with a correlationId in the traces table. Does anyone have an idea why traces are not being sent to Azure?

See OpenTelemetry configuration below.

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);
        });
    });
}

I have tried countless variations of configuring OpenTelemetry and triple-checked to see if my config was the same as in the docs and example applications

I have used OpenTelemetry to instrument an ASP.NET Core project and gather logs and traces. After collection both should be sent to an Azure Application Insights resource for storage and querying. The issue is that while both logs and traces are succesfully captured and displayed on the console, only the logs are being sent to Application Insights. No matter what changes I make to the configuration I can't seem to get the traces to show up in Azure. I would love to see even a single trace with a correlationId in the traces table. Does anyone have an idea why traces are not being sent to Azure?

See OpenTelemetry configuration below.

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);
        });
    });
}

I have tried countless variations of configuring OpenTelemetry and triple-checked to see if my config was the same as in the docs and example applications

Share Improve this question asked 2 days ago Jasper KeijzerJasper Keijzer 11 bronze badge New contributor Jasper Keijzer is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 7
  • Are you sure the connection string is correct? The only relevant configuration here is builder.AddAzureMonitorTraceExporter(o => o.ConnectionString = applicationInsightsConnectionString); You could try setting the Diagnostics parameter of AzureMonitorExporterOptions to see what's actually going on – Panagiotis Kanavos Commented 2 days ago
  • Also, does your application use Activity at all? From this config it seems only ASP.NET Core is instrumented so you'll only see spans generated by ASP.NET Core in response to requests. – Panagiotis Kanavos Commented 2 days ago
  • I'm pretty certain that the connection string is correct because logs do show up in Azure using the same connection string. Also I do use Activity in my application but haven't included that code because I figured it wasn't relevant to the configuration. I can see both ASP.NET Core and custom spans in the console when using the ConsoleExporter. Thanks for the Diagnostics suggestion, I'll try to play around with that and see if I can get more info – Jasper Keijzer Commented 2 days ago
  • Please share your configuration file? – Harshitha Commented 2 days ago
  • Have you tried using a minimal example like the sample console project? If you can't get that to work, the problem is most likely the service configuration. Perhaps you need to provide AAD credentials? – Panagiotis Kanavos Commented 2 days ago
 |  Show 2 more comments

1 Answer 1

Reset to default 0

I have tried with .NET Core 8 and able to see the traces, Dependencies and requests in the Application Insights with your code and the below configuration.

My appsettings.json file:

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

The logs which are shown in Local Console are shown with different parameters in Application Insights.

Local:

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:

  • Same log is shown in Application Insights as Trace.

Service Instance ID is shown as RoleInstance.

  • My .csproj.cs file :
 <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>

Also refer this MSDoc for more details.

本文标签: cOpenTelemetry traces not being sent to AzureStack Overflow