最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

entity framework - Suppress Logger output from EF Core SQL when using NLog - Stack Overflow

programmeradmin2浏览0评论

I am trying to get EF Core to stop writing SQL to my log files. I am using NLog. I have the following in my appsettings.json:

"Logging":
{
    "LogLevel":
    {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information",
        "Microsoft.AspNetCore": "Warning",
        "Microsoft.EntityFrameworkCore.Database.Command": "Warning"
    }
},

I tried setting the following when creating the context:

    services.AddDbContext<TContext>(o =>
    {
        o.UseSqlServer(connectionString);
        o.ConfigureWarnings(c => c.Log((RelationalEventId.CommandExecuting, LogLevel.Warning)));
    });

And I tried something like this when adding the logging:

    services.AddLogging(logging =>
    {
        logging.ClearProviders();
        logging.SetMinimumLevel(LogLevel.Trace);
        logging.AddConfiguration(configuration.GetSection("Logging"));
        logging.AddNLog();
        logging.AddFilter("Microsoft", LogLevel.Warning);
    });

Every time is still logs the Info for the logging:

2024-11-20 15:13:36.3284||INFO|Microsoft.EntityFrameworkCore.Database.Command|Executed DbCommand (34ms)...

What am I missing?

I am trying to get EF Core to stop writing SQL to my log files. I am using NLog. I have the following in my appsettings.json:

"Logging":
{
    "LogLevel":
    {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information",
        "Microsoft.AspNetCore": "Warning",
        "Microsoft.EntityFrameworkCore.Database.Command": "Warning"
    }
},

I tried setting the following when creating the context:

    services.AddDbContext<TContext>(o =>
    {
        o.UseSqlServer(connectionString);
        o.ConfigureWarnings(c => c.Log((RelationalEventId.CommandExecuting, LogLevel.Warning)));
    });

And I tried something like this when adding the logging:

    services.AddLogging(logging =>
    {
        logging.ClearProviders();
        logging.SetMinimumLevel(LogLevel.Trace);
        logging.AddConfiguration(configuration.GetSection("Logging"));
        logging.AddNLog();
        logging.AddFilter("Microsoft", LogLevel.Warning);
    });

Every time is still logs the Info for the logging:

2024-11-20 15:13:36.3284||INFO|Microsoft.EntityFrameworkCore.Database.Command|Executed DbCommand (34ms)...

What am I missing?

Share Improve this question edited Nov 24, 2024 at 12:14 Rolf Kristensen 20k2 gold badges58 silver badges82 bronze badges asked Nov 20, 2024 at 21:18 Tyrel Van NiekerkTyrel Van Niekerk 1,7125 gold badges23 silver badges39 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Your code looks correct; here are my suggestions.

  1. My best guess is that something you may have specified in the Nlog config is overriding the configurations in appsettings.json.

To test this, could you add the following inside services.AddLogging()? Since this filter is at the code level, It should help override NLog settings.

logging.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning);

  1. Do you have an Nlog.config file, and if so, confirm if a more permissive rule is specified for Microsoft.EntityFrameworkCore.Database.Command?
发布评论

评论列表(0)

  1. 暂无评论