2

I need to set profiling level via Mongo C# Driver.

Client.GetDatabase("test") returns IMongoDatabase interface which resolves to MongoDB.Driver.MongoDatabaseImpl at runtime.

According to MongoDB .NET Driver API Documentation MongoDatase class has a SetProfilingLevel method which I can not cast to at runtime.

BTW I also installed Legacy Driver version 2.0.1 since documentation says that SetProfilingLevel method is in it.

2 Answers 2

6
+50

New MongoDB driver (at least 2.3) doesn't have specific method for changing profiling level.
But you can execute any command using RunCommandAsync.

public async Task SetProfilingLevelAsync(IMongoDatabase database, int level)
{
    var command = new BsonDocumentCommand<BsonDocument>(new BsonDocument("profile", level));
    await database.RunCommandAsync(command);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You may want to add optional slowms parameter also.
3

This method is indeed in legacy version of driver. So first intall legacy version and then:

var client = new MongoServer(new MongoServerSettings());
var db = client.GetDatabase("db_name");
db.SetProfilingLevel(ProfilingLevel.All);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.