• EntityFramework日志记录


    首先在应用启动时执行:DbInterception.Add(new LogFormatter());
     
    然后加入如下类:
        #region [ EF的数据库执行日志记录 ]
        public class LogFormatter : IDbCommandInterceptor
        {
            private readonly Stopwatch _stopwatch = new Stopwatch();
    
            public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
            {
                _stopwatch.Restart();
            }
    
            public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
            {
                _stopwatch.Stop();
                Log(command, interceptionContext);
            }
    
            public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
            {
                _stopwatch.Restart();
            }
    
            public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
            {
                _stopwatch.Stop();
                Log(command, interceptionContext);
            }
    
            public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
            {
                _stopwatch.Restart();
            }
    
            public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
            {
                _stopwatch.Stop();
                Log(command, interceptionContext);
            }
    
            private void Log<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
            {
                HRContractSqlLogger.Log(command, _stopwatch.ElapsedMilliseconds, interceptionContext.Exception);
            }
        }
    
        public static class HRContractSqlLogger
        {
            public static void Log(DbCommand command, long elapsedMilliseconds, Exception exception)
            {
                //Do something useful here with the raw data
                //Console.Write("SQL语句:" + command.CommandText + "
    执行时长:" + elapsedMilliseconds.ToString());
                StringBuilder lsbParameters = new StringBuilder();
                for(int i=0;i<command.Parameters.Count;i++)
                {
                    var p = command.Parameters[i];
                    lsbParameters.Append(p.ParameterName + ":" + (p.Value == null ? "NULL":p.Value.ToString()) + " ");
                }
                if (exception == null)
                {
                    Logger.WriteDBLog(command.CommandText, lsbParameters.ToString(), elapsedMilliseconds, Thread.CurrentThread.Name, "");
                }
                else
                {
                    //SQL错误日志,一般情况下,在EF里,不会走到这里,如果有错,在Execute方法中就会抛出了,也走不到这里
                    Logger.WriteErrorDBLog(command.CommandText + " " + exception.Message + "调用堆栈:" + exception.StackTrace, lsbParameters.ToString(), Thread.CurrentThread.Name,"");
                }
            }
        }
    
        #endregion

    还有一种比较简单的方式:使用 DataBase.Log属性赋值,但上面这种方式更好一些。

  • 相关阅读:
    Pyhton入门 笔记 第四天 Python的高级语法与用法
    Pyhton入门 笔记 第四天 正则表达式与JSON
    Pyhton入门 笔记 第三天 正则表达式与JSON
    Pyhton入门 笔记 第三天 面向对象 类
    Pyhton入门 练习 第三天 用循环输出小数点到某位
    Pyhton入门 笔记 第三天 函数
    Pyhton入门 笔记 第三天 包 模块 函数与变量作用域
    Pyhton入门 笔记 第二天 分支 循环 条件与枚举
    JS 获取地址栏参数
    文件的读取和写入
  • 原文地址:https://www.cnblogs.com/wileywong/p/4027016.html
Copyright © 2020-2023  润新知