可以跟踪数据库脚本能很好的帮助我们理解ABP , SQL Server Profiler当然是很好的工具 , 好像MySQL没有这么方便的工具

这里我们讨论用日志记录的方法 

1, 首先我们建两个类来记录EFCore产生的脚本日志

ABP.Net Core使用教程(三)记录数据库脚本日志

EFLogger.cs

using Microsoft.Extensions.Logging;
using System;

namespace AbpDemo.EntityFrameworkCore.Logger
{
    public class EFLogger : ILogger
    {
        public Castle.Core.Logging.ILogger Logger { get; set; }

        private readonly string _categoryName;

        public EFLogger(string categoryName, Castle.Core.Logging.ILogger logger)
        {
            this._categoryName = categoryName;
            this.Logger = logger;
        }

        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
            var logContent2 = formatter(state, exception);
            if (_categoryName == "Microsoft.EntityFrameworkCore.Database.Command"
                && logLevel == LogLevel.Information)
            {
                var logContent = formatter(state, exception);
                Logger.Warn(logContent);
            }
        }

        public IDisposable BeginScope<TState>(TState state) => null;
    }
}
View Code

相关文章: