Tracing and logging within BackgroundService #3666
Unanswered
stevenmccormack
asked this question in
Q&A
Replies: 1 comment
-
Hi, I had the same issue as I ran a background job in Aspire.
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
...
})
.WithTracing(tracing =>
{
tracing.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddSource("ProducerService");
}
});
public class ProducerService(...): BackgroundService
{
public static string ActivitySourceName = "ProducerService";
private static readonly ActivitySource activitySource = new(ActivitySourceName);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using (var activity = activitySource.StartActivity("Producer: ProduceMessage"))
{
.... Thats all |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi all,
I am using OpenTelemetry with Elasticsearch and Kibana in an API which is working fine. Some of the API calls are triggering a background task which is added to a queue. The main application adds items to the queue using a service. The Background service periodically checks for tasks and runs them. My Problem is that when the tasks are processed I can either have traces (which are not part of the main API request) or I can have logging. As soon as I do
activity.SetParentId()
there is no trace sent, but if I do not set it the logs are not associated to the main API request.I created a simplified project for debugging the issue. Please find below the relevant parts:
Program.cs:
Config.cs
Controller action:
BackgroundWorkerService.cs
BackgroundWorker.cs
Can anybody can tell me hot to make version two also generate a trace?
Beta Was this translation helpful? Give feedback.
All reactions