首页 > 代码库 > EntityFramework日志记录
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 + "\r\n执行时长:" + 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 =http://www.mamicode.com/= 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属性赋值,但上面这种方式更好一些。
EntityFramework日志记录
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。