EF语句拦截器-匹配当前的Controller,Action,User
示例代码,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的更多相关文章
- 基于SpringMVC拦截器和注解实现controller中访问权限控制
SpringMVC的拦截器HandlerInterceptorAdapter对应提供了三个preHandle,postHandle,afterCompletion方法. preHandle在业务处理器 ...
- 在ASP.NET Core MVC中子类Controller拦截器要先于父类Controller拦截器执行
我们知道在ASP.NET Core MVC中Controller上的Filter拦截器是有执行顺序的,那么如果我们在有继承关系的两个Controller类上,声明同一种类型的Filter拦截器,那么是 ...
- SpringMVC之八:基于SpringMVC拦截器和注解实现controller中访问权限控制
SpringMVC的拦截器HandlerInterceptorAdapter对应提供了三个preHandle,postHandle,afterCompletion方法. preHandle在业务处理器 ...
- struts2 在拦截器进行注入(依据Action是否实现自己定义接口)
比如:经常在Action中都须要获取当前登录的User,就须要获取Session.然后从Session获取当前登录的User,由于这些步骤都是反复操作,能够想办法在拦截器中进行实现.能够自己定义一个接 ...
- springmvc拦截器匹配规则
- ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action (转载)
ASP.NET MVC 一.获取URL(ASP.NET通用): [1]获取完整url(协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [ ...
- EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离
回到目录 前几天看了一个基于sqlserver的负载均衡与读写分离的软件Moebius,实现的方式还是不错的,这使得用sqlserver数据库的同学时有机会对数据库进行更有效的优化了
- Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例
利用Spring的拦截器可以在处理器Controller方法执行前和后增加逻辑代码,了解拦截器中preHandle.postHandle和afterCompletion方法执行时机. 自定义一个拦截器 ...
- struts2-第二章-拦截器
一,回顾 (1)默认action,404问题;<default-action-ref name="action 名称"/> (2)模块化,package,struts. ...
随机推荐
- mycat 多个逻辑库加读写分离
上3篇笔记主要记录了,读写分离,单库分表,分库分表 这次改动了下,实现了分库分表以及读写分离,基于多个逻辑库,先看配置文件 server.xml <user name="root&qu ...
- webpack+avalon+mmState打包方案
终于到讲授如何整合avalon社区这个最强大的组件,基于状态机的路由系统了! 基于状态机的路由系统,据我所知,目前世界上只有三款,angular社区的ui-router, 网易出品的stateman, ...
- mysql游标错误
DECLARE done INTEGER DEFAULT 0;DECLARE cur CURSOR FOR SELECT f_time,f_value FROM this_yc201508;DECLA ...
- Uni2D 入门 -- Asset Table
转载 http://blog.csdn.net/kakashi8841/article/details/17686791 Uni2D生成了一个自定义的表格用于保存你资源的唯一ID的引用.这个表格用于更 ...
- 吴裕雄 oracle 函数、触发器和包编程
- hdu1576-A/B-(同余定理+乘法逆元+费马小定理+快速幂)
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 设计table表格,用js设计偶数行和奇数行显示不同的颜色
第一种:鼠标经过时table表格中的颜色根据奇偶行改变不同的颜色 <!DOCTYPE html> <html> <head> <meta charset=&q ...
- react+webpack+babel环境搭建
[react+webpack+babel环境搭建] 1.react官方文档推荐使用 babel-preset-react.babel-preset-es2015 两个perset. Babel官方文档 ...
- Webpack Plugin
[Webpack Plugin] Since Loaders only execute transforms on a per-file basis, plugins are most commonl ...
- jenkin 不必要的Execute shell执行失败,导致jenkins都失败的解决
问题:jenkins里配置了多个执行shell,且有后续的执行job任务.但其中一个Execute shell执行失败了导致后续的shell都不执行了 而这个失败的shell并不是一定要执行 解决 ...