Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ public sealed partial class SqlCommand : DbCommand, ICloneable
/// </summary>
private static bool _forceRetryableEnclaveQueryExecutionExceptionDuringGenerateEnclavePackage = false;
#endif

private static readonly SqlDiagnosticListener s_diagnosticListener = new SqlDiagnosticListener();
private bool _parentOperationStarted = false;

internal static readonly Action<object> s_cancelIgnoreFailure = CancelIgnoreFailureCallback;

private _SqlRPC[] _rpcArrayOf1 = null; // Used for RPC executes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public override int ExecuteNonQuery()
// between entry into Execute* API and the thread obtaining the stateObject.
_pendingCancel = false;

#if NET
using var diagnosticScope = s_diagnosticListener.CreateCommandScope(this, _transaction);
#endif

using var eventScope = TryEventScope.Create($"SqlCommand.ExecuteNonQuery | API | Object Id {ObjectID}");
SqlClientEventSource.Log.TryCorrelationTraceEvent(
Expand Down Expand Up @@ -128,9 +126,7 @@ public override int ExecuteNonQuery()
}
catch (Exception ex)
{
#if NET
diagnosticScope.SetException(ex);
#endif

if (ex is SqlException sqlException)
{
Expand Down Expand Up @@ -326,26 +322,20 @@ private void CleanupAfterExecuteNonQueryAsync(Task<int> task, TaskCompletionSour
{
Exception e = task.Exception?.InnerException;

#if NET
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
#endif

source.SetException(e);
}
else if (task.IsCanceled)
{
#if NET
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
#endif

source.SetCanceled();
}
else
{
// Task successful
#if NET
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
#endif

source.SetResult(task.Result);
}
Expand Down Expand Up @@ -652,11 +642,7 @@ private Task<int> InternalExecuteNonQueryAsync(CancellationToken cancellationTok
$"Client Connection Id {_activeConnection?.ClientConnectionId}, " +
$"Command Text '{CommandText}'");

#if NET
Guid operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);
#else
Guid operationId = Guid.Empty;
#endif

// Connection can be used as state in RegisterForConnectionCloseNotification continuation
// to avoid an allocation so use it as the state value if possible but it can be changed if
Expand Down Expand Up @@ -715,9 +701,7 @@ private Task<int> InternalExecuteNonQueryAsync(CancellationToken cancellationTok
}
catch (Exception e)
{
#if NET
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
#endif

source.SetException(e);
context.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,8 @@ public SqlDataReader EndExecuteReader(IAsyncResult asyncResult)
_pendingCancel = false;

// @TODO: Do we want to use a command scope here like nonquery and xml? or is operation id ok?
#if NET
Guid operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);
Exception e = null;
#endif

using var eventScope = TryEventScope.Create($"SqlCommand.ExecuteReader | API | Object Id {ObjectID}");
// @TODO: Do we want to have a correlation trace event here like nonquery and xml?
Expand All @@ -136,9 +134,7 @@ public SqlDataReader EndExecuteReader(IAsyncResult asyncResult)
// @TODO: CER Exception Handling was removed here (see GH#3581)
catch (Exception ex)
{
#if NET
e = ex;
#endif

if (ex is SqlException sqlException)
{
Expand All @@ -152,7 +148,6 @@ public SqlDataReader EndExecuteReader(IAsyncResult asyncResult)
SqlStatistics.StopTimer(statistics);
WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous: true);

#if NET
if (e is not null)
{
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
Expand All @@ -161,7 +156,6 @@ public SqlDataReader EndExecuteReader(IAsyncResult asyncResult)
{
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
}
#endif
}
}

Expand Down Expand Up @@ -573,23 +567,19 @@ private void CleanupExecuteReaderAsync(
{
Exception e = task.Exception.InnerException;

#if NET
if (!_parentOperationStarted)
{
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
}
#endif

source.SetException(e);
}
else
{
#if NET
if (!_parentOperationStarted)
{
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
}
#endif

if (task.IsCanceled)
{
Expand Down Expand Up @@ -940,13 +930,7 @@ private Task<SqlDataReader> InternalExecuteReaderAsync(
$"Client Connection Id {_activeConnection?.ClientConnectionId}, " +
$"Command Text '{CommandText}'");

Guid operationId = Guid.Empty;
#if NET
if (!_parentOperationStarted)
{
operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);
}
#endif
Guid operationId = !_parentOperationStarted ? s_diagnosticListener.WriteCommandBefore(this, _transaction) : Guid.Empty;

// Connection can be used as state in RegisterForConnectionCloseNotification
// continuation to avoid an allocation so use it as the state value if possible, but it
Expand Down Expand Up @@ -1015,12 +999,10 @@ private Task<SqlDataReader> InternalExecuteReaderAsync(
}
catch (Exception e)
{
#if NET
if (!_parentOperationStarted)
{
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
}
#endif

source.SetException(e);
context?.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public override object ExecuteScalar()
// between entry into Execute* API and the thread obtaining the stateObject.
_pendingCancel = false;

#if NET
using var diagnosticScope = s_diagnosticListener.CreateCommandScope(this, _transaction);
#endif

using var eventScope = TryEventScope.Create($"SqlCommand.ExecuteScalar | API | Object Id {ObjectID}");
SqlClientEventSource.Log.TryCorrelationTraceEvent(
Expand Down Expand Up @@ -60,9 +58,7 @@ public override object ExecuteScalar()
}
catch (Exception ex)
{
#if NET
diagnosticScope.SetException(ex);
#endif

if (ex is SqlException sqlException)
{
Expand Down Expand Up @@ -233,10 +229,8 @@ private Task<object> ExecuteScalarAsyncInternal(CancellationToken cancellationTo
$"Client Connection Id {_activeConnection?.ClientConnectionId}, " +
$"Command Text '{CommandText}'");

#if NET
Guid operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);
_parentOperationStarted = true;
#endif

// @TODO: Use continue with state? This would be a good candidate for rewriting async/await
return ExecuteReaderAsync(cancellationToken).ContinueWith(executeTask =>
Expand All @@ -249,13 +243,11 @@ private Task<object> ExecuteScalarAsyncInternal(CancellationToken cancellationTo
}
else if (executeTask.IsFaulted)
{
#if NET
s_diagnosticListener.WriteCommandError(
operationId,
this,
_transaction,
executeTask.Exception.InnerException);
#endif

source.SetException(executeTask.Exception.InnerException);
}
Expand All @@ -279,13 +271,11 @@ private Task<object> ExecuteScalarAsyncInternal(CancellationToken cancellationTo
{
reader.Dispose();

#if NET
s_diagnosticListener.WriteCommandError(
operationId,
this,
_transaction,
readTask.Exception.InnerException);
#endif

source.SetException(readTask.Exception.InnerException);
}
Expand Down Expand Up @@ -316,17 +306,13 @@ private Task<object> ExecuteScalarAsyncInternal(CancellationToken cancellationTo

if (exception is not null)
{
#if NET
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, exception);
#endif

source.SetException(exception);
}
else
{
#if NET
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
#endif

source.SetResult(result);
}
Expand All @@ -341,9 +327,7 @@ private Task<object> ExecuteScalarAsyncInternal(CancellationToken cancellationTo
TaskScheduler.Default);
}

#if NET
_parentOperationStarted = false;
#endif

return source.Task;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ public XmlReader ExecuteXmlReader()
// between entry into Execute* API and the thread obtaining the stateObject.
_pendingCancel = false;

#if NET
using var diagnosticScope = s_diagnosticListener.CreateCommandScope(this, _transaction);
#endif

using var eventScope = TryEventScope.Create($"SqlCommand.ExecuteXmlReader | API | Object Id {ObjectID}");
SqlClientEventSource.Log.TryCorrelationTraceEvent(
Expand Down Expand Up @@ -116,9 +114,7 @@ public XmlReader ExecuteXmlReader()
}
catch (Exception ex)
{
#if NET
diagnosticScope.SetException(ex);
#endif

if (ex is SqlException sqlException)
{
Expand Down Expand Up @@ -365,25 +361,19 @@ private void CleanupAfterExecuteXmlReaderAsync(
{
Exception e = task.Exception?.InnerException;

#if NET
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
#endif

source.SetException(e);
}
else if (task.IsCanceled)
{
#if NET
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
#endif

source.SetCanceled();
}
else
{
#if NET
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
#endif

source.SetResult(task.Result);
}
Expand Down Expand Up @@ -475,11 +465,7 @@ private Task<XmlReader> InternalExecuteXmlReaderAsync(CancellationToken cancella
$"Client Connection Id {_activeConnection?.ClientConnectionId}, " +
$"Command Text '{CommandText}'");

#if NET
Guid operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);
#else
Guid operationId = Guid.Empty;
#endif

// Connection can be used as state in RegisterForConnectionCloseNotification continuation
// to avoid an allocation so use it as the state value if possible but it can be changed if
Expand Down Expand Up @@ -547,9 +533,7 @@ private Task<XmlReader> InternalExecuteXmlReaderAsync(CancellationToken cancella
}
catch (Exception e)
{
#if NET
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
#endif

source.SetException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
using System.Threading;
using Microsoft.Data.Common;
using Microsoft.Data.Sql;

#if NET
using Microsoft.Data.SqlClient.Diagnostics;
#endif

namespace Microsoft.Data.SqlClient
{
Expand Down Expand Up @@ -52,6 +49,18 @@ public sealed partial class SqlCommand : DbCommand, ICloneable
/// </summary>
private static int _objectTypeCount = 0;

/// <summary>
/// Static instance of the <see cref="SqlDiagnosticListener"/> used for capturing and emitting
/// diagnostic events related to SqlCommand operations.
/// </summary>
private static readonly SqlDiagnosticListener s_diagnosticListener = new();

/// <summary>
/// Prevents the completion events for ExecuteReader from being fired if ExecuteReader is being
/// called as part of a parent operation (e.g. ExecuteScalar, or SqlBatch.ExecuteScalar.)
/// </summary>
private bool _parentOperationStarted = false;

/// <summary>
/// Connection that will be used to process the current instance.
/// </summary>
Expand Down Expand Up @@ -571,12 +580,8 @@ internal SqlStatistics Statistics
{
if (_activeConnection is not null)
{
#if NET
bool isStatisticsEnabled = _activeConnection.StatisticsEnabled ||
s_diagnosticListener.IsEnabled(SqlClientCommandAfter.Name);
#else
bool isStatisticsEnabled = _activeConnection.StatisticsEnabled;
#endif

if (isStatisticsEnabled)
{
Expand Down
Loading
Loading