0

How can I trace mongo commands using C# mongo driver and write commands to log

Anadil
  • 489
  • 1
  • 4
  • 7
  • Possible duplicate of [MongoDB logging all queries](https://stackoverflow.com/questions/15204341/mongodb-logging-all-queries) – dnickless Oct 22 '18 at 18:59
  • http://api.mongodb.com/csharp/current/html/M_MongoDB_Driver_MongoDatabase_SetProfilingLevel.htm – dnickless Oct 22 '18 at 19:01

1 Answers1

1

Use MongoClientSettings to create MongoClient as follows:

        var mongoConnectionUrl = new MongoUrl(connectionString);
        var mongoClientSettings = MongoClientSettings.FromUrl(mongoConnectionUrl);
        mongoClientSettings.ClusterConfigurator = cb =>
        {
            cb.Subscribe<CommandStartedEvent>(e =>
            {
                logger.LogInformation($"{e.CommandName} - {e.Command.ToJson()}");
            });
        };

        var client = new MongoClient(mongoClientSettings);
Anadil
  • 489
  • 1
  • 4
  • 7