首先在应用启动时执行: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属性赋值,但上面这种方式更好一些。