Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: fix naming, order and formatting for diagnostics #1637

Merged
merged 1 commit into from
Jun 7, 2022
Merged
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 @@ -114,7 +114,7 @@ protected override void AfterCleared(SqlCommand owner)
private static bool _forceInternalEndQuery = false;
#endif

private static readonly SqlDiagnosticListener _diagnosticListener = new SqlDiagnosticListener(SqlClientDiagnosticListenerExtensions.DiagnosticListenerName);
private static readonly SqlDiagnosticListener s_diagnosticListener = new SqlDiagnosticListener(SqlClientDiagnosticListenerExtensions.DiagnosticListenerName);
private bool _parentOperationStarted = false;

internal static readonly Action<object> s_cancelIgnoreFailure = CancelIgnoreFailureCallback;
Expand Down Expand Up @@ -554,7 +554,7 @@ internal SqlStatistics Statistics
if (null != _activeConnection)
{
if (_activeConnection.StatisticsEnabled ||
_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlAfterExecuteCommand))
s_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlAfterExecuteCommand))
{
return _activeConnection.Statistics;
}
Expand Down Expand Up @@ -1082,7 +1082,7 @@ public override object ExecuteScalar()
// between entry into Execute* API and the thread obtaining the stateObject.
_pendingCancel = false;

using (DiagnosticScope diagnosticScope = _diagnosticListener.CreateCommandScope(this, _transaction))
using (DiagnosticScope diagnosticScope = s_diagnosticListener.CreateCommandScope(this, _transaction))
using (TryEventScope.Create("SqlCommand.ExecuteScalar | API | ObjectId {0}", ObjectID))
{
SqlStatistics statistics = null;
Expand Down Expand Up @@ -1162,7 +1162,7 @@ public override int ExecuteNonQuery()
// between entry into Execute* API and the thread obtaining the stateObject.
_pendingCancel = false;

using (var diagnosticScope = _diagnosticListener.CreateCommandScope(this, _transaction))
using (var diagnosticScope = s_diagnosticListener.CreateCommandScope(this, _transaction))
using (TryEventScope.Create("SqlCommand.ExecuteNonQuery | API | Object Id {0}", ObjectID))
{
SqlStatistics statistics = null;
Expand Down Expand Up @@ -1670,7 +1670,7 @@ public XmlReader ExecuteXmlReader()
// between entry into Execute* API and the thread obtaining the stateObject.
_pendingCancel = false;

using (DiagnosticScope diagnosticScope = _diagnosticListener.CreateCommandScope(this, _transaction))
using (DiagnosticScope diagnosticScope = s_diagnosticListener.CreateCommandScope(this, _transaction))
using (TryEventScope.Create("SqlCommand.ExecuteXmlReader | API | Object Id {0}", ObjectID))
{
SqlStatistics statistics = null;
Expand Down Expand Up @@ -2002,7 +2002,7 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
bool success = false;
int? sqlExceptionNumber = null;
Exception e = null;
Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction);
Guid operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);

using (TryEventScope.Create("SqlCommand.ExecuteReader | API | Object Id {0}", ObjectID))
{
Expand Down Expand Up @@ -2030,11 +2030,11 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous: true);
if (e != null)
{
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
}
else
{
_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
}
}
}
Expand Down Expand Up @@ -2129,12 +2129,16 @@ private void CleanupExecuteReaderAsync(Task<SqlDataReader> task, TaskCompletionS
Exception e = task.Exception.InnerException;
if (!_parentOperationStarted)
{
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
}
source.SetException(e);
}
else
{
if (!_parentOperationStarted)
{
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
}
if (task.IsCanceled)
{
source.SetCanceled();
Expand All @@ -2143,10 +2147,6 @@ private void CleanupExecuteReaderAsync(Task<SqlDataReader> task, TaskCompletionS
{
source.SetResult(task.Result);
}
if (!_parentOperationStarted)
{
_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
}
}
}

Expand Down Expand Up @@ -2524,7 +2524,7 @@ private Task<int> InternalExecuteNonQueryWithRetryAsync(CancellationToken cancel
private Task<int> InternalExecuteNonQueryAsync(CancellationToken cancellationToken)
{
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlCommand.InternalExecuteNonQueryAsync | API | Correlation | Object Id {0}, Activity Id {1}, Client Connection Id {2}, Command Text '{3}'", ObjectID, ActivityCorrelator.Current, Connection?.ClientConnectionId, CommandText);
Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction);
Guid operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);

TaskCompletionSource<int> source = new TaskCompletionSource<int>();

Expand All @@ -2544,32 +2544,35 @@ private Task<int> InternalExecuteNonQueryAsync(CancellationToken cancellationTok
{
returnedTask = RegisterForConnectionCloseNotification(returnedTask);

Task<int>.Factory.FromAsync(BeginExecuteNonQueryAsync, EndExecuteNonQueryAsync, null).ContinueWith((t) =>
{
registration.Dispose();
if (t.IsFaulted)
{
Exception e = t.Exception.InnerException;
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
source.SetException(e);
}
else
Task<int>.Factory.FromAsync(BeginExecuteNonQueryAsync, EndExecuteNonQueryAsync, null)
.ContinueWith((Task<int> task) =>
{
if (t.IsCanceled)
registration.Dispose();
if (task.IsFaulted)
{
source.SetCanceled();
Exception e = task.Exception.InnerException;
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
source.SetException(e);
}
else
{
source.SetResult(t.Result);
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
if (task.IsCanceled)
{
source.SetCanceled();
}
else
{
source.SetResult(task.Result);
}
}
_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
}
}, TaskScheduler.Default);
},
TaskScheduler.Default
);
}
catch (Exception e)
{
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
source.SetException(e);
}

Expand Down Expand Up @@ -2628,7 +2631,7 @@ private Task<SqlDataReader> InternalExecuteReaderAsync(CommandBehavior behavior,
Guid operationId = default(Guid);
if (!_parentOperationStarted)
{
operationId = _diagnosticListener.WriteCommandBefore(this, _transaction);
operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);
}

TaskCompletionSource<SqlDataReader> source = new TaskCompletionSource<SqlDataReader>();
Expand Down Expand Up @@ -2673,7 +2676,7 @@ private Task<SqlDataReader> InternalExecuteReaderAsync(CommandBehavior behavior,
{
if (!_parentOperationStarted)
{
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
}

source.SetException(e);
Expand All @@ -2700,7 +2703,7 @@ private Task<object> InternalExecuteScalarAsync(CancellationToken cancellationTo
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlCommand.InternalExecuteScalarAsync | API | Correlation | Object Id {0}, Activity Id {1}, Client Connection Id {2}, Command Text '{3}'", ObjectID, ActivityCorrelator.Current, Connection?.ClientConnectionId, CommandText);
SqlClientEventSource.Log.TryTraceEvent("SqlCommand.InternalExecuteScalarAsync | API> {0}, Client Connection Id {1}, Command Text = '{2}'", ObjectID, Connection?.ClientConnectionId, CommandText);
_parentOperationStarted = true;
Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction);
Guid operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);

return ExecuteReaderAsync(cancellationToken).ContinueWith((executeTask) =>
{
Expand All @@ -2711,68 +2714,71 @@ private Task<object> InternalExecuteScalarAsync(CancellationToken cancellationTo
}
else if (executeTask.IsFaulted)
{
_diagnosticListener.WriteCommandError(operationId, this, _transaction, executeTask.Exception.InnerException);
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, executeTask.Exception.InnerException);
source.SetException(executeTask.Exception.InnerException);
}
else
{
SqlDataReader reader = executeTask.Result;
reader.ReadAsync(cancellationToken).ContinueWith((readTask) =>
{
try
reader.ReadAsync(cancellationToken)
.ContinueWith((Task<bool> readTask) =>
{
if (readTask.IsCanceled)
{
reader.Dispose();
source.SetCanceled();
}
else if (readTask.IsFaulted)
{
reader.Dispose();
_diagnosticListener.WriteCommandError(operationId, this, _transaction, readTask.Exception.InnerException);
source.SetException(readTask.Exception.InnerException);
}
else
try
{
Exception exception = null;
object result = null;
try
{
bool more = readTask.Result;
if (more && reader.FieldCount > 0)
{
try
{
result = reader.GetValue(0);
}
catch (Exception e)
{
exception = e;
}
}
}
finally
if (readTask.IsCanceled)
{
reader.Dispose();
source.SetCanceled();
}
if (exception != null)
else if (readTask.IsFaulted)
{
_diagnosticListener.WriteCommandError(operationId, this, _transaction, exception);
source.SetException(exception);
reader.Dispose();
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, readTask.Exception.InnerException);
source.SetException(readTask.Exception.InnerException);
}
else
{
_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
source.SetResult(result);
Exception exception = null;
object result = null;
try
{
bool more = readTask.Result;
if (more && reader.FieldCount > 0)
{
try
{
result = reader.GetValue(0);
}
catch (Exception e)
{
exception = e;
}
}
}
finally
{
reader.Dispose();
}
if (exception != null)
{
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, exception);
source.SetException(exception);
}
else
{
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
source.SetResult(result);
}
}
}
}
catch (Exception e)
{
// exception thrown by Dispose...
source.SetException(e);
}
}, TaskScheduler.Default);
catch (Exception e)
{
// exception thrown by Dispose...
source.SetException(e);
}
},
TaskScheduler.Default
);
}
_parentOperationStarted = false;
return source.Task;
Expand All @@ -2797,7 +2803,7 @@ private Task<XmlReader> InternalExecuteXmlReaderWithRetryAsync(CancellationToken
private Task<XmlReader> InternalExecuteXmlReaderAsync(CancellationToken cancellationToken)
{
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlCommand.InternalExecuteXmlReaderAsync | API | Correlation | Object Id {0}, Activity Id {1}, Client Connection Id {2}, Command Text '{3}'", ObjectID, ActivityCorrelator.Current, Connection?.ClientConnectionId, CommandText);
Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction);
Guid operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);

TaskCompletionSource<XmlReader> source = new TaskCompletionSource<XmlReader>();

Expand All @@ -2817,32 +2823,36 @@ private Task<XmlReader> InternalExecuteXmlReaderAsync(CancellationToken cancella
{
returnedTask = RegisterForConnectionCloseNotification(returnedTask);

Task<XmlReader>.Factory.FromAsync(BeginExecuteXmlReaderAsync, EndExecuteXmlReaderAsync, null).ContinueWith((t) =>
{
registration.Dispose();
if (t.IsFaulted)
Task<XmlReader>.Factory.FromAsync(BeginExecuteXmlReaderAsync, EndExecuteXmlReaderAsync, null)
.ContinueWith((Task<XmlReader> task) =>
{
Exception e = t.Exception.InnerException;
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
source.SetException(e);
}
else
{
if (t.IsCanceled)
registration.Dispose();
if (task.IsFaulted)
{
source.SetCanceled();
Exception e = task.Exception.InnerException;
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
source.SetException(e);
}
else
{
source.SetResult(t.Result);
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
if (task.IsCanceled)
{
source.SetCanceled();
}
else
{
source.SetResult(task.Result);
}

}
_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
}
}, TaskScheduler.Default);
},
TaskScheduler.Default
);
}
catch (Exception e)
{
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
source.SetException(e);
}

Expand Down