zoukankan      html  css  js  c++  java
  • 2. EF Core 如何显示执行的SQL语句

    调试的时候需要查看执行的SQL 语句,我一般是使用 SQL Profiler,当然还有另外一种方式,就是配置EF 日志,这两种方式都比较简单实用,SQL Profiler可以过滤掉很多自己不想看的日志,可以只看某一个IP的日志,而EF Core 的日志则不可以;

    SQL Profiler

    TODO 我会在这里添加一个附件,以后使用记得修改hostname

    EF Core 日志

    设置启动方式

    在launchSettings.json中删除IIS节点,使程序以控制台应用启动

    在Program.cs 配置日志

    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    
    namespace CompanyApp
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                CreateHostBuilder(args).Build().Run();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureLogging((hostingContext, logging) =>
                    {
                        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                        logging.AddConsole();
                        logging.AddDebug();
                        logging.AddEventSourceLogger();
                    })
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    });
        }
    }

    启用显示敏感数据

    默认是只能看到参数,看不到参数的值,在startup.cs 的ConfigureServices 方法中启用显示敏感数据:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
    
        services.AddDbContext<CompanyDbContext>(options => {
            //启用显示敏感数据
            options.EnableSensitiveDataLogging(true);
            options.UseSqlServer(Configuration.GetConnectionString("CompanyDbContext"));
        });
    }

    配置 appsettings.json 日志选项

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information",
          "Microsoft.EntityFrameworkCore.Database.Command": "Information"
        }
      },
      "AllowedHosts": "*",
      "ConnectionStrings": {
        "CompanyDbContext": "Server=(localdb)\\mssqllocaldb;Database=CompanyDb;Trusted_Connection=True;MultipleActiveResultSets=true"
      }
    }
  • 相关阅读:
    【2018.05.05 C与C++基础】C++中的自动废料收集:概念与问题引入
    【2018.04.27 C与C++基础】关于switch-case及if-else的效率问题
    【2018.04.19 ROS机器人操作系统】机器人控制:运动规划、路径规划及轨迹规划简介之一
    March 11th, 2018 Week 11th Sunday
    March 10th, 2018 Week 10th Saturday
    March 09th, 2018 Week 10th Friday
    March 08th, 2018 Week 10th Thursday
    March 07th, 2018 Week 10th Wednesday
    ubantu之Git使用
    AMS分析 -- 启动过程
  • 原文地址:https://www.cnblogs.com/maanshancss/p/13360334.html
Copyright © 2011-2022 走看看