Ignore sending logs for tracing in .net

314 Views Asked by At

I am using OpenTelemetry to send traces to Jaeger int .net 6 by this code:

    services.AddOpenTelemetry()
        .ConfigureResource(configureResource)
        .WithTracing(b =>
        {
            b.AddAspNetCoreInstrumentation()
                .AddHttpClientInstrumentation()
                .AddSqlClientInstrumentation()
                .AddOtlpExporter(options => { options.Endpoint = new Uri("http://localhost:4317"); });
        });

the problem is that all the logs using ILogger and all the health checks are sent to Jaeger. I want to ingnore them.

1

There are 1 best solutions below

3
On

AddAspNetCoreInstrumentation has an overload with options. You can configure a Filter option to filter undesired requests:

builder.AddAspNetCoreInstrumentation(options =>
{
    options.Filter = (httpContext) =>
    {
        options.Filter = context => !context.Request.Path.Equals("/health");
    };
})

Similarly to AspNetCore instrumentation, the HttpClientInstrumentation has the FilterHttpRequestMessage to filter the outgoing requests:

builder.AddHttpClientInstrumentation(options =>
{
    options.FilterHttpRequestMessage = httpRequestMessage =>
        httpRequestMessage.RequestUri?.PathAndQuery != "/health";
});

To stop sending logs, you need to remove the OpenTelemetry logging provider from the Program.cs file.

Depending on the format of your Program class, it may look like this

builder.Logging.AddOpenTelemetry()

or this

.ConfigureLogging(logging => logging.AddOpenTelemetry())