在NET Core程序中,我们常常期望能够直观地查看EF向数据库发送的sql,本文将介绍如何配置以查看sql日志

LoggerFactory

首先,我们在Startup类中添加LoggerFactory:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public static readonly LoggerFactory MyLoggerFactory
    = new LoggerFactory(new[] { new DebugLoggerProvider() });
    …………
    …………
    …………
    }

ConfigureServices

然后,修改Startup类中的ConfigureServices方法,添加上一步新建的MyLoggerFactory

    public void ConfigureServices(IServiceCollection services)
    {
    services.AddDbContext<AppDbContext>(options =>
                options.UseLoggerFactory(MyLoggerFactory).UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    …………
    …………
    …………
    }

之后,我们再次启动Debug模式,就可以在输出框中看到EF向数据库发送的SQL了