Can't get tracing to work with Host.CreateDefaultBuilder #4051
Answered
by
Kielek
ramonsmits
asked this question in
Q&A
-
I'm trying to get it to work with the Host builder and used the following as inspiration: https://opentelemetry.io/docs/instrumentation/net/getting-started/ However, no tracing information is shown in the console. The following discussions exists but don't seem to have a resolution.
The mentioned solution to invoke using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
{
services.AddOpenTelemetry().WithTracing(tracerProviderBuilder =>
{
tracerProviderBuilder
.AddConsoleExporter()
.AddSource(MyService.serviceName)
.ConfigureResource(x => x.AddService(MyService.serviceName));
//.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName: MyService.serviceName));
});
services.AddTransient<MyService>();
services.AddHostedService<MyService>();
});
var host = builder.Build();
await host.RunAsync();
class MyService : IHostedService
{
public const string serviceName = "MyCompany.MyProduct.MyService";
static ActivitySource MyActivitySource = new ActivitySource(serviceName);
private readonly ILogger<MyService> _logger;
public MyService(ILogger<MyService> logger)
{
_logger = logger;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
using var activity = MyActivitySource.StartActivity("SayHello");
activity?.SetTag("foo", 1);
activity?.SetTag("bar", "Hello, World!");
activity?.SetTag("baz", new int[] { 1, 2, 3 });
_logger.LogInformation("Doing something");
}
public async Task StopAsync(CancellationToken cancellationToken)
{
//throw new NotImplementedException();
}
} Packages: <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.4.0-rc.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.4.0-rc.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.4.0-rc.1" /> |
Beta Was this translation helpful? Give feedback.
Answered by
Kielek
Jan 3, 2023
Replies: 1 comment
-
Please try services.AddOpenTelemetry().WithTracing(tracerProviderBuilder =>
{
/* your implementation*/
}).StartWithHost(); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ramonsmits
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please try