@@ -21,37 +21,35 @@ public void XEventActivityIDConsistentWithTracing(string query, System.Data.Comm
21
21
// where it can be recorded in an XEvent session. This is documented at:
22
22
// https://learn.microsoft.com/en-us/sql/relational-databases/native-client/features/accessing-diagnostic-information-in-the-extended-events-log
23
23
24
- using ( SqlConnection xEventManagementConnection = new SqlConnection ( DataTestUtility . TCPConnectionString ) )
25
- using ( DataTestUtility . XEventScope xEventSession = new DataTestUtility . XEventScope ( xEventManagementConnection ,
26
- @"ADD EVENT SQL_STATEMENT_STARTING (ACTION (client_connection_id)),
27
- ADD EVENT RPC_STARTING (ACTION (client_connection_id))" ,
28
- "ADD TARGET ring_buffer" ) )
29
- {
30
- Guid connectionId ;
31
- HashSet < string > ids ;
24
+ using SqlConnection activityConnection = new ( DataTestUtility . TCPConnectionString ) ;
25
+ activityConnection . Open ( ) ;
32
26
33
- using ( DataTestUtility . MDSEventListener TraceListener = new ( ) )
34
- using ( SqlConnection connection = new ( DataTestUtility . TCPConnectionString ) )
35
- {
36
- connection . Open ( ) ;
37
- connectionId = connection . ClientConnectionId ;
27
+ Guid connectionId = activityConnection . ClientConnectionId ;
28
+ HashSet < string > ids ;
38
29
39
- using SqlCommand command = new ( query , connection ) { CommandType = commandType } ;
40
- using SqlDataReader reader = command . ExecuteReader ( ) ;
41
- while ( reader . Read ( ) )
42
- {
43
- // Flush data
44
- }
30
+ using SqlConnection xEventManagementConnection = new ( DataTestUtility . TCPConnectionString ) ;
31
+ using DataTestUtility . XEventScope xEventSession = new ( xEventManagementConnection ,
32
+ $@ "ADD EVENT SQL_STATEMENT_STARTING (ACTION (client_connection_id) WHERE (client_connection_id='{ connectionId } ')),
33
+ ADD EVENT RPC_STARTING (ACTION (client_connection_id) WHERE (client_connection_id='{ connectionId } '))" ,
34
+ "ADD TARGET ring_buffer" ) ;
45
35
46
- ids = TraceListener . ActivityIDs ;
36
+ using ( DataTestUtility . MDSEventListener TraceListener = new ( ) )
37
+ {
38
+ using SqlCommand command = new ( query , activityConnection ) { CommandType = commandType } ;
39
+ using SqlDataReader reader = command . ExecuteReader ( ) ;
40
+ while ( reader . Read ( ) )
41
+ {
42
+ // Flush data
47
43
}
48
44
49
- XmlDocument eventList = xEventSession . GetEvents ( ) ;
50
- // Get the associated activity ID from the XEvent session. We expect to see the same ID in the trace as well.
51
- string activityId = GetCommandActivityId ( query , xEvent , connectionId , eventList ) ;
52
-
53
- Assert . Contains ( activityId , ids ) ;
45
+ ids = TraceListener . ActivityIDs ;
54
46
}
47
+
48
+ XmlDocument eventList = xEventSession . GetEvents ( ) ;
49
+ // Get the associated activity ID from the XEvent session. We expect to see the same ID in the trace as well.
50
+ string activityId = GetCommandActivityId ( query , xEvent , connectionId , eventList ) ;
51
+
52
+ Assert . Contains ( activityId , ids ) ;
55
53
}
56
54
57
55
private static string GetCommandActivityId ( string commandText , string eventName , Guid connectionId , XmlDocument xEvents )
0 commit comments