@@ -58,8 +58,7 @@ public void ShouldRedactCommand_should_return_expected_result(string commandJson
5858 public void ShouldTrackState_should_be_correct (
5959 [ Values ( false , true ) ] bool logCommands ,
6060 [ Values ( false , true ) ] bool captureCommandSucceeded ,
61- [ Values ( false , true ) ] bool captureCommandFailed ,
62- [ Values ( false , true ) ] bool traceCommands )
61+ [ Values ( false , true ) ] bool captureCommandFailed )
6362 {
6463 var mockLogger = new Mock < ILogger < LogCategories . Command > > ( ) ;
6564 mockLogger . Setup ( m => m . IsEnabled ( LogLevel . Debug ) ) . Returns ( logCommands ) ;
@@ -76,7 +75,7 @@ public void ShouldTrackState_should_be_correct(
7675 }
7776
7877 var eventLogger = new EventLogger < LogCategories . Command > ( eventCapturer , mockLogger . Object ) ;
79- var tracingOptions = traceCommands ? new TracingOptions ( ) : new TracingOptions { Disabled = true } ;
78+ var tracingOptions = new TracingOptions { Disabled = true } ;
8079 var commandHelper = new CommandEventHelper ( eventLogger , tracingOptions ) ;
8180
8281 // No ActivityListener, so tracing doesn't contribute to _shouldTrackState
@@ -85,7 +84,7 @@ public void ShouldTrackState_should_be_correct(
8584
8685 [ Theory ]
8786 [ ParameterAttributeData ]
88- public void ShouldTrackState_should_be_correct_with_activity_listener (
87+ public void Callbacks_turn_on_when_listener_is_added_even_if_no_events (
8988 [ Values ( false , true ) ] bool logCommands ,
9089 [ Values ( false , true ) ] bool captureCommandSucceeded ,
9190 [ Values ( false , true ) ] bool captureCommandFailed ,
@@ -94,13 +93,6 @@ public void ShouldTrackState_should_be_correct_with_activity_listener(
9493 ActivityListener listener = null ;
9594 try
9695 {
97- listener = new ActivityListener
98- {
99- ShouldListenTo = source => source . Name == "MongoDB.Driver" ,
100- Sample = ( ref ActivityCreationOptions < ActivityContext > _ ) => ActivitySamplingResult . AllData
101- } ;
102- ActivitySource . AddActivityListener ( listener ) ;
103-
10496 var mockLogger = new Mock < ILogger < LogCategories . Command > > ( ) ;
10597 mockLogger . Setup ( m => m . IsEnabled ( LogLevel . Debug ) ) . Returns ( logCommands ) ;
10698
@@ -119,9 +111,27 @@ public void ShouldTrackState_should_be_correct_with_activity_listener(
119111 var tracingOptions = traceCommands ? new TracingOptions ( ) : new TracingOptions { Disabled = true } ;
120112 var commandHelper = new CommandEventHelper ( eventLogger , tracingOptions ) ;
121113
122- // With the new implementation, _shouldTrackState only reflects event tracking,
123- // not tracing with listeners (which is checked dynamically at execution time)
124- commandHelper . _shouldTrackState ( ) . Should ( ) . Be ( logCommands || captureCommandSucceeded || captureCommandFailed ) ;
114+ // When there are no listeners, these only return true if logging is enabled or an event is registered,
115+ // regardless of whether tracing is enabled.
116+ commandHelper . ShouldCallBeforeSending . Should ( ) . Be ( captureCommandSucceeded || captureCommandFailed || logCommands ) ;
117+ commandHelper . ShouldCallAfterSending . Should ( ) . Be ( captureCommandSucceeded || captureCommandFailed || logCommands ) ;
118+ commandHelper . ShouldCallErrorSending . Should ( ) . Be ( captureCommandSucceeded || captureCommandFailed || logCommands ) ;
119+ commandHelper . ShouldCallAfterReceiving . Should ( ) . Be ( captureCommandSucceeded || captureCommandFailed || logCommands ) ;
120+ commandHelper . ShouldCallErrorReceiving . Should ( ) . Be ( captureCommandSucceeded || captureCommandFailed || logCommands ) ;
121+
122+ listener = new ActivityListener
123+ {
124+ ShouldListenTo = source => source . Name == "MongoDB.Driver" ,
125+ Sample = ( ref ActivityCreationOptions < ActivityContext > _ ) => ActivitySamplingResult . AllData
126+ } ;
127+ ActivitySource . AddActivityListener ( listener ) ;
128+
129+ // With listeners registered, these always return true when unless everything is disabled.
130+ commandHelper . ShouldCallBeforeSending . Should ( ) . Be ( captureCommandSucceeded || captureCommandFailed || logCommands || traceCommands ) ;
131+ commandHelper . ShouldCallAfterSending . Should ( ) . Be ( captureCommandSucceeded || captureCommandFailed || logCommands || traceCommands ) ;
132+ commandHelper . ShouldCallErrorSending . Should ( ) . Be ( captureCommandSucceeded || captureCommandFailed || logCommands || traceCommands ) ;
133+ commandHelper . ShouldCallAfterReceiving . Should ( ) . Be ( captureCommandSucceeded || captureCommandFailed || logCommands || traceCommands ) ;
134+ commandHelper . ShouldCallErrorReceiving . Should ( ) . Be ( captureCommandSucceeded || captureCommandFailed || logCommands || traceCommands ) ;
125135 }
126136 finally
127137 {
0 commit comments