如何在 MongoDB C# Driver 2.0 中记录我的查询? [英] How do I log my queries in MongoDB C# Driver 2.0?

查看:23
本文介绍了如何在 MongoDB C# Driver 2.0 中记录我的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚将我的应用程序升级到最新的稳定版 MongoDB C# 驱动程序 2.0.

Just upgraded my application to the latest stable MongoDB C# Driver 2.0.

在迁移期间,基本功能被破坏,即使是最简单的查询,如:this.collection.Find(e => e.Id == id).SingleOrDefaultAsync() 也没有返回正确的数据.

During the migration, basic functionality has been broken and even the simplest query like: this.collection.Find(e => e.Id == id).SingleOrDefaultAsync() doesn't return the correct data.

检查了类映射和约定,但我想查看输出查询以正确识别问题.

Checked the class mappings and conventions but I would like to see the output query in order to properly identify the issue.

那么,在MongoClient端应该怎么做呢?

So, how should it be done on the MongoClient side?

在数据库级别设置分析是可能的,但不是一个好的解决方案,因为我们有多个应用程序和开发人员使用该数据库.

Setting profiling on the database level is possible but not a good solution since we have several applications and developers using the database.

我的应用程序当前在 UI、业务和 EF 数据访问中使用 Ninject.Extensions.Logginglog4net.

My application is currently using Ninject.Extensions.Logging and log4net in the UI, business and EF data access.

推荐答案

对于较新的 C# MongoDB 驱动程序,API 已更改.您必须使用接受 MongoClientSettings 对象而不是连接字符串的更复杂的构造函数.

For newer C# MongoDB drivers the API has changed. You have to use the more complex constructor that accepts a MongoClientSettings object, instead of the connection string.

使用以下代码继续使用连接字符串,但启用每个命令的日志记录:

Use the following code to keep using a connection string, but enable the logging of each command:

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

这篇关于如何在 MongoDB C# Driver 2.0 中记录我的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆