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属性赋值,但上面这种方式更好一些。

  • 相关阅读:
    团队贡献分汇总
    【Gamma】Scrum Meeting 1
    【Beta】测试报告
    【Beta】发布说明
    【技术博客】JWT的认证机制Django项目中应用
    Daily Scrumming* 2015.12.17(Day 9)
    Daily Scrumming* 2015.12.16(Day 8)
    Daily Scrumming* 2015.12.15(Day 7)
    Daily Scrumming* 2015.12.13(Day 5)
    Daily Scrumming* 2015.12.12(Day 4)
  • 原文地址:https://www.cnblogs.com/wileywong/p/4027016.html
Copyright © 2011-2022 走看看