示例代码,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;
        }
    }
}

 

posted on 2017-06-26 17:28  王庆东mas  阅读(588)  评论(0编辑  收藏  举报