Mohamad Lashgarbolok
Mohamad Lashgarbolok

Reputation: 51

add logs to traces with ILogger (.Net web api)

I want to add some logs to my traces using, ILogger, opentrlemetry protocol and Jaegercollector.

this is my code:

var serviceName = "some_service_name" + mode.ToString();
var jaegrCollectorUrl = "http://jaeger_collector_url:4317";


builder.Services.AddOpenTelemetry()
                    .ConfigureResource(resource => resource.AddService(serviceName: serviceName))
                    .WithMetrics()
                    .WithTracing(tracing => tracing
                        .AddJaegerExporter(a =>
                        {
                            a.Endpoint = new Uri(jaegrCollectorUrl);
                        })
                        .AddAspNetCoreInstrumentation(opt =>
                        {
                            opt.EnrichWithHttpRequest = (activity, httpRequest) =>
                            {
                                activity.DisplayName = httpRequest.Path.Value;
                            };
                        })
                        .AddHttpClientInstrumentation()
                        .AddEntityFrameworkCoreInstrumentation(options =>
                        {
                            options.EnrichWithIDbCommand = (activity, command) =>
                            {
                                activity.DisplayName = $"EFCore DB Command: ({command.CommandType})";
                                activity.SetTag("command_text", command.CommandText);
                                activity.AddTag("method_name", null);
                            };
                        })
                        .AddOtlpExporter(otlpOptions =>
                        {
                            otlpOptions.Endpoint = new Uri(jaegrCollectorUrl);
                            otlpOptions.Protocol = OtlpExportProtocol.Grpc;
                        }));

builder.Logging.ClearProviders();
Log.Logger = new LoggerConfiguration()
               .MinimumLevel.Information()
               .WriteTo.Console()
               .WriteTo.OpenTelemetry(a =>
               {
                   a.Endpoint = jaegrCollectorUrl;
                   a.Protocol = OtlpProtocol.Grpc;
               })
               .CreateLogger();
builder.Logging.AddSerilog(Log.Logger);

but when i use:

 activity = ActivitySource.StartActivity("Welcome");
 var event1 = new ActivityEvent("log 1", DateTimeOffset.Now, []);
 Activity.Current.AddEvent(event1);

i see my logs in jaeger-ui

but when i use

logger.LogInformation("Starting work. 2: ");

i don't see any logs

am i wrong somewhere?

i use serilog and microsoft logger, but i can't handle problem

Upvotes: 5

Views: 396

Answers (2)

Argy Dev
Argy Dev

Reputation: 21

Referencing this answer

  • Install install OpenTelemetry.Extensions

Add AttachLogsToActivityEvent

builder.Logging.AddOpenTelemetry(options =>
{
  options.SetResourceBuilder(resource);
  options.IncludeFormattedMessage = true;
  options.IncludeScopes = true;
  options.ParseStateValues = true;
  options.AttachLogsToActivityEvent();
});

Upvotes: 2

tronda
tronda

Reputation: 3942

When you use an ActivityEvent the event is part of the Activity/Span document which is typically persisted in a document database. The Jaeger Collector can't take OTEL log signal and map that to an ActivityEvent/SpanEvent.

It would have been great if there was a log2spanevent-connector in the OpenTelemetry Collector where the log entry got translated to a SpanEvent which then Jaeger Collector could add to the persisted span.

This change would require such a connector and changes to Jaeger Collector and it is not likely to happen any time soon.

Upvotes: 2

Related Questions