diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientDiagnosticListenerExtensions.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientDiagnosticListenerExtensions.cs index 288edb4055..d8b8b073c3 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientDiagnosticListenerExtensions.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientDiagnosticListenerExtensions.cs @@ -38,7 +38,7 @@ internal static class SqlClientDiagnosticListenerExtensions public const string SqlAfterRollbackTransaction = SqlClientPrefix + nameof(WriteTransactionRollbackAfter); public const string SqlErrorRollbackTransaction = SqlClientPrefix + nameof(WriteTransactionRollbackError); - public static Guid WriteCommandBefore(this DiagnosticListener @this, SqlCommand sqlCommand, [CallerMemberName] string operation = "") + public static Guid WriteCommandBefore(this DiagnosticListener @this, SqlCommand sqlCommand, SqlTransaction transaction, [CallerMemberName] string operation = "") { if (@this.IsEnabled(SqlBeforeExecuteCommand)) { @@ -52,6 +52,7 @@ public static Guid WriteCommandBefore(this DiagnosticListener @this, SqlCommand Operation = operation, ConnectionId = sqlCommand.Connection?.ClientConnectionId, Command = sqlCommand, + transaction?.InternalTransaction?.TransactionId, Timestamp = Stopwatch.GetTimestamp() }); @@ -61,7 +62,7 @@ public static Guid WriteCommandBefore(this DiagnosticListener @this, SqlCommand return Guid.Empty; } - public static void WriteCommandAfter(this DiagnosticListener @this, Guid operationId, SqlCommand sqlCommand, [CallerMemberName] string operation = "") + public static void WriteCommandAfter(this DiagnosticListener @this, Guid operationId, SqlCommand sqlCommand, SqlTransaction transaction, [CallerMemberName] string operation = "") { if (@this.IsEnabled(SqlAfterExecuteCommand)) { @@ -73,13 +74,14 @@ public static void WriteCommandAfter(this DiagnosticListener @this, Guid operati Operation = operation, ConnectionId = sqlCommand.Connection?.ClientConnectionId, Command = sqlCommand, + transaction?.InternalTransaction?.TransactionId, Statistics = sqlCommand.Statistics?.GetDictionary(), Timestamp = Stopwatch.GetTimestamp() }); } } - public static void WriteCommandError(this DiagnosticListener @this, Guid operationId, SqlCommand sqlCommand, Exception ex, [CallerMemberName] string operation = "") + public static void WriteCommandError(this DiagnosticListener @this, Guid operationId, SqlCommand sqlCommand, SqlTransaction transaction, Exception ex, [CallerMemberName] string operation = "") { if (@this.IsEnabled(SqlErrorExecuteCommand)) { @@ -91,6 +93,7 @@ public static void WriteCommandError(this DiagnosticListener @this, Guid operati Operation = operation, ConnectionId = sqlCommand.Connection?.ClientConnectionId, Command = sqlCommand, + transaction?.InternalTransaction?.TransactionId, Exception = ex, Timestamp = Stopwatch.GetTimestamp() }); @@ -110,6 +113,7 @@ public static Guid WriteConnectionOpenBefore(this DiagnosticListener @this, SqlC OperationId = operationId, Operation = operation, Connection = sqlConnection, + ClientVersion = ThisAssembly.InformationalVersion, Timestamp = Stopwatch.GetTimestamp() }); @@ -131,6 +135,7 @@ public static void WriteConnectionOpenAfter(this DiagnosticListener @this, Guid Operation = operation, ConnectionId = sqlConnection.ClientConnectionId, Connection = sqlConnection, + ClientVersion = ThisAssembly.InformationalVersion, Statistics = sqlConnection.Statistics?.GetDictionary(), Timestamp = Stopwatch.GetTimestamp() }); @@ -149,6 +154,7 @@ public static void WriteConnectionOpenError(this DiagnosticListener @this, Guid Operation = operation, ConnectionId = sqlConnection.ClientConnectionId, Connection = sqlConnection, + ClientVersion = ThisAssembly.InformationalVersion, Exception = ex, Timestamp = Stopwatch.GetTimestamp() }); @@ -216,7 +222,7 @@ public static void WriteConnectionCloseError(this DiagnosticListener @this, Guid } } - public static Guid WriteTransactionCommitBefore(this DiagnosticListener @this, IsolationLevel isolationLevel, SqlConnection connection, [CallerMemberName] string operation = "") + public static Guid WriteTransactionCommitBefore(this DiagnosticListener @this, IsolationLevel isolationLevel, SqlConnection connection, SqlInternalTransaction transaction, [CallerMemberName] string operation = "") { if (@this.IsEnabled(SqlBeforeCommitTransaction)) { @@ -230,6 +236,7 @@ public static Guid WriteTransactionCommitBefore(this DiagnosticListener @this, I Operation = operation, IsolationLevel = isolationLevel, Connection = connection, + transaction?.TransactionId, Timestamp = Stopwatch.GetTimestamp() }); @@ -239,7 +246,7 @@ public static Guid WriteTransactionCommitBefore(this DiagnosticListener @this, I return Guid.Empty; } - public static void WriteTransactionCommitAfter(this DiagnosticListener @this, Guid operationId, IsolationLevel isolationLevel, SqlConnection connection, [CallerMemberName] string operation = "") + public static void WriteTransactionCommitAfter(this DiagnosticListener @this, Guid operationId, IsolationLevel isolationLevel, SqlConnection connection, SqlInternalTransaction transaction, [CallerMemberName] string operation = "") { if (@this.IsEnabled(SqlAfterCommitTransaction)) { @@ -251,12 +258,13 @@ public static void WriteTransactionCommitAfter(this DiagnosticListener @this, Gu Operation = operation, IsolationLevel = isolationLevel, Connection = connection, + transaction?.TransactionId, Timestamp = Stopwatch.GetTimestamp() }); } } - public static void WriteTransactionCommitError(this DiagnosticListener @this, Guid operationId, IsolationLevel isolationLevel, SqlConnection connection, Exception ex, [CallerMemberName] string operation = "") + public static void WriteTransactionCommitError(this DiagnosticListener @this, Guid operationId, IsolationLevel isolationLevel, SqlConnection connection, SqlInternalTransaction transaction, Exception ex, [CallerMemberName] string operation = "") { if (@this.IsEnabled(SqlErrorCommitTransaction)) { @@ -268,13 +276,14 @@ public static void WriteTransactionCommitError(this DiagnosticListener @this, Gu Operation = operation, IsolationLevel = isolationLevel, Connection = connection, + transaction?.TransactionId, Exception = ex, Timestamp = Stopwatch.GetTimestamp() }); } } - public static Guid WriteTransactionRollbackBefore(this DiagnosticListener @this, IsolationLevel isolationLevel, SqlConnection connection, string transactionName, [CallerMemberName] string operation = "") + public static Guid WriteTransactionRollbackBefore(this DiagnosticListener @this, IsolationLevel isolationLevel, SqlConnection connection, SqlInternalTransaction transaction, string transactionName = null, [CallerMemberName] string operation = "") { if (@this.IsEnabled(SqlBeforeRollbackTransaction)) { @@ -288,6 +297,7 @@ public static Guid WriteTransactionRollbackBefore(this DiagnosticListener @this, Operation = operation, IsolationLevel = isolationLevel, Connection = connection, + transaction?.TransactionId, TransactionName = transactionName, Timestamp = Stopwatch.GetTimestamp() }); @@ -298,7 +308,7 @@ public static Guid WriteTransactionRollbackBefore(this DiagnosticListener @this, return Guid.Empty; } - public static void WriteTransactionRollbackAfter(this DiagnosticListener @this, Guid operationId, IsolationLevel isolationLevel, SqlConnection connection, string transactionName, [CallerMemberName] string operation = "") + public static void WriteTransactionRollbackAfter(this DiagnosticListener @this, Guid operationId, IsolationLevel isolationLevel, SqlConnection connection, SqlInternalTransaction transaction, string transactionName = null, [CallerMemberName] string operation = "") { if (@this.IsEnabled(SqlAfterRollbackTransaction)) { @@ -310,13 +320,14 @@ public static void WriteTransactionRollbackAfter(this DiagnosticListener @this, Operation = operation, IsolationLevel = isolationLevel, Connection = connection, + transaction?.TransactionId, TransactionName = transactionName, Timestamp = Stopwatch.GetTimestamp() }); } } - public static void WriteTransactionRollbackError(this DiagnosticListener @this, Guid operationId, IsolationLevel isolationLevel, SqlConnection connection, string transactionName, Exception ex, [CallerMemberName] string operation = "") + public static void WriteTransactionRollbackError(this DiagnosticListener @this, Guid operationId, IsolationLevel isolationLevel, SqlConnection connection, SqlInternalTransaction transaction, Exception ex, string transactionName = null, [CallerMemberName] string operation = "") { if (@this.IsEnabled(SqlErrorRollbackTransaction)) { @@ -328,6 +339,7 @@ public static void WriteTransactionRollbackError(this DiagnosticListener @this, Operation = operation, IsolationLevel = isolationLevel, Connection = connection, + transaction?.TransactionId, TransactionName = transactionName, Exception = ex, Timestamp = Stopwatch.GetTimestamp() diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs index 2106de5490..d4d336ca77 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs @@ -943,7 +943,7 @@ override public object ExecuteScalar() // between entry into Execute* API and the thread obtaining the stateObject. _pendingCancel = false; - Guid operationId = _diagnosticListener.WriteCommandBefore(this); + Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction); SqlStatistics statistics = null; @@ -981,11 +981,11 @@ override public object ExecuteScalar() WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous: true); if (e != null) { - _diagnosticListener.WriteCommandError(operationId, this, e); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, e); } else { - _diagnosticListener.WriteCommandAfter(operationId, this); + _diagnosticListener.WriteCommandAfter(operationId, this, _transaction); } } } @@ -1027,7 +1027,7 @@ override public int ExecuteNonQuery() // between entry into Execute* API and the thread obtaining the stateObject. _pendingCancel = false; - Guid operationId = _diagnosticListener.WriteCommandBefore(this); + Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction); SqlStatistics statistics = null; @@ -1051,11 +1051,11 @@ override public int ExecuteNonQuery() SqlClientEventSource.Log.ScopeLeaveEvent(scopeID); if (e != null) { - _diagnosticListener.WriteCommandError(operationId, this, e); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, e); } else { - _diagnosticListener.WriteCommandAfter(operationId, this); + _diagnosticListener.WriteCommandAfter(operationId, this, _transaction); } } } @@ -1517,7 +1517,7 @@ public XmlReader ExecuteXmlReader() _pendingCancel = false; bool success = false; - Guid operationId = _diagnosticListener.WriteCommandBefore(this); + Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction); SqlStatistics statistics = null; long scopeID = SqlClientEventSource.Log.ScopeEnterEvent(" {0}", ObjectID); @@ -1553,11 +1553,11 @@ public XmlReader ExecuteXmlReader() WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous: true); if (e != null) { - _diagnosticListener.WriteCommandError(operationId, this, e); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, e); } else { - _diagnosticListener.WriteCommandAfter(operationId, this); + _diagnosticListener.WriteCommandAfter(operationId, this, _transaction); } } } @@ -1844,7 +1844,7 @@ override protected DbDataReader ExecuteDbDataReader(CommandBehavior behavior) // between entry into Execute* API and the thread obtaining the stateObject. _pendingCancel = false; - Guid operationId = _diagnosticListener.WriteCommandBefore(this); + Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction); SqlStatistics statistics = null; bool success = false; @@ -1873,11 +1873,11 @@ override protected DbDataReader ExecuteDbDataReader(CommandBehavior behavior) if (e != null) { - _diagnosticListener.WriteCommandError(operationId, this, e); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, e); } else { - _diagnosticListener.WriteCommandAfter(operationId, this); + _diagnosticListener.WriteCommandAfter(operationId, this, _transaction); } } } @@ -2248,7 +2248,7 @@ private SqlDataReader InternalEndExecuteReader(IAsyncResult asyncResult, bool is public override Task ExecuteNonQueryAsync(CancellationToken cancellationToken) { SqlClientEventSource.Log.CorrelationTraceEvent(" ObjectID {0}, ActivityID {1}", ObjectID, ActivityCorrelator.Current); - Guid operationId = _diagnosticListener.WriteCommandBefore(this); + Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction); TaskCompletionSource source = new TaskCompletionSource(); @@ -2274,7 +2274,7 @@ public override Task ExecuteNonQueryAsync(CancellationToken cancellationTok if (t.IsFaulted) { Exception e = t.Exception.InnerException; - _diagnosticListener.WriteCommandError(operationId, this, e); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, e); source.SetException(e); } else @@ -2287,13 +2287,13 @@ public override Task ExecuteNonQueryAsync(CancellationToken cancellationTok { source.SetResult(t.Result); } - _diagnosticListener.WriteCommandAfter(operationId, this); + _diagnosticListener.WriteCommandAfter(operationId, this, _transaction); } }, TaskScheduler.Default); } catch (Exception e) { - _diagnosticListener.WriteCommandError(operationId, this, e); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, e); source.SetException(e); } @@ -2337,7 +2337,7 @@ protected override Task ExecuteDbDataReaderAsync(CommandBehavior b SqlClientEventSource.Log.CorrelationTraceEvent(" ObjectID {0}, behavior={1}, ActivityID {2}", ObjectID, (int)behavior, ActivityCorrelator.Current); Guid operationId = default(Guid); if (!_parentOperationStarted) - operationId = _diagnosticListener.WriteCommandBefore(this); + operationId = _diagnosticListener.WriteCommandBefore(this, _transaction); TaskCompletionSource source = new TaskCompletionSource(); @@ -2364,7 +2364,7 @@ protected override Task ExecuteDbDataReaderAsync(CommandBehavior b { Exception e = t.Exception.InnerException; if (!_parentOperationStarted) - _diagnosticListener.WriteCommandError(operationId, this, e); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, e); source.SetException(e); } else @@ -2378,14 +2378,14 @@ protected override Task ExecuteDbDataReaderAsync(CommandBehavior b source.SetResult(t.Result); } if (!_parentOperationStarted) - _diagnosticListener.WriteCommandAfter(operationId, this); + _diagnosticListener.WriteCommandAfter(operationId, this, _transaction); } }, TaskScheduler.Default); } catch (Exception e) { if (!_parentOperationStarted) - _diagnosticListener.WriteCommandError(operationId, this, e); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, e); source.SetException(e); } @@ -2397,7 +2397,7 @@ protected override Task ExecuteDbDataReaderAsync(CommandBehavior b public override Task ExecuteScalarAsync(CancellationToken cancellationToken) { _parentOperationStarted = true; - Guid operationId = _diagnosticListener.WriteCommandBefore(this); + Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction); return ExecuteReaderAsync(cancellationToken).ContinueWith((executeTask) => { @@ -2408,7 +2408,7 @@ public override Task ExecuteScalarAsync(CancellationToken cancellationTo } else if (executeTask.IsFaulted) { - _diagnosticListener.WriteCommandError(operationId, this, executeTask.Exception.InnerException); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, executeTask.Exception.InnerException); source.SetException(executeTask.Exception.InnerException); } else @@ -2426,7 +2426,7 @@ public override Task ExecuteScalarAsync(CancellationToken cancellationTo else if (readTask.IsFaulted) { reader.Dispose(); - _diagnosticListener.WriteCommandError(operationId, this, readTask.Exception.InnerException); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, readTask.Exception.InnerException); source.SetException(readTask.Exception.InnerException); } else @@ -2454,12 +2454,12 @@ public override Task ExecuteScalarAsync(CancellationToken cancellationTo } if (exception != null) { - _diagnosticListener.WriteCommandError(operationId, this, exception); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, exception); source.SetException(exception); } else { - _diagnosticListener.WriteCommandAfter(operationId, this); + _diagnosticListener.WriteCommandAfter(operationId, this, _transaction); source.SetResult(result); } } @@ -2486,7 +2486,7 @@ public Task ExecuteXmlReaderAsync() public Task ExecuteXmlReaderAsync(CancellationToken cancellationToken) { SqlClientEventSource.Log.CorrelationTraceEvent(" ObjectID {0}, ActivityID {1}", ObjectID, ActivityCorrelator.Current); - Guid operationId = _diagnosticListener.WriteCommandBefore(this); + Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction); TaskCompletionSource source = new TaskCompletionSource(); @@ -2512,7 +2512,7 @@ public Task ExecuteXmlReaderAsync(CancellationToken cancellationToken if (t.IsFaulted) { Exception e = t.Exception.InnerException; - _diagnosticListener.WriteCommandError(operationId, this, e); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, e); source.SetException(e); } else @@ -2525,13 +2525,13 @@ public Task ExecuteXmlReaderAsync(CancellationToken cancellationToken { source.SetResult(t.Result); } - _diagnosticListener.WriteCommandAfter(operationId, this); + _diagnosticListener.WriteCommandAfter(operationId, this, _transaction); } }, TaskScheduler.Default); } catch (Exception e) { - _diagnosticListener.WriteCommandError(operationId, this, e); + _diagnosticListener.WriteCommandError(operationId, this, _transaction, e); source.SetException(e); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlTransaction.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlTransaction.cs index 0ffb0db1f8..c84dee123b 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlTransaction.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlTransaction.cs @@ -137,7 +137,7 @@ internal SqlStatistics Statistics override public void Commit() { Exception e = null; - Guid operationId = s_diagnosticListener.WriteTransactionCommitBefore(_isolationLevel, _connection); + Guid operationId = s_diagnosticListener.WriteTransactionCommitBefore(_isolationLevel, _connection, InternalTransaction); ZombieCheck(); @@ -176,11 +176,11 @@ override public void Commit() SqlClientEventSource.Log.ScopeLeaveEvent(scopeID); if (e != null) { - s_diagnosticListener.WriteTransactionCommitError(operationId, _isolationLevel, _connection, e); + s_diagnosticListener.WriteTransactionCommitError(operationId, _isolationLevel, _connection, InternalTransaction, e); } else { - s_diagnosticListener.WriteTransactionCommitAfter(operationId, _isolationLevel, _connection); + s_diagnosticListener.WriteTransactionCommitAfter(operationId, _isolationLevel, _connection, InternalTransaction); } _isFromAPI = false; @@ -204,7 +204,7 @@ protected override void Dispose(bool disposing) override public void Rollback() { Exception e = null; - Guid operationId = s_diagnosticListener.WriteTransactionRollbackBefore(_isolationLevel, _connection, null); + Guid operationId = s_diagnosticListener.WriteTransactionRollbackBefore(_isolationLevel, _connection, InternalTransaction); if (IsYukonPartialZombie) { @@ -238,11 +238,11 @@ override public void Rollback() SqlClientEventSource.Log.ScopeLeaveEvent(scopeID); if (e != null) { - s_diagnosticListener.WriteTransactionRollbackError(operationId, _isolationLevel, _connection, null, e); + s_diagnosticListener.WriteTransactionRollbackError(operationId, _isolationLevel, _connection, InternalTransaction, e); } else { - s_diagnosticListener.WriteTransactionRollbackAfter(operationId, _isolationLevel, _connection, null); + s_diagnosticListener.WriteTransactionRollbackAfter(operationId, _isolationLevel, _connection, InternalTransaction); } _isFromAPI = false; } @@ -253,7 +253,7 @@ override public void Rollback() public void Rollback(string transactionName) { Exception e = null; - Guid operationId = s_diagnosticListener.WriteTransactionRollbackBefore(_isolationLevel, _connection, transactionName); + Guid operationId = s_diagnosticListener.WriteTransactionRollbackBefore(_isolationLevel, _connection, InternalTransaction, transactionName); ZombieCheck(); long scopeID = SqlClientEventSource.Log.ScopeEnterEvent(" {0} transactionName='{1}'", ObjectID, transactionName); @@ -277,11 +277,11 @@ public void Rollback(string transactionName) SqlClientEventSource.Log.ScopeLeaveEvent(scopeID); if (e != null) { - s_diagnosticListener.WriteTransactionRollbackError(operationId, _isolationLevel, _connection, transactionName, e); + s_diagnosticListener.WriteTransactionRollbackError(operationId, _isolationLevel, _connection, InternalTransaction, e, transactionName); } else { - s_diagnosticListener.WriteTransactionRollbackAfter(operationId, _isolationLevel, _connection, transactionName); + s_diagnosticListener.WriteTransactionRollbackAfter(operationId, _isolationLevel, _connection, InternalTransaction, transactionName); } _isFromAPI = false;