Dapper queries in an ASP.NET Core debug dashboard, without wrapping your connections
AspNetDebugDashboard is my take on Laravel Telescope for ASP.NET Core. One NuGet package, and in Development you get a page at /_debug with every request your app handled: its SQL queries on a timeline, outgoing HttpClient calls, logs and exceptions.
Until this week the SQL part only saw EF Core, through a DbCommandInterceptor. Dapper and plain ADO.NET queries never showed up, and plenty of apps run their hot paths on Dapper. 2.6 and 2.7 close that gap.
Nothing to set up
The drivers already report their commands, so the dashboard listens instead of asking you to wrap anything:
using var conn = new SqlConnection(connectionString); // or NpgsqlConnection, MySqlConnection
var orders = await conn.QueryAsync<Order>("select * from Orders where CustomerId = @id", new { id });
SqlClient publishes every command as a DiagnosticListener event, the same mechanism the dashboard already uses to see every HttpClient call, and those events carry the full command, parameters included.
Npgsql and MySqlConnector report each command as a trace span instead. The span has the SQL and the timing but no parameter values, and it runs until the reader is closed, so on Postgres and MySQL the time includes reading the rows.
The catch is EF Core: it runs on the same drivers, so its commands show up there too. Left alone, every EF query would be recorded twice. For SqlClient the EF interceptor claims each command right before it runs and the listener skips claimed ones.
Spans carry no command to claim, so for Npgsql and MySqlConnector the dashboard skips spans that start in a request while one of EF's commands is running. The cost is that a Dapper query running at the very same moment as an EF query in the same request gets skipped too.
When you want more
SQLite reports nothing, and on Postgres or MySQL you might want the parameter values. For those there's one call:
using var conn = new NpgsqlConnection(connectionString).WithDebugDashboard();
The wrapper records each command with its parameters and rows affected. When the dashboard isn't active, which by default means anywhere but Development, it hands back the connection you gave it, so the call can stay in production code.
It's a plain DbConnection, though, so code that casts it back to NpgsqlConnection (binary import, say) needs the original.
Same timeline, same N+1 check
Dapper queries go on the request timeline like EF's, and the repeated-query check works on them too. The sample app has a Dapper version of its N+1 report, one query per order.
Since all of this rides on events I don't control, CI now starts a real SQL Server and a real Postgres on every build and checks, on .NET 8, 10 and 11, that Dapper queries come through and EF queries are recorded exactly once.
Aspire
If you run your app from an Aspire AppHost, a small new package adds a link to the dashboard in Aspire's resource list:
builder.AddProject<Projects.Api>("api").WithDebugDashboard();
Without the package, the address is logged at startup (Debug dashboard: http://localhost:5000/_debug), and Aspire's console view turns that line into a link.
Trying it
dotnet add package AspNetDebugDashboard
builder.Services.AddDebugDashboard();
var app = builder.Build();
app.UseDebugDashboard();
Then open /_debug. Everything stays in a local LiteDB file. It's MIT and runs on .NET 8 through 11.
Comments
No comments yet. Start the discussion.