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

EntityFramework日志记录的更多相关文章

  1. 前端学HTTP之日志记录

    前面的话 几乎所有的服务器和代理都会记录下它们所处理的HTTP事务摘要.这么做出于一系列的原因:跟踪使用情况.安全性.计费.错误检测等等.本文将谥介绍日志记录 记录内容 大多数情况下,日志的记录出于两 ...

  2. RMS Server打开或关闭日志记录

    原文: https://technet.microsoft.com/zh-cn/library/cc732758 在 Active Directory Rights Management Servic ...

  3. 从零开始编写自己的C#框架(20)——框架异常处理及日志记录

    最近很忙,杂事也多,所以开发本框架也是断断续续的,终于在前两天将前面设定的功能都基本完成了,剩下一些小功能遗漏的以后发现再补上.接下来的章节主要都是讲解在本框架的基础上进行开发的小巧. 本框架主要有四 ...

  4. PHP日志记录规范PSR-3

    .note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...

  5. 利用AOP与ToStringBuilder简化日志记录

    刚学spring的时候书上就强调spring的核心就是ioc和aop blablabla...... IOC到处都能看到...AOP么刚开始接触的时候使用在声明式事务上面..当时书上还提到一个用到ao ...

  6. Log4Net异常日志记录在asp.net mvc3.0的应用

    前言 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.本文主要是简单的介绍如何在Visual ...

  7. ABP理论学习之日志记录

    返回总目录 本篇目录 服务端 获取Logger 基类中的Logger 配置 客户端 服务端 ABP使用的是Castle Windsor的日志记录设备.它可以和不同的日志类库一起工作,比如Log4Net ...

  8. Web APi之异常处理(Exception)以及日志记录(NLog)(十六)

    前言 上一篇文章我们介绍了关于日志记录用的是Log4net,确实也很挺强大,但是别忘了我们.NET有专属于我们的日志框架,那就是NLog,相对于Log4net而言,NLog可以说也是一个很好的记录日志 ...

  9. c#自定义日志记录

    废话不多说,直接上代码: 很简单:将类复制到项目中,最后在配置文件上配置一下:logUrl即可. 默认保存在:项目/temp/log /// <summary> /// 日志类 /// & ...

随机推荐

  1. 利用StringBuffer向字符串特定的重复子字符串插入数据

    public class InsertDetail {    public void insertInvoiceDetail(StringBuffer sb, String Label, String ...

  2. 验证码计时 -- UIButton setTitle 闪烁问题解决方案

    首先,有各种版本 方法一: 我运用的一种极其简单的版本:  将UIButton的Type 设成 Custom 就不会有闪烁的问题重现 p.p1 { margin: 0.0px 0.0px 0.0px ...

  3. Android中帧动画的创建

    帧动画,实质上就是快速播放多张连接效果的图片,现在一般可用于下拉刷新时候的headView 实现步骤: 1.首先应该准备一组连接效果的图片 2.在res>drawable目录下创建xml文件,将 ...

  4. JSON及与XML比较

      JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - Decem ...

  5. php:跨域

    一个没那么难的历史难题,其实只要在被请求端,加一句: header('Access-Control-Allow-Origin: *'); 然后--然后没有了. //跨域访问的时候才会存在此字段 $or ...

  6. 3-jQuery - AJAX get()

    介绍 GET 基本上用于从服务器获得(取回)数据.注释:GET 方法可能返回缓存数据. 格式 $.get(URL,callback); //必需的 URL 参数规定您希望请求的 URL. //可选的 ...

  7. 关于ApkTool不同版本在重新打游戏包时候的区别

    在工作中由于使用到将游戏CP提供的apk包重新反编译,然后二次签名出包,所以遇到了一个奇怪的bug. 下面是CP提供的apk包,将近358M 但是在重新编译之后,包变成了250M左右的apk包,虽然可 ...

  8. java 读取excel 正常 xls

    package com.sun.test; import java.io.BufferedInputStream;import java.io.File;import java.io.FileInpu ...

  9. 《高性能Javascript》读书笔记-2

    第二章 数据存取 字面量: 代表自身 不存特定位置   :字符串 数字 bool 对象 array 函数  正则表达 本地变量: var 数组: 存储在js数组对象内部,以数字做索引 对象成员 存储在 ...

  10. ebtables使用

    Targets: (1)ACCEPT : 让帧通过 (2)DROP: 丢弃帧. (3)CONTINUE:让帧继续走下面的规则(rule) (4)RETURN: 停止当前链的过滤规则,进而去走前面链的下 ...