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]