@@ -36,9 +36,9 @@ internal class CommandEventHelper
3636 private readonly EventLogger < LogCategories . Command > _eventLogger ;
3737 private ConcurrentDictionary < int , CommandState > _state ;
3838
39- private readonly bool _shouldProcessRequestMessages ;
39+ private readonly bool _eventsNeedBeforeSending ;
4040 private readonly bool _shouldTrackStarted ;
41- private readonly bool _shouldTrackState ;
41+ private readonly bool _eventsNeedState ;
4242 private readonly bool _shouldTrackFailed ;
4343 private readonly bool _shouldTrackSucceeded ;
4444 private readonly bool _tracingDisabled ;
@@ -57,26 +57,26 @@ public CommandEventHelper(EventLogger<LogCategories.Command> eventLogger, Tracin
5757 _tracingDisabled = tracingOptions ? . Disabled == true ;
5858 _queryTextMaxLength = tracingOptions ? . QueryTextMaxLength ?? 0 ;
5959
60- _shouldTrackState = _shouldTrackSucceeded || _shouldTrackFailed ;
61- _shouldProcessRequestMessages = _shouldTrackStarted || _shouldTrackState ;
60+ _eventsNeedState = _shouldTrackSucceeded || _shouldTrackFailed ;
61+ _eventsNeedBeforeSending = _shouldTrackStarted || _eventsNeedState ;
6262
63- if ( _shouldTrackState )
63+ if ( _eventsNeedState )
6464 {
6565 // we only need to track state if we have to raise
6666 // a succeeded or failed event or for tracing
6767 _state = new ConcurrentDictionary < int , CommandState > ( ) ;
6868 }
6969 }
7070
71- public bool ShouldCallBeforeSending => _shouldProcessRequestMessages || ShouldTraceWithActivityListener ( ) ;
71+ public bool ShouldCallBeforeSending => _eventsNeedBeforeSending || ShouldTraceWithActivityListener ( ) ;
7272
73- public bool ShouldCallAfterSending => _shouldTrackState || ShouldTraceWithActivityListener ( ) ;
73+ public bool ShouldCallAfterSending => _eventsNeedState || ShouldTraceWithActivityListener ( ) ;
7474
75- public bool ShouldCallErrorSending => _shouldTrackState || ShouldTraceWithActivityListener ( ) ;
75+ public bool ShouldCallErrorSending => _eventsNeedState || ShouldTraceWithActivityListener ( ) ;
7676
77- public bool ShouldCallAfterReceiving => _shouldTrackState || ShouldTraceWithActivityListener ( ) ;
77+ public bool ShouldCallAfterReceiving => _eventsNeedState || ShouldTraceWithActivityListener ( ) ;
7878
79- public bool ShouldCallErrorReceiving => _shouldTrackState || ShouldTraceWithActivityListener ( ) ;
79+ public bool ShouldCallErrorReceiving => _eventsNeedState || ShouldTraceWithActivityListener ( ) ;
8080
8181 private bool ShouldTraceWithActivityListener ( )
8282 => ! _tracingDisabled && MongoTelemetry . ActivitySource . HasListeners ( ) ;
@@ -742,16 +742,12 @@ private void TrackCommandState(
742742 {
743743 var shouldTraceCommand = ShouldTraceWithActivityListener ( ) && ! shouldRedactCommand && ! skipLogging ;
744744
745- if ( ! _shouldTrackState && ! shouldTraceCommand )
745+ if ( ! _eventsNeedState && ! shouldTraceCommand )
746746 {
747747 return ;
748748 }
749749
750- if ( ! _shouldTrackState && shouldTraceCommand )
751- {
752- // Lazy initialize state dictionary when tracing is needed but event tracking is not
753- _state ??= new ConcurrentDictionary < int , CommandState > ( ) ;
754- }
750+ _state ??= new ConcurrentDictionary < int , CommandState > ( ) ;
755751
756752 var commandState = new CommandState
757753 {
0 commit comments