buhong 发表于 2018-10-20 09:38:20

EntityFramework之监听者判断SQL性能指标

private void Executed(DbCommand command, DbCommandInterceptionContext interceptionContext)  
      {            var timer = (Stopwatch)interceptionContext.UserState;
  
            timer.Stop();            if (interceptionContext.Exception != null)
  
            {
  
                File.AppendAllLines(
  
                  _logFile,                  new string[]
  
                {                "错误SQL语句",
  
                interceptionContext.Exception.Message,
  
                command.CommandText,
  
                Environment.StackTrace,                string.Empty,                string.Empty,
  
                });
  
            }            else if (timer.ElapsedMilliseconds >= _executionTime)
  
            {
  
                File.AppendAllLines(
  
                  _logFile,                  new string[]
  
                {                string.Format("耗时SQL语句({0}ms)",timer.ElapsedMilliseconds),
  
                command.CommandText,
  
                Environment.StackTrace,                string.Empty,                string.Empty,
  
                });
  
            }
  
      }


页: [1]
查看完整版本: EntityFramework之监听者判断SQL性能指标