Turnerj Undefined since May, 2024

Using MiniProfiler with MongoDB

Jun 8, 2019

If you're familiar with .NET, you may have heard of an awesome project called MiniProfiler made by the awesome folks at Stack Overflow.

If you're unfamiliar, you may have gathered from the name it has something to do with profiling code - you wouldn't be wrong!

MiniProfiler isn't designed to profile every method call - it is however designed to profile the calls you specifically want to know about. These include things like database calls, your controllers or your views. You can optionally profile any other code you want through MiniProfiler's API.

MongoDB - some love it, some hate it but regardless I use it and I like it for my projects. What I don't like with MongoDB is the C# driver so as an ongoing project of mine, I have built my own wrapper for MongoDB C# driver called MongoFramework. I have written about this library previously here so I won't go into much detail besides saying it makes dealing with MongoDB similar to dealing with Entity Framework.

MongoFramework however is how MiniProfiler and MongoDB meet. You see, MiniProfiler has official packages for profiling EF6 and EF Core but doesn't actually support profiling of MongoDB queries. As I had already written a wrapper around the official MongoDB driver, I was already in a good place to extend my own integration to support profiling.

MongoFramework uses a diagnostic layer inspired by how MiniProfiler actually connects into EF Core. This diagnostic layer is per connection for MongoFramework and as an interface, can easily be swapped out for any other diagnostic tool. The diagnostic layer is invoked at every entity read/write as well as the creation of indexes.

MiniProfiler profiling dialog showing a MongoDB column

MiniProfiler showing an actual MongoDB query

So if you:

  • are building a project in .NET
  • are using MongoDB for persistence via MongoFramework
  • are using or want to use MiniProfiler

Then this might be exactly what you are looking for! Check it out on NuGet and GitHub.