using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Text;
using System.Data.Entity.Infrastructure.Interception;
using System.Diagnostics;
using System.Data.Common;

using NLog;

namespace Libing.Portal.Web.Common.Interceptors
{
    public class NLogDbCommandInterceptor : DbCommandInterceptor
    {
        private static readonly Stopwatch watch = new Stopwatch();
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();

        public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
            base.ScalarExecuting(command, interceptionContext);

            watch.Restart();
        }

        public override void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
            watch.Stop();

            if (interceptionContext.Exception != null)
            {
                logger.Error("Exception:{1} \r\n --> Error executing command: {0}", command.CommandText, interceptionContext.Exception.ToString());
            }
            else
            {
                StringBuilder message = new StringBuilder();
                message.AppendFormat("\r\n-->{0}", command.CommandText);
                foreach (DbParameter parameter in command.Parameters)
                {
                    message.AppendFormat("\r\n-- {0}: '{1}' (Type = {2}, IsNullable = {3})", parameter.ParameterName, parameter.Value, parameter.DbType, parameter.IsNullable);
                }
                message.AppendFormat("\r\n-- Completed in {0} ms", watch.ElapsedMilliseconds, command);

                logger.Trace(message.ToString());
            }

            base.ScalarExecuted(command, interceptionContext);
        }

        public override void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            base.NonQueryExecuting(command, interceptionContext);

            watch.Restart();
        }

        public override void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            watch.Stop();

            if (interceptionContext.Exception != null)
            {
                logger.Error("Exception:{1} \r\n --> Error executing command: {0}", command.CommandText, interceptionContext.Exception.ToString());
            }
            else
            {
                StringBuilder message = new StringBuilder();
                message.AppendFormat("\r\n-->{0}", command.CommandText);
                foreach (DbParameter parameter in command.Parameters)
                {
                    message.AppendFormat("\r\n-- {0}: '{1}' (Type = {2}, IsNullable = {3})", parameter.ParameterName, parameter.Value, parameter.DbType, parameter.IsNullable);
                }
                message.AppendFormat("\r\n-- Completed in {0} ms", watch.ElapsedMilliseconds, command);

                logger.Trace(message.ToString());
            }

            base.NonQueryExecuted(command, interceptionContext);
        }

        public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
            base.ReaderExecuting(command, interceptionContext);

            watch.Restart();
        }

        public override void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
            watch.Stop();

            if (interceptionContext.Exception != null)
            {
                logger.Error("Exception:{1} \r\n --> Error executing command: {0}", command.CommandText, interceptionContext.Exception.ToString());
            }
            else
            {
                StringBuilder message = new StringBuilder();
                message.AppendFormat("\r\n-->{0}", command.CommandText);
                foreach (DbParameter parameter in command.Parameters)
                {
                    message.AppendFormat("\r\n-- {0}: '{1}' (Type = {2}, IsNullable = {3})", parameter.ParameterName, parameter.Value, parameter.DbType, parameter.IsNullable);
                }
                message.AppendFormat("\r\n-- Completed in {0} ms", watch.ElapsedMilliseconds, command);

                logger.Trace(message.ToString());
            }

            base.ReaderExecuted(command, interceptionContext);
        }
    }
}
NLogDbCommandInterceptor.cs

相关文章: