简体   繁体   English

如何在MongoDB C#Driver 2.0中记录我的查询?

[英]How do I log my queries in MongoDB C# Driver 2.0?

Just upgraded my application to the latest stable MongoDB C# Driver 2.0. 刚刚将我的应用程序升级到最新的稳定的MongoDB C#Driver 2.0。

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. 在迁移过程中,基本功能已被破坏,即使是最简单的查询,例如: this.collection.Find(e => e.Id == id).SingleOrDefaultAsync()也不会返回正确的数据。

Checked the class mappings and conventions but I would like to see the output query in order to properly identify the issue. 检查了类映射和约定,但我希望看到输出查询以正确识别问题。

So, how should it be done on the MongoClient side? 那么,应该如何在MongoClient端完成?

Setting profiling on the database level is possible but not a good solution since we have several applications and developers using the database. 在数据库级别设置分析是可能的,但不是一个好的解决方案,因为我们有几个应用程序和开发人员使用数据库。

My application is currently using Ninject.Extensions.Logging and log4net in the UI, business and EF data access. 我的应用程序当前正在UI,业务和EF数据访问中使用Ninject.Extensions.Logginglog4net

For newer C# MongoDB drivers the API has changed. 对于较新的C#MongoDB驱动程序,API已更改。 You have to use the more complex constructor that accepts a MongoClientSettings object, instead of the connection string. 您必须使用接受MongoClientSettings对象的更复杂的构造函数,而不是连接字符串。

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

You can enable logging by the mongo driver itself : 您可以通过mongo驱动程序本身启用日志记录

var settings = new MongoClientSettings
{
    ClusterConfigurator = cb =>
    {
        var textWriter = TextWriter.Synchronized(new StreamWriter("mylogfile.txt"));
        cb.AddListener(new LogListener(textWriter));
    }
};

You can hook it up to log4net if you wish. 如果您愿意,可以将它连接到log4net。

Logging to VS output with 2.7.3 driver. 使用2.7.3驱动程序登录VS输出。

using MongoDB.Bson;
using MongoDB.Driver;
using System;
#if TRACE
using System.Diagnostics;
using MongoDB.Driver.Core.Configuration;
#endif

...

public static ClusterBuilder ConfigureCluster(ClusterBuilder builder)
{
#if TRACE
    var traceSource = new TraceSource(nameof(Geotagging), SourceLevels.Verbose);
    builder.TraceWith(traceSource);
    builder.TraceCommandsWith(traceSource);
#endif
    return builder;
}

public MongoClient BuildMongoClient(string connection_string)
{
    var mongoUrlBuilder = new MongoUrlBuilder(connection_string);
    var settings = MongoClientSettings.FromUrl(mongoUrlBuilder.ToMongoUrl());
    settings.ClusterConfigurator = cb => ConfigureCluster(cb);
    return new MongoClient(settings);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM