示例代码,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. linux 覆盖可执行文件的问题

    测试环境是3.10.0 内核. 有一次操作中,发现cp -f A B执行的时候,行为不一样: 当B没被打开,则正常覆盖B. 当B是被打开,但没有被执行,则能覆盖, 当B被打开,且被执行,则不能直接覆盖 ...

  2. Examples: How to Pronounce T

    Examples: How to Pronounce T Share Tweet Share Tagged With: Flap T, Stop T The [t] sound is not alwa ...

  3. elk日志平台搭建小记

    最近抽出点时间,搭建了新版本的elk日志平台 elastaicsearch 和logstash,kibana和filebeat都是5.6版本的 中间使用redis做缓存,版本为3.2 使用的系统为ce ...

  4. [Linux]Linux下动态安装PHP扩展的一般方法(图)

    ---------------------------------------------------------------------------------------------------- ...

  5. Android DevArt3:SingleTask启动模式探究:首先从MainActivity启动案例入口AActivity,并在A中启动BActivity,从B启动CActivity, 再从C中又启动AActivity, 最后在A中启动B,现在按两次back键,然后回到的是哪个Activity? 答案是,回到MainActivity。

    SingleTask启动模式探究 GitHub如题:首先从MainActivity启动案例入口AActivity,并在A中启动BActivity,从B启动CActivity,再从C中又启动AActiv ...

  6. C#调用Delphi接口(ITest = interface)

    首先创建一个delphi的DLL工程 library testintfdll; { Important note about DLL memory management: ShareMem must ...

  7. 18.4 运行脚本 sudo ./launcher.sh 必须先给他权限 才能使用

    我们发现我们每次启动服务都要打一大堆命令,ng build --watch ,nodemon server.js ,等等 凡是重复的工作,我们要可以代码完成 要想运行它呢 你必须将你运行的上述(脚本中 ...

  8. ArcGIS案例学习笔记-查找重叠的多边形

    ArcGIS案例学习笔记-查找重叠的多边形 联系方式:谢老师,135-4855-4328,xiexiaokui@qq.com 目的:对于多边形图层,查找具有重叠(相互覆盖)的面 数据: 方法: 1. ...

  9. swift杂记

    1.0   数据类型强转  范围小 --->范围大  不会丢失精度  : 范围大 ---> 范围小 ,可能丢失精度 如 :Int(4.2) = 4 :CGFloat(2) = 2.0 2. ...

  10. nltk的使用

    1. 命令行; import nltk nltk.download() #下载相关模型等,...