zoukankan      html  css  js  c++  java
  • EF Code 如何输出sql语句

    首先写拷贝下面类

        public class EFLoggerProvider : ILoggerProvider
        {
            public ILogger CreateLogger(string categoryName) => new EFLogger(categoryName);
            public void Dispose() { }
        }
    
    
        public class EFLogger : ILogger
        {
            private readonly string categoryName;
    
            public EFLogger(string categoryName) => this.categoryName = categoryName;
    
            public bool IsEnabled(LogLevel logLevel) => true;
    
            public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
            {
                //ef core执行数据库查询时的categoryName为Microsoft.EntityFrameworkCore.Database.Command,日志级别为Information
                if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command"
                        && logLevel == LogLevel.Information)
                {
                    var logContent = formatter(state, exception);
                    //TODO: 拿到日志内容想怎么玩就怎么玩吧
                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(logContent);
                    Console.ResetColor();
                }
            }
    
            public IDisposable BeginScope<TState>(TState state) => null;
        }

    然后在dbcontext里面,应用日志

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                var loggerFactory = new LoggerFactory();
                loggerFactory.AddProvider(new EFLoggerProvider());
                optionsBuilder.UseLoggerFactory(loggerFactory);
    
                base.OnConfiguring(optionsBuilder);
            }
  • 相关阅读:
    CUDA 纹理内存
    CUDA三维数组
    cutil.h问题
    GPU和CPU耗时统计方法
    NVIDIA CUDA Library Documentation
    device not ready cuda
    送给女朋友的礼物
    手机屏幕录制软件分享
    统计函数运行时间-CPU端
    二十四孝,图文并茂,古今必读!
  • 原文地址:https://www.cnblogs.com/IWings/p/12436298.html
Copyright © 2011-2022 走看看