diff --git a/doc/snippets/Microsoft.Data.SqlClient/SqlClientDiagnostic.xml b/doc/snippets/Microsoft.Data.SqlClient/SqlClientDiagnostic.xml new file mode 100644 index 0000000000..3711bbb7f7 --- /dev/null +++ b/doc/snippets/Microsoft.Data.SqlClient/SqlClientDiagnostic.xml @@ -0,0 +1,312 @@ + + + + + A guid value used to correlate before, after and error events. + + + The name of the operation. + + + The timestamp of the event. + + + + Gets the element at the specified index in the read-only list. + + The zero-based index of the element to get. + The element at the specified index in the read-only list. + + + + Gets the number of elements in the collection. + The number of elements in the collection. + + + Returns an enumerator that iterates through the collection. + An enumerator that can be used to iterate through the collection. + + + + + Contains diagnostic information emitted before a command is executed. + + + The name of the event that needs to be enabled for the event to be raised. + + + A nullable guid uniquely identifying the connection that the xommand is being executed on. + + + A nullable long uniquely identifying the transaction that the command enrolled in if it is enrolled in one. + + + The command object that is executing. + + + + + Contains diagnostic information emitted after a command is successfully executed. + + + The name of the event that needs to be enabled for the event to be raised. + + + A nullable guid uniquely identifying the connection that the command is being executed on. + + + A nullable long uniquely identifying the transaction that the command is enrolled in if it is enrolled in one, or null. + + + The command object that is executing. + + + An IDictionary of statistic information about the event that has completed. + + + + + Contains diagnostic information emitted after a command execution fails with an exception. + + + The name of the event that needs to be enabled for the event to be raised. + + + A nullable guid uniquely identifying the connection that the command is being executed on. + + + A nullable long uniquely identifying the transaction that the command is enrolled in if it is enrolled in one, or null. + + + The command object that is executing. + + + The exception object that caused the command execution to fail. + + + + + Contains diagnostic information emitted before a connection is opened. + + + The name of the event that needs to be enabled for the event to be raised. + + + The connection object that is being opened. + + + The version of the SqlClient library. + + + + + Contains diagnostic information emitted after a connection has been successfully opened. + + + The name of the event that needs to be enabled for the event to be raised. + + + The connection object that has been opened. + + + The version of the SqlClient library. + + + The unique guid assigned to the connection. + + + An IDictionary of statistic information about the event that has completed. + + + + + Contains diagnostic information emitted after a connection open fails with an exception. + + + The name of the event that needs to be enabled for the event to be raised. + + + The connection object that has been opened. + + + The version of the SqlClient library. + + + The unique guid assigned to the connection. + + + The exception object that caused the command execution to fail. + + + + + Contains diagnostic information emitted before a connection is closed. + + + The name of the event that needs to be enabled for the event to be raised. + + + The connection object that is being closed. + + + The unique guid assigned to the connection. + + + An IDictionary of statistic information about the connection. + + + + + Contains diagnostic information emitted after a connection has been successfully closed. + + + The name of the event that needs to be enabled for the event to be raised. + + + The connection object that has been closed. + + + The unique guid assigned to the connection. + + + An IDictionary of statistic information about the connection. + + + + + Contains diagnostic information emitted after a connection close fails with an exception. + + + The name of the event that needs to be enabled for the event to be raised. + + + The connection object that has been closed. + + + The unique guid assigned to the connection. + + + An IDictionary of statistic information about the connection. + + + The exception object that caused the command execution to fail. + + + + + Contains diagnostic information emitted before a transaction is opened. + + + The name of the event that needs to be enabled for the event to be raised. + + + The connection object that the transaction belongs to. + + + The IsolationLevel of the transaction. + + + A nullable long uniquely identifying the transaction that the command is enrolled in if it is enrolled in one, or null. + + + + + Contains diagnostic information emitted after a transaction is successfully committed. + + + The name of the event that needs to be enabled for the event to be raised. + + + The connection object that the transaction belongs to. + + + The IsolationLevel of the transaction. + + + A nullable long uniquely identifying the transaction that the command is enrolled in if it is enrolled in one, or null. + + + + + Contains diagnostic information emitted after a transaction commit fails with an exception. + + + The name of the event that needs to be enabled for the event to be raised. + + + The connection object that the transaction belongs to. + + + The IsolationLevel of the transaction. + + + A nullable long uniquely identifying the transaction that the command is enrolled in if it is enrolled in one, or null. + + + The exception object that caused the command execution to fail. + + + + + Contains diagnostic information emitted before a transaction rollback is rolled back. + + + The name of the event that needs to be enabled for the event to be raised. + + + The connection object that the transaction belongs to. + + + The IsolationLevel of the transaction. + + + A nullable long uniquely identifying the transaction that the command is enrolled in if it is enrolled in one, or null. + + + The name of the transaction which is being rolled back. + + + + + Contains diagnostic information emitted after a transaction is rolled back successfully. + + + The name of the event that needs to be enabled for the event to be raised. + + + The connection object that the transaction belongs to. + + + The IsolationLevel of the transaction. + + + A nullable long uniquely identifying the transaction, or null. + + + The name of the transaction which is being rolled back. + + + + + Contains diagnostic information emitted after a transaction roll back failes with an exception. + + + The name of the event that needs to be enabled for the event to be raised. + + + The connection object that the transaction belongs to. + + + The IsolationLevel of the transaction. + + + A nullable long uniquely identifying the transaction , or null. + + + The name of the transaction which is being rolled back. + + + The exception object that caused the command execution to fail. + + + diff --git a/src/Microsoft.Data.SqlClient.sln b/src/Microsoft.Data.SqlClient.sln index 04668c8f81..9ab18e4989 100644 --- a/src/Microsoft.Data.SqlClient.sln +++ b/src/Microsoft.Data.SqlClient.sln @@ -100,6 +100,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.Data.SqlClient", ..\doc\snippets\Microsoft.Data.SqlClient\SqlBulkCopyColumnMapping.xml = ..\doc\snippets\Microsoft.Data.SqlClient\SqlBulkCopyColumnMapping.xml ..\doc\snippets\Microsoft.Data.SqlClient\SqlBulkCopyColumnMappingCollection.xml = ..\doc\snippets\Microsoft.Data.SqlClient\SqlBulkCopyColumnMappingCollection.xml ..\doc\snippets\Microsoft.Data.SqlClient\SqlBulkCopyOptions.xml = ..\doc\snippets\Microsoft.Data.SqlClient\SqlBulkCopyOptions.xml + ..\doc\snippets\Microsoft.Data.SqlClient\SqlClientDiagnostic.xml = ..\doc\snippets\Microsoft.Data.SqlClient\SqlClientDiagnostic.xml ..\doc\snippets\Microsoft.Data.SqlClient\SqlClientFactory.xml = ..\doc\snippets\Microsoft.Data.SqlClient\SqlClientFactory.xml ..\doc\snippets\Microsoft.Data.SqlClient\SqlClientLogger.xml = ..\doc\snippets\Microsoft.Data.SqlClient\SqlClientLogger.xml ..\doc\snippets\Microsoft.Data.SqlClient\SqlClientMetaDataCollectionNames.xml = ..\doc\snippets\Microsoft.Data.SqlClient\SqlClientMetaDataCollectionNames.xml diff --git a/src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs b/src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs index 72203bb433..5dfbf8af3f 100644 --- a/src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs +++ b/src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs @@ -5,6 +5,8 @@ // NOTE: The current Microsoft.VSDesigner editor attributes are implemented for System.Data.SqlClient, and are not publicly available. // New attributes that are designed to work with Microsoft.Data.SqlClient and are publicly documented should be included in future. +using System; + [assembly: System.CLSCompliant(true)] namespace Microsoft.Data { @@ -1908,6 +1910,316 @@ public sealed class SqlConfigurableRetryFactory public static SqlRetryLogicBaseProvider CreateNoneRetryProvider() { throw null; } } } +namespace Microsoft.Data.SqlClient.Diagnostics +{ + /// + public abstract class SqlClientDiagnostic : System.Collections.Generic.IReadOnlyList> + { + internal SqlClientDiagnostic() { } + /// + protected const int CommonPropertyCount = 3; + /// + protected SqlClientDiagnostic(System.Guid operationId, string operation, long timestamp) { } + /// + public System.Guid OperationId => throw null; + /// + public string Operation => throw null; + /// + public long Timestamp => throw null; + /// > + public int Count => CommonPropertyCount + GetDerivedCount(); + /// > + public System.Collections.Generic.KeyValuePair this[int index] => throw null; + /// > + public System.Collections.Generic.IEnumerator> GetEnumerator() => throw null; + /// > + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + /// + protected bool TryGetCommonProperty(int index, out System.Collections.Generic.KeyValuePair property) => throw null; + /// + protected abstract int GetDerivedCount(); + /// + protected abstract System.Collections.Generic.KeyValuePair GetDerivedProperty(int index); + } + + /// + public sealed class SqlClientCommandBefore : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteCommandBefore"; + /// + public System.Guid? ConnectionId => throw null; + /// + public long? TransactionId => throw null; + /// + public SqlCommand Command => throw null; + /// > + protected sealed override int GetDerivedCount() => 3; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientCommandAfter : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteCommandAfter"; + /// + public System.Guid? ConnectionId => throw null; + /// + public long? TransactionId => throw null; + /// + public SqlCommand Command => throw null; + /// + public System.Collections.IDictionary Statistics => throw null; + /// > + protected sealed override int GetDerivedCount() => 4; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientCommandError : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteCommandError"; + /// + public System.Guid? ConnectionId => throw null; + /// + public long? TransactionId => throw null; + /// + public SqlCommand Command => throw null; + /// + public System.Exception Exception { get; } + /// > + protected sealed override int GetDerivedCount() => 4; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientConnectionOpenBefore : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteConnectionOpenBefore"; + /// + public SqlConnection Connection => throw null; + /// + public string ClientVersion => throw null; + /// > + protected override int GetDerivedCount() => 2; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientConnectionOpenAfter : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteConnectionOpenAfter"; + /// + public System.Guid ConnectionId => throw null; + /// + public SqlConnection Connection => throw null; + /// + public string ClientVersion => throw null; + /// + public System.Collections.IDictionary Statistics => throw null; + /// > + protected override int GetDerivedCount() => 4; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientConnectionOpenError : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteConnectionOpenError"; + /// + public System.Guid ConnectionId => throw null; + /// + public SqlConnection Connection => throw null; + /// + public string ClientVersion => throw null; + /// + public System.Exception Exception => throw null; + /// > + protected override int GetDerivedCount() => 4; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientConnectionCloseBefore : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteConnectionCloseBefore"; + /// + public System.Guid? ConnectionId => throw null; + /// + public SqlConnection Connection => throw null; + /// + public System.Collections.IDictionary Statistics => throw null; + /// > + protected sealed override int GetDerivedCount() => 3; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientConnectionCloseAfter : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteConnectionCloseAfter"; + + /// + public System.Guid? ConnectionId => throw null; + + /// + public SqlConnection Connection => throw null; + + /// + public System.Collections.IDictionary Statistics => throw null; + + /// > + protected sealed override int GetDerivedCount() => 3; + + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientConnectionCloseError : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteConnectionCloseError"; + /// + public System.Guid? ConnectionId => throw null; + /// + public SqlConnection Connection => throw null; + /// + public System.Collections.IDictionary Statistics => throw null; + /// + public System.Exception Exception => throw null; + /// > + protected sealed override int GetDerivedCount() => 4; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientTransactionCommitBefore : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteTransactionCommitBefore"; + /// + public System.Data.IsolationLevel IsolationLevel => throw null; + /// + public SqlConnection Connection => throw null; + /// + public long? TransactionId => throw null; + /// > + protected sealed override int GetDerivedCount() => 3; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientTransactionCommitAfter : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteTransactionCommitAfter"; + /// + public System.Data.IsolationLevel IsolationLevel => throw null; + /// + public SqlConnection Connection => throw null; + /// + public long? TransactionId => throw null; + /// > + protected sealed override int GetDerivedCount() => 3; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientTransactionCommitError : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteTransactionCommitError"; + /// + public System.Data.IsolationLevel IsolationLevel => throw null; + /// + public SqlConnection Connection => throw null; + /// + public long? TransactionId => throw null; + /// + public System.Exception Exception => throw null; + /// > + protected sealed override int GetDerivedCount() => 4; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientTransactionRollbackBefore : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteTransactionRollbackBefore"; + /// + public System.Data.IsolationLevel IsolationLevel => throw null; + /// + public SqlConnection Connection => throw null; + /// + public long? TransactionId => throw null; + /// + public string TransactionName => throw null; + /// > + protected sealed override int GetDerivedCount() => 4; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientTransactionRollbackAfter : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteTransactionRollbackAfter"; + /// + public System.Data.IsolationLevel IsolationLevel => throw null; + /// + public SqlConnection Connection => throw null; + /// + public long? TransactionId => throw null; + /// + public string TransactionName => throw null; + /// > + protected sealed override int GetDerivedCount() => 4; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } + + /// + public sealed class SqlClientTransactionRollbackError : SqlClientDiagnostic + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteTransactionRollbackError"; + /// + public System.Data.IsolationLevel IsolationLevel => throw null; + /// + public SqlConnection Connection => throw null; + /// + public long? TransactionId => throw null; + /// + public string TransactionName => throw null; + /// + public System.Exception Exception => throw null; + /// > + protected sealed override int GetDerivedCount() => 5; + /// > + protected sealed override System.Collections.Generic.KeyValuePair GetDerivedProperty(int index) => throw null; + } +} namespace Microsoft.Data.SqlClient.Server { /// diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index 7cf0b1e619..46a77d5373 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -647,6 +647,7 @@ + diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientDiagnostic.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientDiagnostic.cs new file mode 100644 index 0000000000..51a2f8f0ba --- /dev/null +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientDiagnostic.cs @@ -0,0 +1,970 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Data; + +namespace Microsoft.Data.SqlClient.Diagnostics +{ + /// + public sealed class SqlClientCommandBefore : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteCommandBefore"; + + internal SqlClientCommandBefore(Guid operationId, string operation, long timestamp, Guid? connectionId, long? transactionId, SqlCommand command) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + ConnectionId = connectionId; + TransactionId = transactionId; + Command = command; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + /// + public Guid? ConnectionId { get; } + /// + public long? TransactionId { get; } + /// + public SqlCommand Command { get; } + + /// + public int Count => 3 + 3; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(ConnectionId), ConnectionId), + 4 => new KeyValuePair(nameof(TransactionId), TransactionId), + 5 => new KeyValuePair(nameof(Command), Command), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientCommandAfter : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteCommandAfter"; + + internal SqlClientCommandAfter(Guid operationId, string operation, long timestamp, Guid? connectionId, long? transactionId, SqlCommand command, IDictionary statistics) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + ConnectionId = connectionId; + TransactionId = transactionId; + Command = command; + Statistics = statistics; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + /// + public Guid? ConnectionId { get; } + /// + public long? TransactionId { get; } + /// + public SqlCommand Command { get; } + /// + public IDictionary Statistics { get; } + + /// + public int Count => 3 + 4; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(ConnectionId), ConnectionId), + 4 => new KeyValuePair(nameof(TransactionId), TransactionId), + 5 => new KeyValuePair(nameof(Command), Command), + 6 => new KeyValuePair(nameof(Statistics), Statistics), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientCommandError : IReadOnlyList> + { + /// + + public const string Name = "Microsoft.Data.SqlClient.WriteCommandError"; + + internal SqlClientCommandError(Guid operationId, string operation, long timestamp, Guid? connectionId, long? transactionId, SqlCommand command, Exception exception) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + ConnectionId = connectionId; + TransactionId = transactionId; + Command = command; + Exception = exception; + } + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + /// + public Guid? ConnectionId { get; } + /// + public long? TransactionId { get; } + /// + public SqlCommand Command { get; } + /// + public Exception Exception { get; } + + /// + public int Count => 3 + 4; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(ConnectionId), ConnectionId), + 4 => new KeyValuePair(nameof(TransactionId), TransactionId), + 5 => new KeyValuePair(nameof(Command), Command), + 6 => new KeyValuePair(nameof(Exception), Exception), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientConnectionOpenBefore : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteConnectionOpenBefore"; + + internal SqlClientConnectionOpenBefore(Guid operationId, string operation, long timestamp, SqlConnection connection, string clientVersion) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + Connection = connection; + ClientVersion = clientVersion; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + /// + public SqlConnection Connection { get; } + /// + public string ClientVersion { get; } + + /// + public int Count => 3 + 2; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(Connection), Connection), + 4 => new KeyValuePair(nameof(ClientVersion), ClientVersion), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientConnectionOpenAfter : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteConnectionOpenAfter"; + + internal SqlClientConnectionOpenAfter(Guid operationId, string operation, long timestamp, Guid connectionId, SqlConnection connection, string clientVersion, IDictionary statistics) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + ConnectionId = connectionId; + Connection = connection; + ClientVersion = clientVersion; + Statistics = statistics; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + /// + public Guid ConnectionId { get; } + /// + public SqlConnection Connection { get; } + /// + public string ClientVersion { get; } + /// + public IDictionary Statistics { get; } + + /// + public int Count => 3 + 4; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(ConnectionId), ConnectionId), + 4 => new KeyValuePair(nameof(Connection), Connection), + 5 => new KeyValuePair(nameof(ClientVersion), ClientVersion), + 6 => new KeyValuePair(nameof(Statistics), Statistics), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientConnectionOpenError : IReadOnlyList> + { + /// + + public const string Name = "Microsoft.Data.SqlClient.WriteConnectionOpenError"; + + internal SqlClientConnectionOpenError(Guid operationId, string operation, long timestamp, Guid connectionId, SqlConnection connection, string clientVersion, Exception exception) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + ConnectionId = connectionId; + Connection = connection; + ClientVersion = clientVersion; + Exception = exception; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + + /// + public Guid ConnectionId { get; } + /// + public SqlConnection Connection { get; } + /// + public string ClientVersion { get; } + /// + public Exception Exception { get; } + + /// + public int Count => 3 + 4; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(ConnectionId), ConnectionId), + 4 => new KeyValuePair(nameof(Connection), Connection), + 5 => new KeyValuePair(nameof(ClientVersion), ClientVersion), + 6 => new KeyValuePair(nameof(Exception), Exception), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientConnectionCloseBefore : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteConnectionCloseBefore"; + + internal SqlClientConnectionCloseBefore(Guid operationId, string operation, long timestamp, Guid? connectionId, SqlConnection connection, IDictionary statistics) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + ConnectionId = connectionId; + Connection = connection; + Statistics = statistics; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + /// + public Guid? ConnectionId { get; } + /// + public SqlConnection Connection { get; } + /// + public IDictionary Statistics { get; } + + /// + public int Count => 3 + 3; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(ConnectionId), ConnectionId), + 4 => new KeyValuePair(nameof(Connection), Connection), + 5 => new KeyValuePair(nameof(Statistics), Statistics), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientConnectionCloseAfter : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteConnectionCloseAfter"; + + internal SqlClientConnectionCloseAfter(Guid operationId, string operation, long timestamp, Guid? connectionId, SqlConnection connection, IDictionary statistics) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + ConnectionId = connectionId; + Connection = connection; + Statistics = statistics; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + /// + public Guid? ConnectionId { get; } + /// + public SqlConnection Connection { get; } + /// + public IDictionary Statistics { get; } + + /// + public int Count => 3 + 3; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(ConnectionId), ConnectionId), + 4 => new KeyValuePair(nameof(Connection), Connection), + 5 => new KeyValuePair(nameof(Statistics), Statistics), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientConnectionCloseError : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteConnectionCloseError"; + + internal SqlClientConnectionCloseError(Guid operationId, string operation, long timestamp, Guid? connectionId, SqlConnection connection, IDictionary statistics, Exception ex) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + ConnectionId = connectionId; + Connection = connection; + Statistics = statistics; + Exception = ex; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + + /// + public Guid? ConnectionId { get; } + /// + public SqlConnection Connection { get; } + /// + public IDictionary Statistics { get; } + + /// + public Exception Exception { get; } + + /// + public int Count => 3 + 4; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(ConnectionId), ConnectionId), + 4 => new KeyValuePair(nameof(Connection), Connection), + 5 => new KeyValuePair(nameof(Statistics), Statistics), + 6 => new KeyValuePair(nameof(Exception), Exception), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientTransactionCommitBefore : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteTransactionCommitBefore"; + + internal SqlClientTransactionCommitBefore(Guid operationId, string operation, long timestamp, IsolationLevel isolationLevel, SqlConnection connection, long? transactionId) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + IsolationLevel = isolationLevel; + Connection = connection; + TransactionId = transactionId; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + /// + public IsolationLevel IsolationLevel { get; } + /// + public SqlConnection Connection { get; } + /// + public long? TransactionId { get; } + + /// + public int Count => 3 + 3; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(IsolationLevel), IsolationLevel), + 4 => new KeyValuePair(nameof(Connection), Connection), + 5 => new KeyValuePair(nameof(TransactionId), TransactionId), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientTransactionCommitAfter : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteTransactionCommitAfter"; + + internal SqlClientTransactionCommitAfter(Guid operationId, string operation, long timestamp, IsolationLevel isolationLevel, SqlConnection connection, long? transactionId) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + IsolationLevel = isolationLevel; + Connection = connection; + TransactionId = transactionId; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + + /// + public IsolationLevel IsolationLevel { get; } + /// + public SqlConnection Connection { get; } + /// + public long? TransactionId { get; } + + /// + public int Count => 3 + 3; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(IsolationLevel), IsolationLevel), + 4 => new KeyValuePair(nameof(Connection), Connection), + 5 => new KeyValuePair(nameof(TransactionId), TransactionId), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientTransactionCommitError : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteTransactionCommitError"; + + internal SqlClientTransactionCommitError(Guid operationId, string operation, long timestamp, IsolationLevel isolationLevel, SqlConnection connection, long? transactionId, Exception ex) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + IsolationLevel = isolationLevel; + Connection = connection; + TransactionId = transactionId; + Exception = ex; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + + /// + public IsolationLevel IsolationLevel { get; } + /// + public SqlConnection Connection { get; } + /// + public long? TransactionId { get; } + /// + public Exception Exception { get; } + + /// + public int Count => 3 + 4; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(IsolationLevel), IsolationLevel), + 4 => new KeyValuePair(nameof(Connection), Connection), + 5 => new KeyValuePair(nameof(TransactionId), TransactionId), + 6 => new KeyValuePair(nameof(Exception), Exception), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientTransactionRollbackBefore : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteTransactionRollbackBefore"; + + internal SqlClientTransactionRollbackBefore(Guid operationId, string operation, long timestamp, IsolationLevel isolationLevel, SqlConnection connection, long? transactionId, string transactionName) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + IsolationLevel = isolationLevel; + Connection = connection; + TransactionId = transactionId; + TransactionName = transactionName; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + + /// + public IsolationLevel IsolationLevel { get; } + /// + public SqlConnection Connection { get; } + /// + public long? TransactionId { get; } + /// + public string TransactionName { get; } + + /// + public int Count => 3 + 4; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(IsolationLevel), IsolationLevel), + 4 => new KeyValuePair(nameof(Connection), Connection), + 5 => new KeyValuePair(nameof(TransactionId), TransactionId), + 6 => new KeyValuePair(nameof(TransactionName), TransactionName), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientTransactionRollbackAfter : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteTransactionRollbackAfter"; + + internal SqlClientTransactionRollbackAfter(Guid operationId, string operation, long timestamp, IsolationLevel isolationLevel, SqlConnection connection, long? transactionId, string transactionName) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + IsolationLevel = isolationLevel; + Connection = connection; + TransactionId = transactionId; + TransactionName = transactionName; + } + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + /// + public IsolationLevel IsolationLevel { get; } + /// + public SqlConnection Connection { get; } + /// + public long? TransactionId { get; } + /// + public string TransactionName { get; } + + /// + public int Count => 3 + 4; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(IsolationLevel), IsolationLevel), + 4 => new KeyValuePair(nameof(Connection), Connection), + 5 => new KeyValuePair(nameof(TransactionId), TransactionId), + 6 => new KeyValuePair(nameof(TransactionName), TransactionName), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } + + /// + public sealed class SqlClientTransactionRollbackError : IReadOnlyList> + { + /// + public const string Name = "Microsoft.Data.SqlClient.WriteTransactionRollbackError"; + + internal SqlClientTransactionRollbackError(Guid operationId, string operation, long timestamp, IsolationLevel isolationLevel, SqlConnection connection, long? transactionId, string transactionName, Exception ex) + { + OperationId = operationId; + Operation = operation; + Timestamp = timestamp; + IsolationLevel = isolationLevel; + Connection = connection; + TransactionId = transactionId; + TransactionName = transactionName; + Exception = ex; + } + + + /// + public Guid OperationId { get; } + /// + public string Operation { get; } + /// + public long Timestamp { get; } + /// + public IsolationLevel IsolationLevel { get; } + /// + public SqlConnection Connection { get; } + /// + public long? TransactionId { get; } + /// + public string TransactionName { get; } + /// + public Exception Exception { get; } + + /// + public int Count => 3 + 5; + + /// + public KeyValuePair this[int index] + { + get => index switch + { + 0 => new KeyValuePair(nameof(OperationId), OperationId), + 1 => new KeyValuePair(nameof(Operation), Operation), + 2 => new KeyValuePair(nameof(Timestamp), Timestamp), + 3 => new KeyValuePair(nameof(IsolationLevel), IsolationLevel), + 4 => new KeyValuePair(nameof(Connection), Connection), + 5 => new KeyValuePair(nameof(TransactionId), TransactionId), + 6 => new KeyValuePair(nameof(TransactionName), TransactionName), + 7 => new KeyValuePair(nameof(Exception), Exception), + _ => throw new IndexOutOfRangeException(nameof(index)), + }; + } + + /// + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + public IEnumerator> GetEnumerator() + { + int count = Count; + for (int index = 0; index < count; index++) + { + yield return this[index]; + } + } + } +} 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 1d955b8c47..9f63cbaba8 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 @@ -3,9 +3,12 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections; +using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Runtime.CompilerServices; +using Microsoft.Data.SqlClient.Diagnostics; namespace Microsoft.Data.SqlClient { @@ -16,168 +19,155 @@ internal static class SqlClientDiagnosticListenerExtensions { public const string DiagnosticListenerName = "SqlClientDiagnosticListener"; - private const string SqlClientPrefix = "Microsoft.Data.SqlClient."; - - public const string SqlBeforeExecuteCommand = SqlClientPrefix + nameof(WriteCommandBefore); - public const string SqlAfterExecuteCommand = SqlClientPrefix + nameof(WriteCommandAfter); - public const string SqlErrorExecuteCommand = SqlClientPrefix + nameof(WriteCommandError); - - public const string SqlBeforeOpenConnection = SqlClientPrefix + nameof(WriteConnectionOpenBefore); - public const string SqlAfterOpenConnection = SqlClientPrefix + nameof(WriteConnectionOpenAfter); - public const string SqlErrorOpenConnection = SqlClientPrefix + nameof(WriteConnectionOpenError); - - public const string SqlBeforeCloseConnection = SqlClientPrefix + nameof(WriteConnectionCloseBefore); - public const string SqlAfterCloseConnection = SqlClientPrefix + nameof(WriteConnectionCloseAfter); - public const string SqlErrorCloseConnection = SqlClientPrefix + nameof(WriteConnectionCloseError); - - public const string SqlBeforeCommitTransaction = SqlClientPrefix + nameof(WriteTransactionCommitBefore); - public const string SqlAfterCommitTransaction = SqlClientPrefix + nameof(WriteTransactionCommitAfter); - public const string SqlErrorCommitTransaction = SqlClientPrefix + nameof(WriteTransactionCommitError); - - public const string SqlBeforeRollbackTransaction = SqlClientPrefix + nameof(WriteTransactionRollbackBefore); - public const string SqlAfterRollbackTransaction = SqlClientPrefix + nameof(WriteTransactionRollbackAfter); - public const string SqlErrorRollbackTransaction = SqlClientPrefix + nameof(WriteTransactionRollbackError); - public static Guid WriteCommandBefore(this SqlDiagnosticListener @this, SqlCommand sqlCommand, SqlTransaction transaction, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlBeforeExecuteCommand)) + if (@this.IsEnabled(SqlClientCommandBefore.Name)) { Guid operationId = Guid.NewGuid(); @this.Write( - SqlBeforeExecuteCommand, - new - { - OperationId = operationId, - Operation = operation, - ConnectionId = sqlCommand.Connection?.ClientConnectionId, - Command = sqlCommand, + SqlClientCommandBefore.Name, + new SqlClientCommandBefore( + operationId, + operation, + Stopwatch.GetTimestamp(), + sqlCommand.Connection?.ClientConnectionId, transaction?.InternalTransaction?.TransactionId, - Timestamp = Stopwatch.GetTimestamp() - }); + sqlCommand + ) + ); return operationId; } else + { return Guid.Empty; + } } public static void WriteCommandAfter(this SqlDiagnosticListener @this, Guid operationId, SqlCommand sqlCommand, SqlTransaction transaction, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlAfterExecuteCommand)) + if (@this.IsEnabled(SqlClientCommandAfter.Name)) { @this.Write( - SqlAfterExecuteCommand, - new - { - OperationId = operationId, - Operation = operation, - ConnectionId = sqlCommand.Connection?.ClientConnectionId, - Command = sqlCommand, + SqlClientCommandAfter.Name, + new SqlClientCommandAfter( + operationId, + operation, + Stopwatch.GetTimestamp(), + sqlCommand.Connection?.ClientConnectionId, transaction?.InternalTransaction?.TransactionId, - Statistics = sqlCommand.Statistics?.GetDictionary(), - Timestamp = Stopwatch.GetTimestamp() - }); + sqlCommand, + sqlCommand.Statistics?.GetDictionary() + ) + ); } } public static void WriteCommandError(this SqlDiagnosticListener @this, Guid operationId, SqlCommand sqlCommand, SqlTransaction transaction, Exception ex, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlErrorExecuteCommand)) + if (@this.IsEnabled(SqlClientCommandError.Name)) { @this.Write( - SqlErrorExecuteCommand, - new - { - OperationId = operationId, - Operation = operation, - ConnectionId = sqlCommand.Connection?.ClientConnectionId, - Command = sqlCommand, + SqlClientCommandError.Name, + new SqlClientCommandError + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + sqlCommand.Connection?.ClientConnectionId, transaction?.InternalTransaction?.TransactionId, - Exception = ex, - Timestamp = Stopwatch.GetTimestamp() - }); + sqlCommand, + ex + ) + ); } } public static Guid WriteConnectionOpenBefore(this SqlDiagnosticListener @this, SqlConnection sqlConnection, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlBeforeOpenConnection)) + if (@this.IsEnabled(SqlClientConnectionOpenBefore.Name)) { Guid operationId = Guid.NewGuid(); @this.Write( - SqlBeforeOpenConnection, - new - { - OperationId = operationId, - Operation = operation, - Connection = sqlConnection, - ClientVersion = ThisAssembly.InformationalVersion, - Timestamp = Stopwatch.GetTimestamp() - }); + SqlClientConnectionOpenBefore.Name, + new SqlClientConnectionOpenBefore + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + sqlConnection, + ThisAssembly.InformationalVersion + ) + ); return operationId; } else + { return Guid.Empty; + } } public static void WriteConnectionOpenAfter(this SqlDiagnosticListener @this, Guid operationId, SqlConnection sqlConnection, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlAfterOpenConnection)) + if (@this.IsEnabled(SqlClientConnectionOpenAfter.Name)) { @this.Write( - SqlAfterOpenConnection, - new - { - OperationId = operationId, - Operation = operation, - ConnectionId = sqlConnection.ClientConnectionId, - Connection = sqlConnection, - ClientVersion = ThisAssembly.InformationalVersion, - Statistics = sqlConnection.Statistics?.GetDictionary(), - Timestamp = Stopwatch.GetTimestamp() - }); + SqlClientConnectionOpenAfter.Name, + new SqlClientConnectionOpenAfter + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + sqlConnection.ClientConnectionId, + sqlConnection, + ThisAssembly.InformationalVersion, + sqlConnection.Statistics?.GetDictionary() + ) + ); } } public static void WriteConnectionOpenError(this SqlDiagnosticListener @this, Guid operationId, SqlConnection sqlConnection, Exception ex, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlErrorOpenConnection)) + if (@this.IsEnabled(SqlClientConnectionOpenError.Name)) { @this.Write( - SqlErrorOpenConnection, - new - { - OperationId = operationId, - Operation = operation, - ConnectionId = sqlConnection.ClientConnectionId, - Connection = sqlConnection, - ClientVersion = ThisAssembly.InformationalVersion, - Exception = ex, - Timestamp = Stopwatch.GetTimestamp() - }); + SqlClientConnectionOpenError.Name, + new SqlClientConnectionOpenError + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + sqlConnection.ClientConnectionId, + sqlConnection, + ThisAssembly.InformationalVersion, + ex + ) + ); } } public static Guid WriteConnectionCloseBefore(this SqlDiagnosticListener @this, SqlConnection sqlConnection, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlBeforeCloseConnection)) + if (@this.IsEnabled(SqlClientConnectionCloseBefore.Name)) { Guid operationId = Guid.NewGuid(); @this.Write( - SqlBeforeCloseConnection, - new - { - OperationId = operationId, - Operation = operation, - ConnectionId = sqlConnection.ClientConnectionId, - Connection = sqlConnection, - Statistics = sqlConnection.Statistics?.GetDictionary(), - Timestamp = Stopwatch.GetTimestamp() - }); + SqlClientConnectionCloseBefore.Name, + new SqlClientConnectionCloseBefore + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + sqlConnection.ClientConnectionId, + sqlConnection, + sqlConnection.Statistics?.GetDictionary() + ) + ); return operationId; } @@ -187,163 +177,175 @@ public static Guid WriteConnectionCloseBefore(this SqlDiagnosticListener @this, public static void WriteConnectionCloseAfter(this SqlDiagnosticListener @this, Guid operationId, Guid clientConnectionId, SqlConnection sqlConnection, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlAfterCloseConnection)) + if (@this.IsEnabled(SqlClientConnectionCloseAfter.Name)) { @this.Write( - SqlAfterCloseConnection, - new - { - OperationId = operationId, - Operation = operation, - ConnectionId = clientConnectionId, - Connection = sqlConnection, - Statistics = sqlConnection.Statistics?.GetDictionary(), - Timestamp = Stopwatch.GetTimestamp() - }); + SqlClientConnectionCloseAfter.Name, + new SqlClientConnectionCloseAfter + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + clientConnectionId, + sqlConnection, + sqlConnection.Statistics?.GetDictionary() + ) + ); } } public static void WriteConnectionCloseError(this SqlDiagnosticListener @this, Guid operationId, Guid clientConnectionId, SqlConnection sqlConnection, Exception ex, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlErrorCloseConnection)) + if (@this.IsEnabled(SqlClientConnectionCloseError.Name)) { @this.Write( - SqlErrorCloseConnection, - new - { - OperationId = operationId, - Operation = operation, - ConnectionId = clientConnectionId, - Connection = sqlConnection, - Statistics = sqlConnection.Statistics?.GetDictionary(), - Exception = ex, - Timestamp = Stopwatch.GetTimestamp() - }); + SqlClientConnectionCloseError.Name, + new SqlClientConnectionCloseError + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + clientConnectionId, + sqlConnection, + sqlConnection.Statistics?.GetDictionary(), + ex + ) + ); } } public static Guid WriteTransactionCommitBefore(this SqlDiagnosticListener @this, IsolationLevel isolationLevel, SqlConnection connection, SqlInternalTransaction transaction, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlBeforeCommitTransaction)) + if (@this.IsEnabled(SqlClientTransactionCommitBefore.Name)) { Guid operationId = Guid.NewGuid(); @this.Write( - SqlBeforeCommitTransaction, - new - { - OperationId = operationId, - Operation = operation, - IsolationLevel = isolationLevel, - Connection = connection, - transaction?.TransactionId, - Timestamp = Stopwatch.GetTimestamp() - }); + SqlClientTransactionCommitBefore.Name, + new SqlClientTransactionCommitBefore + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + isolationLevel, + connection, + transaction?.TransactionId + ) + ); return operationId; } else + { return Guid.Empty; + } } public static void WriteTransactionCommitAfter(this SqlDiagnosticListener @this, Guid operationId, IsolationLevel isolationLevel, SqlConnection connection, SqlInternalTransaction transaction, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlAfterCommitTransaction)) + if (@this.IsEnabled(SqlClientTransactionCommitAfter.Name)) { @this.Write( - SqlAfterCommitTransaction, - new - { - OperationId = operationId, - Operation = operation, - IsolationLevel = isolationLevel, - Connection = connection, - transaction?.TransactionId, - Timestamp = Stopwatch.GetTimestamp() - }); + SqlClientTransactionCommitAfter.Name, + new SqlClientTransactionCommitAfter + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + isolationLevel, + connection, + transaction?.TransactionId + ) + ); } } public static void WriteTransactionCommitError(this SqlDiagnosticListener @this, Guid operationId, IsolationLevel isolationLevel, SqlConnection connection, SqlInternalTransaction transaction, Exception ex, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlErrorCommitTransaction)) + if (@this.IsEnabled(SqlClientTransactionCommitError.Name)) { @this.Write( - SqlErrorCommitTransaction, - new - { - OperationId = operationId, - Operation = operation, - IsolationLevel = isolationLevel, - Connection = connection, + SqlClientTransactionCommitError.Name, + new SqlClientTransactionCommitError + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + isolationLevel, + connection, transaction?.TransactionId, - Exception = ex, - Timestamp = Stopwatch.GetTimestamp() - }); + ex + ) + ); } } public static Guid WriteTransactionRollbackBefore(this SqlDiagnosticListener @this, IsolationLevel isolationLevel, SqlConnection connection, SqlInternalTransaction transaction, string transactionName = null, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlBeforeRollbackTransaction)) + if (@this.IsEnabled(SqlClientTransactionRollbackBefore.Name)) { Guid operationId = Guid.NewGuid(); @this.Write( - SqlBeforeRollbackTransaction, - new - { - OperationId = operationId, - Operation = operation, - IsolationLevel = isolationLevel, - Connection = connection, + SqlClientTransactionRollbackBefore.Name, + new SqlClientTransactionRollbackBefore + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + isolationLevel, + connection, transaction?.TransactionId, - TransactionName = transactionName, - Timestamp = Stopwatch.GetTimestamp() - }); + transactionName + ) + ); return operationId; } else + { return Guid.Empty; + } } public static void WriteTransactionRollbackAfter(this SqlDiagnosticListener @this, Guid operationId, IsolationLevel isolationLevel, SqlConnection connection, SqlInternalTransaction transaction, string transactionName = null, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlAfterRollbackTransaction)) + if (@this.IsEnabled(SqlClientTransactionRollbackAfter.Name)) { @this.Write( - SqlAfterRollbackTransaction, - new - { - OperationId = operationId, - Operation = operation, - IsolationLevel = isolationLevel, - Connection = connection, + SqlClientTransactionRollbackAfter.Name, + new SqlClientTransactionRollbackAfter + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + isolationLevel, + connection, transaction?.TransactionId, - TransactionName = transactionName, - Timestamp = Stopwatch.GetTimestamp() - }); + transactionName + ) + ); } } public static void WriteTransactionRollbackError(this SqlDiagnosticListener @this, Guid operationId, IsolationLevel isolationLevel, SqlConnection connection, SqlInternalTransaction transaction, Exception ex, string transactionName = null, [CallerMemberName] string operation = "") { - if (@this.IsEnabled(SqlErrorRollbackTransaction)) + if (@this.IsEnabled(SqlClientTransactionRollbackError.Name)) { @this.Write( - SqlErrorRollbackTransaction, - new - { - OperationId = operationId, - Operation = operation, - IsolationLevel = isolationLevel, - Connection = connection, + SqlClientTransactionRollbackError.Name, + new SqlClientTransactionRollbackError + ( + operationId, + operation, + Stopwatch.GetTimestamp(), + isolationLevel, + connection, transaction?.TransactionId, - TransactionName = transactionName, - Exception = ex, - Timestamp = Stopwatch.GetTimestamp() - }); + transactionName, + ex + ) + ); } } 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 94325d3c88..9b484fc96f 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 @@ -19,6 +19,7 @@ using System.Xml; using Microsoft.Data.Common; using Microsoft.Data.Sql; +using Microsoft.Data.SqlClient.Diagnostics; using Microsoft.Data.SqlClient.Server; // NOTE: The current Microsoft.VSDesigner editor attributes are implemented for System.Data.SqlClient, and are not publicly available. @@ -596,7 +597,7 @@ internal SqlStatistics Statistics if (null != _activeConnection) { if (_activeConnection.StatisticsEnabled || - s_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlAfterExecuteCommand)) + s_diagnosticListener.IsEnabled(SqlClientCommandAfter.Name)) { return _activeConnection.Statistics; } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs index 1d044633ec..697315abc9 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs @@ -19,6 +19,7 @@ using System.Threading.Tasks; using Microsoft.Data.Common; using Microsoft.Data.ProviderBase; +using Microsoft.Data.SqlClient.Diagnostics; using Microsoft.SqlServer.Server; namespace Microsoft.Data.SqlClient @@ -1689,8 +1690,8 @@ private Task InternalOpenAsync(CancellationToken cancellationToken) TaskCompletionSource completion = new TaskCompletionSource(transaction); TaskCompletionSource result = new TaskCompletionSource(state: this); - if (s_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlAfterOpenConnection) || - s_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlErrorOpenConnection)) + if (s_diagnosticListener.IsEnabled(SqlClientConnectionOpenAfter.Name) || + s_diagnosticListener.IsEnabled(SqlClientConnectionOpenError.Name)) { result.Task.ContinueWith( continuationAction: s_openAsyncComplete, @@ -1879,8 +1880,8 @@ internal void Retry(Task retryTask) private void PrepareStatisticsForNewConnection() { if (StatisticsEnabled || - s_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlAfterExecuteCommand) || - s_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlAfterOpenConnection)) + s_diagnosticListener.IsEnabled(SqlClientCommandAfter.Name) || + s_diagnosticListener.IsEnabled(SqlClientConnectionOpenAfter.Name)) { if (null == _statistics) { @@ -1985,7 +1986,7 @@ private bool TryOpen(TaskCompletionSource retry, SqlConnec // The _statistics can change with StatisticsEnabled. Copying to a local variable before checking for a null value. SqlStatistics statistics = _statistics; if (StatisticsEnabled || - (s_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlAfterExecuteCommand) && statistics != null)) + (s_diagnosticListener.IsEnabled(SqlClientCommandAfter.Name) && statistics != null)) { _statistics._openTimestamp = ADP.TimerCurrent(); tdsInnerConnection.Parser.Statistics = _statistics;