如何使用C#mongo驱动程序跟踪mongo命令

时间:2018-10-22 18:56:46

标签: c# mongodb logging

如何使用C#mongo驱动程序跟踪mongo命令并编写命令进行记录

1 个答案:

答案 0 :(得分:0)

使用MongoClientSettings如下创建MongoClient:

        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);