Log Queries executed by Entity Framework DbContext

后端 未结 5 1787
夕颜
夕颜 2020-11-27 16:16

I\'m using EF 6.0 with LINQ in MVC 5 project. I want to log all the SQL queries executed by the Entity Framework DbContext for debugging/performance-measurement purpose.

5条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 17:12

    If you've got a .NET Core setup with a logger, then EF will log its queries to whichever output you want: debug output window, console, file, etc.

    You merely need to configure the 'Information' log level in your appsettings. For instance, this has EF logging to the debug output window:

    "Logging": {
      "PathFormat": "Logs/log-{Date}.txt",
      "IncludeScopes": false,
      "Debug": {
        "LogLevel": {
          "Default": "Information",
          "System": "Information",
          "Microsoft": "Information"
        }
      },
      "Console": {
        "LogLevel": {
          "Default": "Information",
          "System": "Warning",
          "Microsoft": "Warning"
        }
      },
      "File": {
        "LogLevel": {
          "Default": "Information",
          "System": "Warning",
          "Microsoft": "Warning"
        }
      },
      "LogLevel": {
        "Default": "Information",
        "System": "Warning",
        "Microsoft": "Warning"
      }
    }
    

提交回复
热议问题