Print/Intercept Database operations

Similar to the “Log” in SQL, is there a similar command to print operations for MongoDB?

context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);

Hi @Nicholas_Vella,

Welcome to the MongoDB Community Forums. In order to print the DB operations in the log (like the MQL), you can use LogTo with any logger such as Console.WriteLine and EnableSentitiveDataLogging to make sure you can see all fields in the database while creating your DbContext.

public static MflixDbContext Create(IMongoDatabase database) =>
    new(new DbContextOptionsBuilder<MflixDbContext>()
        .UseMongoDB(database.Client, database.DatabaseNamespace.DatabaseName)
        .EnableSensitiveDataLogging()
        .LogTo(Console.WriteLine)
        .Options);

Hope that helps.

Thanks,

Rishit.

Thank you, I was able to get the information I needed.

1 Like