EntityFramework日志记录
#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日志记录的更多相关文章
- 前端学HTTP之日志记录
前面的话 几乎所有的服务器和代理都会记录下它们所处理的HTTP事务摘要.这么做出于一系列的原因:跟踪使用情况.安全性.计费.错误检测等等.本文将谥介绍日志记录 记录内容 大多数情况下,日志的记录出于两 ...
- RMS Server打开或关闭日志记录
原文: https://technet.microsoft.com/zh-cn/library/cc732758 在 Active Directory Rights Management Servic ...
- 从零开始编写自己的C#框架(20)——框架异常处理及日志记录
最近很忙,杂事也多,所以开发本框架也是断断续续的,终于在前两天将前面设定的功能都基本完成了,剩下一些小功能遗漏的以后发现再补上.接下来的章节主要都是讲解在本框架的基础上进行开发的小巧. 本框架主要有四 ...
- PHP日志记录规范PSR-3
.note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...
- 利用AOP与ToStringBuilder简化日志记录
刚学spring的时候书上就强调spring的核心就是ioc和aop blablabla...... IOC到处都能看到...AOP么刚开始接触的时候使用在声明式事务上面..当时书上还提到一个用到ao ...
- Log4Net异常日志记录在asp.net mvc3.0的应用
前言 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.本文主要是简单的介绍如何在Visual ...
- ABP理论学习之日志记录
返回总目录 本篇目录 服务端 获取Logger 基类中的Logger 配置 客户端 服务端 ABP使用的是Castle Windsor的日志记录设备.它可以和不同的日志类库一起工作,比如Log4Net ...
- Web APi之异常处理(Exception)以及日志记录(NLog)(十六)
前言 上一篇文章我们介绍了关于日志记录用的是Log4net,确实也很挺强大,但是别忘了我们.NET有专属于我们的日志框架,那就是NLog,相对于Log4net而言,NLog可以说也是一个很好的记录日志 ...
- c#自定义日志记录
废话不多说,直接上代码: 很简单:将类复制到项目中,最后在配置文件上配置一下:logUrl即可. 默认保存在:项目/temp/log /// <summary> /// 日志类 /// & ...
随机推荐
- MTK平台Android中常用的路径
开机Logo的路径: bootable\bootloader\lk\dev\logo 开机Logo图片分辨率路径: device\rq\rq6735_35gt_b_l1\ProjectConfig.m ...
- fiddler抓包使用①
链接:http://jingyan.baidu.com/article/3a2f7c2e0d5f2126aed61175.html 设置好代理后,有的设备需要访问"192.168.1.1 ...
- HDU-1757--A Simple Math Problem(矩阵乘法)
Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x > ...
- cocos2d-3.x 创建动画
1.多文件帧序列动画 TrademarkAnimation.h #ifndef __TRADEMARK_ANIMATION_H__ #define __TRADEMARK_ANIMATION_H__ ...
- HDU 5834 Magic boy Bi Luo with his excited tree
树形dp. 先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解. 然后从上到下可以推出所有答案.细节较多,很容易写错. #pragma comment(linker, " ...
- MSSQL记录
批量添加: DECLARE @GID INT,@UID INT,@Indexs INTSET @GID=1SET @UID=37SET @Indexs=0WHILE @GID<674 --674 ...
- agentX各个角色功能
AgentX Roles 1.master An entity acting in a master agent role performs the following functions: - ...
- HDU 1242 Rescue(BFS),ZOJ 1649
题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...
- [ An Ac a Day ^_^ ] UVALive 7270 Osu! Master
2015icpc北京区域赛的签到题 还是很签到的 一次就过了 题意呢 就是统计B C后最长上升序列还有S的个数 当然B C要被S分割开…… /* *************************** ...
- java 内部类(摘抄自网络)
Java内部类 1.内部类分为成员内部类.静态嵌套类.方法内部类.匿名内部类. 几种内部类的共性: A.内部类仍然是一个独立的类,在编译之后会内部类会被编译成独立的.class文件,但是前面冠以外部类 ...