Ejemplo n.º 1
0
            /// <summary>
            /// Commands the executing.
            /// </summary>
            /// <param name="command">The command.</param>
            /// <param name="interceptionContext">The interception context.</param>
            /// <param name="userState">State of the user.</param>
            /// <inheritdoc />
            private void CommandExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext, out object userState)
            {
                userState = null;
                if (!interceptionContext.DbContexts.Any(a => this.DbContextList.Contains(a) || this.EnableForAllDbContexts))
                {
                    return;
                }

                if (!(command is System.Data.SqlClient.SqlCommand))
                {
                    // not a SQL command. Nothing is going to the Database. Probably interception.
                    return;
                }

                if (SessionId.IsNotNullOrWhiteSpace())
                {
                    if (System.Web.HttpContext.Current?.Session?.SessionID != SessionId)
                    {
                        return;
                    }
                }

                var incrementedCallCount = Interlocked.Increment(ref DebugHelper._callCounts);

                if (!TimingsOnly && !SummaryOnly)
                {
                    StringBuilder sbDebug = GetSQLBlock(command, incrementedCallCount);
                    _sqlOutput.Append(sbDebug);
                    System.Diagnostics.Debug.Write(sbDebug.ToString());
                }

                var sqlConnection = command.Connection as System.Data.SqlClient.SqlConnection;

                sqlConnection.StatisticsEnabled = true;
                sqlConnection.ResetStatistics();

                if (userState == null)
                {
                    userState = new DebugHelperUserState {
                        CallNumber = incrementedCallCount, Stopwatch = Stopwatch.StartNew()
                    };
                }
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Commands the executing.
            /// </summary>
            /// <param name="command">The command.</param>
            /// <param name="interceptionContext">The interception context.</param>
            /// <param name="userState">State of the user.</param>
            /// <inheritdoc />
            private void CommandExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext, out object userState)
            {
                userState = null;
                if (!interceptionContext.DbContexts.Any(a => this.DbContextList.Contains(a) || this.EnableForAllDbContexts))
                {
                    return;
                }

                DebugHelper._callCounts++;

                StringBuilder sbDebug = new StringBuilder();

                sbDebug.AppendLine("\n");

                StackTrace st     = new StackTrace(2, true);
                var        frames = st.GetFrames().Where(a => a.GetFileName() != null);

                sbDebug.AppendLine(string.Format("/* Call# {0}*/", DebugHelper._callCounts));

                sbDebug.AppendLine(string.Format("/*\n{0}*/", frames.ToList().AsDelimited("")));

                sbDebug.AppendLine("BEGIN\n");

                var declares = command.Parameters.OfType <System.Data.SqlClient.SqlParameter>()
                               .Select(p =>
                {
                    if (p.SqlDbType == System.Data.SqlDbType.NVarChar)
                    {
                        var sqlString   = ( SqlString )p.SqlValue;
                        string sqlValue = sqlString.IsNull ? "null" : sqlString.Value?.Truncate(255);

                        return(string.Format("@{0} {1}({2}) = '{3}'", p.ParameterName, p.SqlDbType, p.Size, sqlValue?.Replace("'", "''")));
                    }

                    if (p.SqlDbType == System.Data.SqlDbType.Int)
                    {
                        return(string.Format("@{0} {1} = {2}", p.ParameterName, p.SqlDbType, p.SqlValue ?? "null"));
                    }
                    else if (p.SqlDbType == System.Data.SqlDbType.Udt)
                    {
                        return(string.Format("@{0} {1} = '{2}'", p.ParameterName, p.UdtTypeName, p.SqlValue));
                    }
                    else if (p.SqlDbType == System.Data.SqlDbType.Bit)
                    {
                        return(string.Format("@{0} {1} = {2}", p.ParameterName, p.SqlDbType, ((System.Data.SqlTypes.SqlBoolean)p.SqlValue).ByteValue));
                    }
                    else if (p.SqlDbType == System.Data.SqlDbType.Decimal)
                    {
                        return(string.Format("@{0} {1} = {2}", p.ParameterName, p.SqlDbType, p.SqlValue ?? "null"));
                    }
                    else
                    {
                        return(string.Format("@{0} {1} = '{2}'", p.ParameterName, p.SqlDbType, p.SqlValue));
                    }
                }).ToList().AsDelimited(",\n");

                if (!string.IsNullOrEmpty(declares))
                {
                    sbDebug.AppendLine("DECLARE\n" + declares + "\n\n");
                }

                sbDebug.AppendLine(command.CommandText);

                sbDebug.AppendLine("\nEND\nGO\n\n");

                System.Diagnostics.Debug.Write(sbDebug.ToString());

                var sqlConnection = command.Connection as System.Data.SqlClient.SqlConnection;

                sqlConnection.StatisticsEnabled = true;
                sqlConnection.ResetStatistics();

                if (userState == null)
                {
                    userState = new DebugHelperUserState {
                        CallNumber = DebugHelper._callCounts, Stopwatch = Stopwatch.StartNew()
                    };
                }
            }
Ejemplo n.º 3
0
            /// <summary>
            /// Commands the executing.
            /// </summary>
            /// <param name="command">The command.</param>
            /// <param name="interceptionContext">The interception context.</param>
            /// <param name="userState">State of the user.</param>
            /// <inheritdoc />
            private void CommandExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext, out object userState)
            {
                userState = null;
                if (RockContext != null && !interceptionContext.DbContexts.Any(a => a == RockContext))
                {
                    return;
                }

                DebugHelper._callCounts++;

                StringBuilder sbDebug = new StringBuilder();

                sbDebug.AppendLine("\n");

                StackTrace st     = new StackTrace(1, true);
                var        frames = st.GetFrames().Where(a => a.GetFileName() != null);

                sbDebug.AppendLine(string.Format("/* Call# {0}*/", DebugHelper._callCounts));

                sbDebug.AppendLine(string.Format("/*\n{0}*/", frames.ToList().AsDelimited("")));

                sbDebug.AppendLine("BEGIN\n");

                var declares = command.Parameters.OfType <System.Data.SqlClient.SqlParameter>()
                               .Select(p =>
                {
                    if (p.SqlDbType == System.Data.SqlDbType.NVarChar)
                    {
                        return(string.Format("@{0} {1}({2}) = '{3}'", p.ParameterName, p.SqlDbType, p.Size, p.SqlValue.ToString().Replace("'", "''")));
                    }
                    if (p.SqlDbType == System.Data.SqlDbType.Int)
                    {
                        return(string.Format("@{0} {1} = {2}", p.ParameterName, p.SqlDbType, p.SqlValue ?? "null"));
                    }
                    else if (p.SqlDbType == System.Data.SqlDbType.Udt)
                    {
                        return(string.Format("@{0} {1} = '{2}'", p.ParameterName, p.UdtTypeName, p.SqlValue));
                    }
                    else
                    {
                        return(string.Format("@{0} {1} = '{2}'", p.ParameterName, p.SqlDbType, p.SqlValue));
                    }
                }).ToList().AsDelimited(",\n");

                if (!string.IsNullOrEmpty(declares))
                {
                    sbDebug.AppendLine("DECLARE\n" + declares + "\n\n");
                }

                sbDebug.AppendLine(command.CommandText);

                sbDebug.AppendLine("\nEND\nGO\n\n");

                if (userState == null)
                {
                    userState = new DebugHelperUserState {
                        CallNumber = DebugHelper._callCounts, Stopwatch = Stopwatch.StartNew()
                    };
                }

                System.Diagnostics.Debug.Write(sbDebug.ToString());
            }
Ejemplo n.º 4
0
            private static void HandleStatementCompleted(System.Data.SqlClient.SqlCommand sqlCommand, object sender, DebugHelperUserState debugHelperUserState)
            {
                // handle StatementCompleted Event
                debugHelperUserState.Stopwatch.Stop();
                var eventSqlConnection = sqlCommand.Connection as System.Data.SqlClient.SqlConnection;
                var eventStats         = eventSqlConnection.RetrieveStatistics();

                var eventCommandExecutionTimeInMs = ( long )eventStats["ExecutionTime"];

                debugHelperUserState.StatementCompletedStopwatchMS        = debugHelperUserState.Stopwatch.Elapsed.TotalMilliseconds;
                debugHelperUserState.StatementCompletedSqlExecutionTimeMS = eventCommandExecutionTimeInMs - debugHelperUserState.CommandExecutedSqlExecutionTimeMS;
                eventSqlConnection.StatisticsEnabled = false;
                string statementExecutionTimeText = $"{debugHelperUserState.StatementCompletedSqlExecutionTimeMS,6:0}     ms";

                if (debugHelperUserState.StatementCompletedSqlExecutionTimeMS < 2)
                {
                    statementExecutionTimeText = "-   ".PadLeft(13);
                }

                var statementCompletedElapsedTimeMS = debugHelperUserState.StatementCompletedStopwatchMS - debugHelperUserState.CommandExecutedStopwatchMS;

                if (!SummaryOnly)
                {
                    var summary = $@"
--  [{statementExecutionTimeText}] ExecutionTime (StatementCompleted)
--  [{statementCompletedElapsedTimeMS,10:0.000} ms] ElapsedTime   (StatementCompleted)
--  [{debugHelperUserState.StatementCompletedStopwatchMS,10:0.000} ms] Total".Trim();

                    System.Diagnostics.Debug.WriteLine(summary);
                }
            }