zoukankan      html  css  js  c++  java
  • 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属性赋值,但上面这种方式更好一些。

  • 相关阅读:
    输入法searchLookUpEditd的使用
    DevExpress GridControl使用方法总结
    DevExpress 控件中GridControl的使用
    MSSQl 事务的使用
    Python class NameError name "xxx" is not defined
    win 10 slmgr.vbs -xpr 无法运行,被豆麦笔记打开解决方法
    git checkout 撤销多个文件,撤销整个文件夹
    Python argparse 模块,参数传递
    Python Enum 枚举 用法汇总
    git branch & checkout fetch 的使用和冲突解决
  • 原文地址:https://www.cnblogs.com/wileywong/p/4027016.html
Copyright © 2011-2022 走看看