zoukankan      html  css  js  c++  java
  • 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} 
     --> Error executing command: {0}", command.CommandText, interceptionContext.Exception.ToString()));
                }
                else
                {
                    aLog.SqlQuery = (string.Format("
    执行时间:{0} 毫秒
    -->ScalarExecuted.Command:{1}
    ", _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} 
     --> Error executing command:
     {0}", command.CommandText, interceptionContext.Exception.ToString()));
                }
                else
                {
                    aLog.SqlQuery = (string.Format("
    执行时间:{0} 毫秒
    -->NonQueryExecuted.Command:
    {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} 
     --> Error executing command:
     {0}", command.CommandText, interceptionContext.Exception.ToString()));
                }
                else
                {
                    aLog.SqlQuery = (string.Format("
    执行时间:{0} 毫秒 
     -->ReaderExecuted.Command:
    {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;
            }
        }
    }
  • 相关阅读:
    爱情七十八课,闲了就“犯贱”
    阿里巴巴中文站的CSS设计规则(转)
    爱情八十一课,可预测的分手
    [性格][管理]《九型人格2》 唐·理查德·里索(美)、拉斯·赫德森(美)
    爱情八十二课,爱情三国杀
    爱情七十九课,不爱权力大
    [心理学]《爱情心灵安全岛》 四四
    一些你不知道的囧知识,保证让你崩溃
    爱情七十四课,我们的意义
    爱情七十六课,门当户对
  • 原文地址:https://www.cnblogs.com/x-poior/p/7081549.html
Copyright © 2011-2022 走看看