Enable raw SQL logging in Entity Framework Core

后端 未结 2 1598
天命终不由人
天命终不由人 2020-12-30 08:53

How do I enable the logging of DbCommand raw SQL queries?

I have added the following code to my Startup.cs file, but do not see any log entries from the Entity Frame

相关标签:
2条回答
  • 2020-12-30 09:27

    Figured it out - need to configure DbContext to use logger factory.

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
    
        optionsBuilder.UseLoggerFactory(_loggerFactory);
    }
    
    0 讨论(0)
  • 2020-12-30 09:36

    From MVC Core 2, logging SQL is the default behaviour. Just make sure logging level in appSettings json file is correct.

    "Logging": {
      "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Microsoft": "Information"
      }
    }
    
    0 讨论(0)
提交回复
热议问题