Skip to content

Commit 2b364f4

Browse files
authored
fix naming, order and formatting for diagnostics (#1637)
1 parent 031afe8 commit 2b364f4

File tree

1 file changed

+108
-98
lines changed
  • src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient

1 file changed

+108
-98
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs

+108-98
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected override void AfterCleared(SqlCommand owner)
114114
private static bool _forceInternalEndQuery = false;
115115
#endif
116116

117-
private static readonly SqlDiagnosticListener _diagnosticListener = new SqlDiagnosticListener(SqlClientDiagnosticListenerExtensions.DiagnosticListenerName);
117+
private static readonly SqlDiagnosticListener s_diagnosticListener = new SqlDiagnosticListener(SqlClientDiagnosticListenerExtensions.DiagnosticListenerName);
118118
private bool _parentOperationStarted = false;
119119

120120
internal static readonly Action<object> s_cancelIgnoreFailure = CancelIgnoreFailureCallback;
@@ -554,7 +554,7 @@ internal SqlStatistics Statistics
554554
if (null != _activeConnection)
555555
{
556556
if (_activeConnection.StatisticsEnabled ||
557-
_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlAfterExecuteCommand))
557+
s_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlAfterExecuteCommand))
558558
{
559559
return _activeConnection.Statistics;
560560
}
@@ -1082,7 +1082,7 @@ public override object ExecuteScalar()
10821082
// between entry into Execute* API and the thread obtaining the stateObject.
10831083
_pendingCancel = false;
10841084

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

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

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

20072007
using (TryEventScope.Create("SqlCommand.ExecuteReader | API | Object Id {0}", ObjectID))
20082008
{
@@ -2030,11 +2030,11 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
20302030
WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous: true);
20312031
if (e != null)
20322032
{
2033-
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
2033+
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
20342034
}
20352035
else
20362036
{
2037-
_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
2037+
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
20382038
}
20392039
}
20402040
}
@@ -2129,12 +2129,16 @@ private void CleanupExecuteReaderAsync(Task<SqlDataReader> task, TaskCompletionS
21292129
Exception e = task.Exception.InnerException;
21302130
if (!_parentOperationStarted)
21312131
{
2132-
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
2132+
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
21332133
}
21342134
source.SetException(e);
21352135
}
21362136
else
21372137
{
2138+
if (!_parentOperationStarted)
2139+
{
2140+
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
2141+
}
21382142
if (task.IsCanceled)
21392143
{
21402144
source.SetCanceled();
@@ -2143,10 +2147,6 @@ private void CleanupExecuteReaderAsync(Task<SqlDataReader> task, TaskCompletionS
21432147
{
21442148
source.SetResult(task.Result);
21452149
}
2146-
if (!_parentOperationStarted)
2147-
{
2148-
_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
2149-
}
21502150
}
21512151
}
21522152

@@ -2524,7 +2524,7 @@ private Task<int> InternalExecuteNonQueryWithRetryAsync(CancellationToken cancel
25242524
private Task<int> InternalExecuteNonQueryAsync(CancellationToken cancellationToken)
25252525
{
25262526
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);
2527-
Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction);
2527+
Guid operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);
25282528

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

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

2547-
Task<int>.Factory.FromAsync(BeginExecuteNonQueryAsync, EndExecuteNonQueryAsync, null).ContinueWith((t) =>
2548-
{
2549-
registration.Dispose();
2550-
if (t.IsFaulted)
2551-
{
2552-
Exception e = t.Exception.InnerException;
2553-
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
2554-
source.SetException(e);
2555-
}
2556-
else
2547+
Task<int>.Factory.FromAsync(BeginExecuteNonQueryAsync, EndExecuteNonQueryAsync, null)
2548+
.ContinueWith((Task<int> task) =>
25572549
{
2558-
if (t.IsCanceled)
2550+
registration.Dispose();
2551+
if (task.IsFaulted)
25592552
{
2560-
source.SetCanceled();
2553+
Exception e = task.Exception.InnerException;
2554+
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
2555+
source.SetException(e);
25612556
}
25622557
else
25632558
{
2564-
source.SetResult(t.Result);
2559+
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
2560+
if (task.IsCanceled)
2561+
{
2562+
source.SetCanceled();
2563+
}
2564+
else
2565+
{
2566+
source.SetResult(task.Result);
2567+
}
25652568
}
2566-
_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
2567-
}
2568-
}, TaskScheduler.Default);
2569+
},
2570+
TaskScheduler.Default
2571+
);
25692572
}
25702573
catch (Exception e)
25712574
{
2572-
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
2575+
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
25732576
source.SetException(e);
25742577
}
25752578

@@ -2628,7 +2631,7 @@ private Task<SqlDataReader> InternalExecuteReaderAsync(CommandBehavior behavior,
26282631
Guid operationId = default(Guid);
26292632
if (!_parentOperationStarted)
26302633
{
2631-
operationId = _diagnosticListener.WriteCommandBefore(this, _transaction);
2634+
operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);
26322635
}
26332636

26342637
TaskCompletionSource<SqlDataReader> source = new TaskCompletionSource<SqlDataReader>();
@@ -2673,7 +2676,7 @@ private Task<SqlDataReader> InternalExecuteReaderAsync(CommandBehavior behavior,
26732676
{
26742677
if (!_parentOperationStarted)
26752678
{
2676-
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
2679+
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
26772680
}
26782681

26792682
source.SetException(e);
@@ -2700,7 +2703,7 @@ private Task<object> InternalExecuteScalarAsync(CancellationToken cancellationTo
27002703
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);
27012704
SqlClientEventSource.Log.TryTraceEvent("SqlCommand.InternalExecuteScalarAsync | API> {0}, Client Connection Id {1}, Command Text = '{2}'", ObjectID, Connection?.ClientConnectionId, CommandText);
27022705
_parentOperationStarted = true;
2703-
Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction);
2706+
Guid operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);
27042707

27052708
return ExecuteReaderAsync(cancellationToken).ContinueWith((executeTask) =>
27062709
{
@@ -2711,68 +2714,71 @@ private Task<object> InternalExecuteScalarAsync(CancellationToken cancellationTo
27112714
}
27122715
else if (executeTask.IsFaulted)
27132716
{
2714-
_diagnosticListener.WriteCommandError(operationId, this, _transaction, executeTask.Exception.InnerException);
2717+
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, executeTask.Exception.InnerException);
27152718
source.SetException(executeTask.Exception.InnerException);
27162719
}
27172720
else
27182721
{
27192722
SqlDataReader reader = executeTask.Result;
2720-
reader.ReadAsync(cancellationToken).ContinueWith((readTask) =>
2721-
{
2722-
try
2723+
reader.ReadAsync(cancellationToken)
2724+
.ContinueWith((Task<bool> readTask) =>
27232725
{
2724-
if (readTask.IsCanceled)
2725-
{
2726-
reader.Dispose();
2727-
source.SetCanceled();
2728-
}
2729-
else if (readTask.IsFaulted)
2730-
{
2731-
reader.Dispose();
2732-
_diagnosticListener.WriteCommandError(operationId, this, _transaction, readTask.Exception.InnerException);
2733-
source.SetException(readTask.Exception.InnerException);
2734-
}
2735-
else
2726+
try
27362727
{
2737-
Exception exception = null;
2738-
object result = null;
2739-
try
2740-
{
2741-
bool more = readTask.Result;
2742-
if (more && reader.FieldCount > 0)
2743-
{
2744-
try
2745-
{
2746-
result = reader.GetValue(0);
2747-
}
2748-
catch (Exception e)
2749-
{
2750-
exception = e;
2751-
}
2752-
}
2753-
}
2754-
finally
2728+
if (readTask.IsCanceled)
27552729
{
27562730
reader.Dispose();
2731+
source.SetCanceled();
27572732
}
2758-
if (exception != null)
2733+
else if (readTask.IsFaulted)
27592734
{
2760-
_diagnosticListener.WriteCommandError(operationId, this, _transaction, exception);
2761-
source.SetException(exception);
2735+
reader.Dispose();
2736+
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, readTask.Exception.InnerException);
2737+
source.SetException(readTask.Exception.InnerException);
27622738
}
27632739
else
27642740
{
2765-
_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
2766-
source.SetResult(result);
2741+
Exception exception = null;
2742+
object result = null;
2743+
try
2744+
{
2745+
bool more = readTask.Result;
2746+
if (more && reader.FieldCount > 0)
2747+
{
2748+
try
2749+
{
2750+
result = reader.GetValue(0);
2751+
}
2752+
catch (Exception e)
2753+
{
2754+
exception = e;
2755+
}
2756+
}
2757+
}
2758+
finally
2759+
{
2760+
reader.Dispose();
2761+
}
2762+
if (exception != null)
2763+
{
2764+
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, exception);
2765+
source.SetException(exception);
2766+
}
2767+
else
2768+
{
2769+
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
2770+
source.SetResult(result);
2771+
}
27672772
}
27682773
}
2769-
}
2770-
catch (Exception e)
2771-
{
2772-
// exception thrown by Dispose...
2773-
source.SetException(e);
2774-
}
2775-
}, TaskScheduler.Default);
2774+
catch (Exception e)
2775+
{
2776+
// exception thrown by Dispose...
2777+
source.SetException(e);
2778+
}
2779+
},
2780+
TaskScheduler.Default
2781+
);
27762782
}
27772783
_parentOperationStarted = false;
27782784
return source.Task;
@@ -2797,7 +2803,7 @@ private Task<XmlReader> InternalExecuteXmlReaderWithRetryAsync(CancellationToken
27972803
private Task<XmlReader> InternalExecuteXmlReaderAsync(CancellationToken cancellationToken)
27982804
{
27992805
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);
2800-
Guid operationId = _diagnosticListener.WriteCommandBefore(this, _transaction);
2806+
Guid operationId = s_diagnosticListener.WriteCommandBefore(this, _transaction);
28012807

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

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

2820-
Task<XmlReader>.Factory.FromAsync(BeginExecuteXmlReaderAsync, EndExecuteXmlReaderAsync, null).ContinueWith((t) =>
2821-
{
2822-
registration.Dispose();
2823-
if (t.IsFaulted)
2826+
Task<XmlReader>.Factory.FromAsync(BeginExecuteXmlReaderAsync, EndExecuteXmlReaderAsync, null)
2827+
.ContinueWith((Task<XmlReader> task) =>
28242828
{
2825-
Exception e = t.Exception.InnerException;
2826-
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
2827-
source.SetException(e);
2828-
}
2829-
else
2830-
{
2831-
if (t.IsCanceled)
2829+
registration.Dispose();
2830+
if (task.IsFaulted)
28322831
{
2833-
source.SetCanceled();
2832+
Exception e = task.Exception.InnerException;
2833+
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
2834+
source.SetException(e);
28342835
}
28352836
else
28362837
{
2837-
source.SetResult(t.Result);
2838+
s_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
2839+
if (task.IsCanceled)
2840+
{
2841+
source.SetCanceled();
2842+
}
2843+
else
2844+
{
2845+
source.SetResult(task.Result);
2846+
}
2847+
28382848
}
2839-
_diagnosticListener.WriteCommandAfter(operationId, this, _transaction);
2840-
}
2841-
}, TaskScheduler.Default);
2849+
},
2850+
TaskScheduler.Default
2851+
);
28422852
}
28432853
catch (Exception e)
28442854
{
2845-
_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
2855+
s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e);
28462856
source.SetException(e);
28472857
}
28482858

0 commit comments

Comments
 (0)