loggingazure-functionsazure-application-insights

Exclude EF messages from Logging


In order to transmit log messages to Azure Application Insights, I am setting up logging in a.Net C# Azure functions application. View the contents of my Program.cs below. How can I set up the logging such that the EntityFramework messages are not recorded?

I want to keep recording LogInfo() messages but the rule I'm deleting is meant to only allow errors to be logged.

var config = new ConfigurationBuilder()
    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();
var connectionString = config["defaultConnection"];

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults(worker => worker.UseNewtonsoftJson())
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
    })
    .ConfigureLogging(logging =>
    {
        logging.Services.Configure<LoggerFilterOptions>(options =>
        {
            foreach (LoggerFilterRule rule in options.Rules)
            {
                if (rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider")
                    defaultRule = rule;
            }
            if (defaultRule is not null)
            {
                options.Rules.Remove(defaultRule);
            }
        });
    })
    .ConfigureOpenApi()
    .ConfigureServices(services =>
    {
        services.AddDbContext<DijestContext>(options =>
                options.UseSqlServer(
                connectionString,
                sqlServerOptions => sqlServerOptions.CommandTimeout(600)));
    }
    )
    .Build();

host.Run();

Solution

  • I think adding this to the start of your ConfigureLogging should do the trick

    logging.AddFilter("Microsoft.EntityFrameworkCore", LogLevel.None);
    logging.AddFilter("Default", LogLevel.Information);