opentelemetry-dotnet icon indicating copy to clipboard operation
opentelemetry-dotnet copied to clipboard

UseOtlpExporter doesn't seem to work with just a logging configuration

Open davidfowl opened this issue 1 year ago • 2 comments

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
    <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
    <PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
  </ItemGroup>

</Project>
using OpenTelemetry;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddOpenTelemetry();

builder.Services.AddOpenTelemetry().UseOtlpExporter();

var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

cc @CodeBlanch

davidfowl avatar May 25 '24 04:05 davidfowl

@davidfowl your code works for me image What I think is a problem that you don't need builder.Logging.AddOpenTelemetry(); if you do builder.Services.AddOpenTelemetry().UseOtlpExporter(); since it enables the logging.

I have created an issue here because I want to use serilog as provider but seeing duplicated logs in my otel

The following code exports logs, dotnet 8.0.300, OTEL 1.8.1

using OpenTelemetry;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry().UseOtlpExporter();

var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

dario-griffo-paytently avatar May 27 '24 07:05 dario-griffo-paytently

@davidfowl

I just tested it, this seems to work fine:

builder.Logging.AddOpenTelemetry();

builder.Services.AddOpenTelemetry().UseOtlpExporter();

Which is what I would expect.

When using UseOtlpExporter you can just call:

builder.Services.AddOpenTelemetry().UseOtlpExporter();

Because UseOtlpExporter does the work of Logging.AddOpenTelemetry().

But this alone though wouldn't work:

builder.Logging.AddOpenTelemetry();

That would need to be:

builder.Logging.AddOpenTelemetry(options => options.AddOtlpExporter());

Can you clarify what you are trying to do and seeing?

CodeBlanch avatar May 28 '24 18:05 CodeBlanch

This issue was marked stale due to lack of activity and will be closed in 7 days. Commenting will instruct the bot to automatically remove the label. This bot runs once per day.

github-actions[bot] avatar Mar 25 '25 03:03 github-actions[bot]

Closed as inactive. Feel free to reopen if this issue is still a concern.

github-actions[bot] avatar Apr 01 '25 03:04 github-actions[bot]