示例代码,ps:一切都能实现,关键是你尝试的方向,别把简单问题复杂化导致进入死胡同出不来。

using Mobile360.Core.Interfaces;
using Mobile360.Core.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure.Interception;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing; namespace Mobile360.Data
{
/// <summary>
/// 数据库执行拦截
/// </summary>
public class EFIntercepterLogging : DbCommandInterceptor
{ private readonly Stopwatch _stopwatch = new Stopwatch();
private IRepository repo;
public EFIntercepterLogging()
{
this.repo = DependencyResolver.Current.GetService<IRepository>();
} public override void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
base.ScalarExecuting(command, interceptionContext);
_stopwatch.Restart();
}
public override void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
_stopwatch.Stop();
AuditLog aLog = InitLog(); if (interceptionContext.Exception != null)
{
aLog.SqlQuery = (string.Format("Exception:{1} \r\n --> Error executing command: {0}", command.CommandText, interceptionContext.Exception.ToString()));
}
else
{
aLog.SqlQuery = (string.Format("\r\n执行时间:{0} 毫秒\r\n-->ScalarExecuted.Command:{1}\r\n", _stopwatch.ElapsedMilliseconds, command.CommandText));
} repo.Insert<AuditLog>(aLog);
repo.SaveChangesAsync(); base.ScalarExecuted(command, interceptionContext);
}
public override void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
base.NonQueryExecuting(command, interceptionContext);
_stopwatch.Restart();
}
public override void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
_stopwatch.Stop();
AuditLog aLog = InitLog(); if (interceptionContext.Exception != null)
{
aLog.SqlQuery = (string.Format("Exception:{1} \r\n --> Error executing command:\r\n {0}", command.CommandText, interceptionContext.Exception.ToString()));
}
else
{
aLog.SqlQuery = (string.Format("\r\n执行时间:{0} 毫秒\r\n-->NonQueryExecuted.Command:\r\n{1}", _stopwatch.ElapsedMilliseconds, command.CommandText));
}
repo.Insert<AuditLog>(aLog);
repo.SaveChangesAsync(); base.NonQueryExecuted(command, interceptionContext);
}
public override void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
base.ReaderExecuting(command, interceptionContext);
_stopwatch.Restart();
}
public override void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
_stopwatch.Stop();
AuditLog aLog = InitLog(); if (interceptionContext.Exception != null)
{
aLog.SqlQuery = (string.Format("Exception:{1} \r\n --> Error executing command:\r\n {0}", command.CommandText, interceptionContext.Exception.ToString()));
}
else
{
aLog.SqlQuery = (string.Format("\r\n执行时间:{0} 毫秒 \r\n -->ReaderExecuted.Command:\r\n{1}", _stopwatch.ElapsedMilliseconds, command.CommandText));
}
repo.Insert<AuditLog>(aLog);
repo.SaveChangesAsync(); base.ReaderExecuted(command, interceptionContext);
} private AuditLog InitLog()
{
AuditLog log = new AuditLog(); HttpContextBase context = new HttpContextWrapper(HttpContext.Current);
RouteData rd = RouteTable.Routes.GetRouteData(context);
if (rd != null)
{
string controllerName = rd.GetRequiredString("controller");
string actionName = rd.GetRequiredString("action");
string userName = HttpContext.Current.User.Identity.Name; log.Controller = controllerName;
log.Action = actionName;
log.StartTime = DateTime.Now;
log.EndTime = DateTime.Now;
log.AuditAccount = userName; }
return log;
}
}
}

EF语句拦截器-匹配当前的Controller,Action,User的更多相关文章

  1. 基于SpringMVC拦截器和注解实现controller中访问权限控制

    SpringMVC的拦截器HandlerInterceptorAdapter对应提供了三个preHandle,postHandle,afterCompletion方法. preHandle在业务处理器 ...

  2. 在ASP.NET Core MVC中子类Controller拦截器要先于父类Controller拦截器执行

    我们知道在ASP.NET Core MVC中Controller上的Filter拦截器是有执行顺序的,那么如果我们在有继承关系的两个Controller类上,声明同一种类型的Filter拦截器,那么是 ...

  3. SpringMVC之八:基于SpringMVC拦截器和注解实现controller中访问权限控制

    SpringMVC的拦截器HandlerInterceptorAdapter对应提供了三个preHandle,postHandle,afterCompletion方法. preHandle在业务处理器 ...

  4. struts2 在拦截器进行注入(依据Action是否实现自己定义接口)

    比如:经常在Action中都须要获取当前登录的User,就须要获取Session.然后从Session获取当前登录的User,由于这些步骤都是反复操作,能够想办法在拦截器中进行实现.能够自己定义一个接 ...

  5. springmvc拦截器匹配规则

  6. ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action (转载)

    ASP.NET MVC 一.获取URL(ASP.NET通用): [1]获取完整url(协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [ ...

  7. EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离

    回到目录 前几天看了一个基于sqlserver的负载均衡与读写分离的软件Moebius,实现的方式还是不错的,这使得用sqlserver数据库的同学时有机会对数据库进行更有效的优化了

  8. Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例

    利用Spring的拦截器可以在处理器Controller方法执行前和后增加逻辑代码,了解拦截器中preHandle.postHandle和afterCompletion方法执行时机. 自定义一个拦截器 ...

  9. struts2-第二章-拦截器

    一,回顾 (1)默认action,404问题;<default-action-ref name="action 名称"/> (2)模块化,package,struts. ...

随机推荐

  1. java为什么有些异常throw出去需要在函数头用throws声明,一些就不用。

    Excepiton分两类:checked exception.runtime exception:直接继承自Exception就是checked exception,继承自RuntimeExcepti ...

  2. wx小程序获取组件属性数据data-prop

    在微信小程序中有时会在组件上定义一些属性,使用data-来定义 <view data-idvalue="id" data-Index-Name="IndexName ...

  3. Linux基本命令(新手入门使用)

    Linux常用基本命令主要包括目录操作命令.文件操作命令.文件查看命令.磁盘管理命令.用户管理命令.系统管理命令等. 目录操作命令:cd.ls.mkdir.pwd.rmdir 文件操作命令:cp.mv ...

  4. 编译Linux内核(Mac OS平台)

    操作系统第一次实验需要编译Linux内核,我之前在Mac上一直使用的都是Parallels Desktop这个软件,所以这次也将课程网站上提供的Ubuntu安装在了PD上,但是编译完内核后无法进入Ub ...

  5. Android 操作UI线程的一些方法

    我们经常会在后台线程中去做一些耗时的操作,比如去网络取数据.但是当数据取回来,需要显示到页面上的时候,会遇到一些小麻烦,因为我们都知道,android的UI页面是不允许在其他线程直接操作的.下面总结4 ...

  6. css3文本和颜色

    1.文本阴影text-shadow 语法 text-shadow:X-Offset Y-Offset blur color; X-Offset:表示阴影的水平偏移距离,其值为正值时阴影向右偏移,反之向 ...

  7. Python中struct.pack()和struct.unpack()

    https://blog.csdn.net/tjuyanming/article/details/79700601 https://www.cnblogs.com/yezl/p/5861787.htm ...

  8. Maximum Likelihood及Maximum Likelihood Estimation

    1.What is Maximum Likelihood? 极大似然是一种找到最可能解释一组观测数据的函数的方法. Maximum Likelihood is a way to find the mo ...

  9. 1.5.1、CDH 搭建Hadoop在安装之前(定制安装解决方案---使用内部包裹存储库)

    使用内部包裹存储库 您可以通过托管内部存储库或手动将存储库文件复制到Cloudera Manager主机来为Cloudera Manager创建parcel存储库. 继续阅读: 托管内部包裹存储库 配 ...

  10. tomcat启动时引用非JAVA_HOME的指定路径

    参考 https://jingyan.baidu.com/article/066074d62d371cc3c21cb0ec.html 先查看bin/catalina.bat 再查看bin/setcla ...