-1

I have connected MongoDB to .NET and I'm trying to save data into the database. The program works fine and my data is inserted into the database, but my understanding from the documentation is that I should be able to see everything that is occurring in the console window as well. This is my sample program that I'm working on.

static void Main(string[] args)
    {
        MainAsync().Wait();
        Console.ReadLine();
    }

    static async Task MainAsync()
    {
        var client = new MongoClient("mongodb://localhost:27017");
        IMongoDatabase db = client.GetDatabase("machines");
        var collection = db.GetCollection<BsonDocument>("cranes");

        var document = new BsonDocument
        {
            { "code", BsonValue.Create("0x657")},
            { "departments", new BsonArray(new[] {"Mech", "Forge"}) },
        };

        await collection.InsertOneAsync(document);
    }

This is the console output I am seeing when running the program.

console_output

I can see that the data has been succesffully added in MongoDB Compass, but I do not have any feedback in the console window.

Valrog
  • 172
  • 1
  • 1
  • 11
  • *"....but my understanding from the documentation is that I should be able to see everything that is occurring in the console window as well"*. So if that's your understanding, then where is this statement actually made? Happy to bring up an issue if something is misrepresented. But if you want to make a statement like that, then you need to back it up by providing the reference. Then think about it. Why would using a driver log any output "just for the sake of it. By generally reasoning, you would need to explicitly tell it do something first. – Neil Lunn Apr 19 '18 at 13:42

1 Answers1

1

You need to initiate MongoClient by using MongoClientSettings instead, and subscribe for command events there.

var url = MongoUrl.Create("mongodb://localhost:27017");
var settings = MongoClientSettings.FromUrl(url);
settings.ClusterConfigurator += b => b.Subscribe<CommandStartedEvent>(
    e => Console.WriteLine($"MongoDB: {e.Command.ToJson()}")
);

var client = new MongoClient(settings);
...
tia
  • 9,518
  • 1
  • 30
  • 44