.NET Core — XRay trace all ADO.NET operations, even Dapper requests, using MiniProfiler
Dragnet Tech is moving to a serverless architecture using AWS Lambda. As part of this we’ve started using the XRay and the AWS XRay .NET tookit. XRay provides a clear implementation for profiling SQL Server SQLCommand operations but it was less clear how to profile non-SQL Server operations and also how to cleanly profile Dapper connections without peppering my code with XRay references. I’m using PostgreSQL from my database, not SQL Server so the XRay Toolkit’s default SQL tooling didn’t work for me.
What I really wanted was a way to attach the profiling when the connection was first opened and then every operation on that connection would be profiled. MiniProfiler has implemented this cleanly for their web profiling tools and it is easy to extend.
What I ended up with was the following. Install the following Nuget package.
Install-Package MiniProfiler.Shared
Anywhere you open a connection add the line in the function. This is an example of how I open connections in my app.
Add this class somewhere in your code base.
The reason I have to wrap the ProfileDbConnection class with NpgsqlConnection is because Dapper checks the name of the class for the connection to determine if it should use PostgreSQL specific features.
You’ll see for each operations it opens a subsegment and closes the subsegment and logs out the SQL operations. You’ll notice I get timings now for every PostgreSQL Dapper query I make in my application without having to touch very much.