Esempio n. 1
0
        public void LogCommand(DbCommand command, DbCommandContext interceptionCommandContext)
        {
            var commandText = command.CommandText ?? "<null>";
            var commandInfo = new Command
            {
                IsAsync    = interceptionCommandContext.IsAsync,
                Sql        = commandText.Trim(),
                SqlHash    = commandText.Trim().ComputeHash(),
                StackTrace = new CallingMethod {
                    AssembliesToExclude = AssembliesToExclude
                }.GetCallingMethodInfo()
            };

            setBaseInfo(interceptionCommandContext, commandInfo);
            commandInfo.CommandId = UniqueIdExtensions <DbCommand> .GetUniqueId(command).ToInt();

            if (commandInfo.ConnectionId == null)
            {
                commandInfo.ConnectionId = UniqueIdExtensions <DbConnection> .GetUniqueId(command.Connection).ToInt();
            }

            if (command.Transaction != null)
            {
                commandInfo.TransactionId = UniqueIdExtensions <DbTransaction> .GetUniqueId(command.Transaction).ToInt();

                commandInfo.IsolationLevel = command.Transaction.IsolationLevel;
            }

            foreach (var parameter in command.Parameters.OfType <DbParameter>())
            {
                commandInfo.Parameters.Add(logParameter(parameter));
            }

            _baseInfoQueue.Enqueue(commandInfo);
        }
Esempio n. 2
0
        public void LogConnection(DbConnection connection,
                                  DbConnectionContext interceptionConnectionContext,
                                  CommandConnectionType type)
        {
            var commandConnection = new CommandConnection
            {
                IsAsync          = interceptionConnectionContext.IsAsync,
                Type             = type,
                IsCanceled       = interceptionConnectionContext.IsCanceled,
                ConnectionString = connection.ConnectionString,
                Exception        = interceptionConnectionContext.Exception != null ? interceptionConnectionContext.Exception.Message : "",
                StackTrace       = new CallingMethod {
                    AssembliesToExclude = AssembliesToExclude
                }.GetCallingMethodInfo()
            };

            setBaseInfo(interceptionConnectionContext, commandConnection);

            if (commandConnection.ConnectionId == null)
            {
                commandConnection.ConnectionId = UniqueIdExtensions <DbConnection> .GetUniqueId(connection).ToInt();
            }

            _baseInfoQueue.Enqueue(commandConnection);
        }
Esempio n. 3
0
        private static ISet <string> getKeys(DbInterceptionContext interceptionContext)
        {
            if (interceptionContext.ObjectContexts == null || !interceptionContext.ObjectContexts.Any())
            {
                return(new HashSet <string>());
            }

            var objectContext   = interceptionContext.ObjectContexts.First();
            var objectContextId = UniqueIdExtensions <ObjectContext> .GetUniqueId(objectContext).ToInt();

            HashSet <string> keys;

            if (_keys.TryGetValue(objectContextId, out keys))
            {
                return(keys);
            }

            var results     = new HashSet <string>();
            var entityProps = objectContext.MetadataWorkspace.GetItems <EntityType>(DataSpace.SSpace);

            foreach (var entityType in entityProps)
            {
                foreach (var edmProperty in entityType.KeyProperties)
                {
                    results.Add(edmProperty.Name);
                }
            }

            _keys.TryAdd(objectContextId, results);

            return(results);
        }
Esempio n. 4
0
        public void LogResult(DbCommand command, DbCommandContext interceptionCommandContext)
        {
            var result       = interceptionCommandContext.Result;
            var resultString = (object)result == null
                ? "null"
                : (result is DbDataReader)
                    ? result.GetType().Name
                    : result.ToString();

            var commandResult = new CommandResult
            {
                Exception           = interceptionCommandContext.Exception != null ? interceptionCommandContext.Exception.Message : "",
                IsCanceled          = interceptionCommandContext.IsCanceled,
                ElapsedMilliseconds = interceptionCommandContext.ElapsedMilliseconds,
                ResultString        = resultString,
                StackTrace          = new CallingMethod {
                    AssembliesToExclude = AssembliesToExclude
                }.GetCallingMethodInfo(),
                FieldsCount = gefFieldsCount(result)
            };

            setBaseInfo(interceptionCommandContext, commandResult);

            commandResult.CommandId = UniqueIdExtensions <DbCommand> .GetUniqueId(command).ToInt();

            if (commandResult.ConnectionId == null)
            {
                commandResult.ConnectionId = UniqueIdExtensions <DbConnection> .GetUniqueId(command.Connection).ToInt();
            }

            if (command.Transaction != null)
            {
                commandResult.TransactionId = UniqueIdExtensions <DbTransaction> .GetUniqueId(command.Transaction).ToInt();
            }

            if (interceptionCommandContext.DataTable != null && interceptionCommandContext.DataTable.Rows != null)
            {
                commandResult.RowsReturned = interceptionCommandContext.DataTable.Rows.Count;

                foreach (DataColumn column in interceptionCommandContext.DataTable.Columns)
                {
                    commandResult.Columns.Add(new ColumnInfo
                    {
                        ColumnName = column.ColumnName,
                        DataType   = column.DataType.ToString(),
                        Ordinal    = column.Ordinal,
                        IsKey      = interceptionCommandContext.Keys != null && interceptionCommandContext.Keys.Contains(column.ColumnName)
                    });
                }
            }

            _baseInfoQueue.Enqueue(commandResult);
        }
        public static DbTransactionContext GetLoggedDbTransaction(DbTransaction transaction, MutableInterceptionContext interceptionContext)
        {
            var context = new DbTransactionContext
            {
                IsAsync    = interceptionContext.IsAsync,
                IsCanceled = interceptionContext.TaskStatus.HasFlag(TaskStatus.Canceled),
                Exception  = interceptionContext.OriginalException ?? interceptionContext.Exception
            };

            setBaseInfo(interceptionContext, context);
            context.TransactionId = UniqueIdExtensions <DbTransaction> .GetUniqueId(transaction).ToInt();

            return(context);
        }
        public static DbCommandContext GetLoggedDbCommand(DbCommand command, Exception exception)
        {
            var context = new DbCommandContext
            {
                IsAsync           = false,
                IsCanceled        = false,
                Exception         = exception,
                ObjectContextId   = SessionIdLoggingContext.SessionId.GetUniqueId(),
                ObjectContextName = SessionId,
                ConnectionId      = UniqueIdExtensions <DbConnection> .GetUniqueId(command.Connection).ToInt()
            };

            return(context);
        }
        public static DbTransactionContext GetLoggedDbTransaction(DbTransaction transaction, int connectionId)
        {
            var context = new DbTransactionContext
            {
                IsAsync           = false,
                IsCanceled        = false,
                Exception         = null,
                ObjectContextId   = SessionIdLoggingContext.SessionId.GetUniqueId(),
                ObjectContextName = SessionId,
                ConnectionId      = connectionId,
                TransactionId     = UniqueIdExtensions <DbTransaction> .GetUniqueId(transaction).ToInt()
            };

            return(context);
        }
        private static void setBaseInfo(DbInterceptionContext interceptionContext, DbContextBase info)
        {
            if (interceptionContext.ObjectContexts == null || !interceptionContext.ObjectContexts.Any())
            {
                return;
            }

            var context = interceptionContext.ObjectContexts.First();  //todo: ??

            info.ObjectContextId = UniqueIdExtensions <ObjectContext> .GetUniqueId(context).ToInt();

            info.ObjectContextName = context.DefaultContainerName;
            if (context.TransactionHandler != null && context.TransactionHandler.Connection != null)
            {
                info.ConnectionId = UniqueIdExtensions <DbConnection> .GetUniqueId(context.TransactionHandler.Connection).ToInt();
            }
        }
        public static DbConnectionContext GetLoggedDbConnection(DbConnection connection, MutableInterceptionContext interceptionContext)
        {
            var context = new DbConnectionContext
            {
                IsAsync          = interceptionContext.IsAsync,
                IsCanceled       = interceptionContext.TaskStatus.HasFlag(TaskStatus.Canceled),
                Exception        = interceptionContext.OriginalException ?? interceptionContext.Exception,
                ConnectionString = connection.ConnectionString
            };

            setBaseInfo(interceptionContext, context);
            if (context.ConnectionId == null)
            {
                context.ConnectionId = UniqueIdExtensions <DbConnection> .GetUniqueId(connection).ToInt();
            }
            return(context);
        }
Esempio n. 10
0
        public ProfiledDbTransaction(DbTransaction transaction, ProfiledDbConnection connection, IDbProfiler profiler)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException("transaction");
            }
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }
            InnerTransaction = transaction;
            _connection      = connection;
            _profiler        = profiler;
            _connectionId    = UniqueIdExtensions <DbConnection> .GetUniqueId(connection.InnerConnection).ToInt();

            _profiler.TransactionBegan(connection.InnerConnection, NHProfilerContextProvider.GetLoggedDbConnection(this, _connectionId));
        }
        public static DbConnectionContext GetLoggedDbConnection(IDbConnection connection, BeginTransactionInterceptionContext interceptionContext)
        {
            var context = new DbConnectionContext
            {
                IsAsync          = interceptionContext.IsAsync,
                IsCanceled       = interceptionContext.TaskStatus.HasFlag(TaskStatus.Canceled),
                Exception        = interceptionContext.OriginalException ?? interceptionContext.Exception,
                ConnectionString = connection.ConnectionString,
                TransactionId    =
                    interceptionContext.Result == null
                        ? (int?)null
                        : UniqueIdExtensions <DbTransaction> .GetUniqueId(interceptionContext.Result).ToInt(),
                IsolationLevel =
                    interceptionContext.Result == null ? IsolationLevel.Unspecified : interceptionContext.Result.IsolationLevel
            };

            setBaseInfo(interceptionContext, context);
            return(context);
        }
        public static DbCommandContext GetLoggedResult(
            DbCommand command, Exception exception, object result,
            long?elapsedMilliseconds, DataTable dataTable)
        {
            var context = new DbCommandContext
            {
                IsAsync             = false,
                IsCanceled          = false,
                Exception           = exception,
                Result              = result,
                ElapsedMilliseconds = elapsedMilliseconds,
                DataTable           = dataTable,
                ObjectContextId     = SessionIdLoggingContext.SessionId.GetUniqueId(),
                ObjectContextName   = SessionId,
                ConnectionId        = UniqueIdExtensions <DbConnection> .GetUniqueId(command.Connection).ToInt()
            };

            return(context);
        }
Esempio n. 13
0
        public void LogTransaction(DbTransaction transaction,
                                   DbTransactionContext interceptionTransactionContext,
                                   CommandTransactionType type)
        {
            var commandTransaction = new CommandTransaction
            {
                IsAsync         = interceptionTransactionContext.IsAsync,
                TransactionType = type,
                IsCanceled      = interceptionTransactionContext.IsCanceled,
                Exception       = interceptionTransactionContext.Exception != null ? interceptionTransactionContext.Exception.Message : "",
                StackTrace      = new CallingMethod {
                    AssembliesToExclude = AssembliesToExclude
                }.GetCallingMethodInfo()
            };

            setBaseInfo(interceptionTransactionContext, commandTransaction);
            commandTransaction.TransactionId = UniqueIdExtensions <DbTransaction> .GetUniqueId(transaction).ToInt();

            _baseInfoQueue.Enqueue(commandTransaction);
        }
        public static DbConnectionContext GetLoggedDbConnection(ProfiledDbTransaction interceptionContext, int connectionId)
        {
            var context = new DbConnectionContext
            {
                IsAsync          = false,
                IsCanceled       = false,
                Exception        = null,
                ConnectionString = interceptionContext.Connection.ConnectionString,
                TransactionId    =
                    interceptionContext.InnerTransaction == null
                        ? (int?)null
                        : UniqueIdExtensions <DbTransaction> .GetUniqueId(interceptionContext.InnerTransaction).ToInt(),
                IsolationLevel    = interceptionContext.IsolationLevel,
                ObjectContextId   = SessionIdLoggingContext.SessionId.GetUniqueId(),
                ObjectContextName = SessionId,
                ConnectionId      = connectionId
            };

            return(context);
        }
Esempio n. 15
0
 public static int?GetHttpContextCurrentId()
 {
     return(!IsInWeb ? (int?)null : UniqueIdExtensions <HttpContext> .GetUniqueId(HttpContext.Current).ToInt());;
 }