opentracing-contrib / csharp-netcore

OpenTracing instrumentation for .NET Core 3.1 & .NET 6+ apps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Turn off DiagnosticSource Logs

hannahchan opened this issue · comments

I like the current implementation of OpenTracing where it creates a new span for each Activity and calls span.Log for each DiagnosticSource log event. However I'm dealing with an issue where spans can be too big and don't get reported. I was wondering if there is a way to turn off the DiagnosticSource log events but still keep the behaviour where a new span is created for each new Activity?

I particularly find the Microsoft.AspNetCore.Mvc logs noisy and mostly useless when troubleshooting issues with my application.

I have found this to be an issue too.

There is a class that is accessible that can be configured in DI after OpenTracing has been added.

services.AddSingleton<IOptions<GenericEventOptions>>((sp) => Options.Create(new GenericEventOptions { IgnoreAll = true }));

This will then remove all logs for generic events. Most of the 'Microsoft.AspNetCore.Mvc' events execute through the generic event processor.

Also, services.AddOpenTracing() effectively calls services.AddAspNetCore().AddCoreFx().AddEntityFrameworkCore().AddLoggerProvider();

You can then remove the .AddLoggingProvider() => services.AddAspNetCore().AddCoreFx().AddEntityFrameworkCore() Which will then remove any other span logs which the logger uses.

To add, I think it might be a good idea to add an extension method for configuring the GenericEventOptions as finding this was not obvious.

Thanks heaps. I just tried this out and it seems to work. I'm going to play around with it a bit more to see how much it can be fined tuned. FYI, I used the following line to configure the options as it seems a bit more cleaner.

services.Configure<GenericEventOptions>(options => options.IgnoreAll = true);

I'm having the same issue here. Sometimes the span logs have too many entries because of OpenTracing loging every single entity being tracked by Entity Framework.

I first tried to avoid AddEntityFrameworkCore being called by OpenTracing. So instead of calling:

services.AddOpenTracing()

I do:

services.AddOpenTracingCoreServices(otBuilder =>
{
  otBuilder
    .AddAspNetCore()
    .AddCoreFx()
    .AddLoggerProvider();
}

This is basically the implementation of AddOpenTracing without the call to AddEntityFrameworkCore. The problem with the code above is that it does not work. Entity Framework logs are still being logged.

This is because AddEntityFrameworkCore also disables the listener name "Microsoft.EntityFrameworkCore" for GenericDiagnostics, I guess so that two Subscribers are not registered to EF events sumiltaneously (EntityFrameworkCoreDiagnostics and GenericDiagnostics). But now since EntityFrameworkCore listener name is not ignored, GenericDiacnostics is registered as a subscriber for EntityFrameworkCore events.
So even if you don't call AddEntityFrameworkCore a subscriber is registered.

The solution is to do:

services.AddOpenTracingCoreServices(otBuilder =>
{
  otBuilder
    .AddAspNetCore()
    .AddCoreFx()
    .AddLoggerProvider()
    .ConfigureGenericDiagnostics(options=>options.IgnoreListenerNames.Add("Microsoft.EntityFrameworkCore"));
}

Note that there is a .ConfigureEntityFrameworkCore extension method but it does no provide any useful option to disable it.

To me all this is an ugly hack. Possibly there is an easier way by using some Diagnostigs configuration. I've not been able to find it out by looking at the code. The extensive DiagnosticSource User's Guide is not helping either.

What we as users need are simple instructions on how to disable any log that is automatically added.

It's now possible to better configure the events that are added to spans:

  • To completely disable tracing generic DiagnosticListeners, use otBuilder.RemoveGenericDiagnostics(). This is the replacement for GenericEventOptions.IgnoreAll.
  • To ignore all events from a certain generic DiagnosticListener, use otBuilder.ConfigureGenericDiagnostics(options => options.IgnoredListenerNames.Add("My.Listener").
  • To ignore certain events from a certain listener, use otBuilder.ConfigureGenericDiagnostics(options => options.IgnoreEvent("My.Listener", "MyEvent") (missing in v0.7.0, but available again in v0.7.1).
  • To disable all events for an instrumented library (ASP.NET Core, EF Core, SqlClient, HttpClient), use its corresponding Configure*(options => options.LogEvents = false) method - e.g. otBuilder.ConfigureEntityFrameworkCore(options => options.LogEvents = false).
  • To ignore certain events from an instrumented library (ASP.NET Core, EF Core, SqlClient, HttpClient), use its corresponding Configure*(options => options.IgnoredEvents.Add("MyEvent")) method - e.g. otBuilder.ConfigureAspNetCore(options => options.IgnoredEvents.Add("Microsoft.AspNetCore.Mvc.AfterOnAuthorization")).

Note that the instrumented libraries now already set some defaults for IgnoredEvents to reduce noise. Use IgnoredEvents.Clear() to get all events again and modify the lists to your liking.