private void ProcessCommandRequestMessage(CommandRequestMessage originalMessage, Queue <RequestMessage> messageQueue, ConnectionId connectionId, ObjectId?serviceId, CommandMessageBinaryEncoder encoder, Stopwatch stopwatch)
        {
            var requestId   = originalMessage.RequestId;
            var operationId = EventContext.OperationId;

            var decodedMessage = encoder.ReadMessage();

            using (new CommandMessageDisposer(decodedMessage))
            {
                var type0Section  = decodedMessage.Sections.OfType <Type0CommandMessageSection>().Single();
                var command       = (BsonDocument)type0Section.Document;
                var type1Sections = decodedMessage.Sections.OfType <Type1CommandMessageSection>().ToList();
                if (type1Sections.Count > 0)
                {
                    command = new BsonDocument(command); // materialize the top level of the command RawBsonDocument
                    foreach (var type1Section in type1Sections)
                    {
                        var name  = type1Section.Identifier;
                        var items = new BsonArray(type1Section.Documents.GetBatchItems().Cast <RawBsonDocument>());
                        command[name] = items;
                    }
                }
                var commandName         = command.GetElement(0).Name;
                var databaseName        = command["$db"].AsString;
                var databaseNamespace   = new DatabaseNamespace(databaseName);
                var shouldRedactCommand = ShouldRedactCommand(command);
                if (shouldRedactCommand)
                {
                    command = new BsonDocument();
                }

                if (_startedEvent != null)
                {
                    var @event = new CommandStartedEvent(
                        commandName,
                        command,
                        databaseNamespace,
                        operationId,
                        requestId,
                        connectionId,
                        serviceId);

                    _startedEvent(@event);
                }

                if (_shouldTrackState)
                {
                    _state.TryAdd(requestId, new CommandState
                    {
                        CommandName          = commandName,
                        OperationId          = operationId,
                        Stopwatch            = stopwatch,
                        QueryNamespace       = new CollectionNamespace(databaseNamespace, "$cmd"),
                        ExpectedResponseType = decodedMessage.MoreToCome ? ExpectedResponseType.None : ExpectedResponseType.Command,
                        ShouldRedactReply    = shouldRedactCommand
                    });
                }
            }
        }
        private void ProcessCommandRequestMessage(CommandRequestMessage originalMessage, Queue <RequestMessage> messageQueue, ConnectionId connectionId, CommandMessageBinaryEncoder encoder, Stopwatch stopwatch)
        {
            var requestId   = originalMessage.RequestId;
            var operationId = EventContext.OperationId;

            var decodedMessage = encoder.ReadMessage();

            using (new CommandMessageDisposer(decodedMessage))
            {
                var type0Section      = decodedMessage.Sections.OfType <Type0CommandMessageSection>().Single();
                var command           = (BsonDocument)type0Section.Document;
                var commandName       = command.GetElement(0).Name;
                var databaseName      = command["$db"].AsString;
                var databaseNamespace = new DatabaseNamespace(databaseName);
                if (__securitySensitiveCommands.Contains(commandName))
                {
                    command = new BsonDocument();
                }

                if (_startedEvent != null)
                {
                    var @event = new CommandStartedEvent(
                        commandName,
                        command,
                        databaseNamespace,
                        operationId,
                        requestId,
                        connectionId);

                    _startedEvent(@event);
                }

                if (_shouldTrackState)
                {
                    _state.TryAdd(requestId, new CommandState
                    {
                        CommandName          = commandName,
                        OperationId          = operationId,
                        Stopwatch            = stopwatch,
                        QueryNamespace       = new CollectionNamespace(databaseNamespace, "$cmd"),
                        ExpectedResponseType = ExpectedResponseType.Command
                    });
                }
            }
        }