asp.net-coreminiprofiler

Asp.net core 2.2 api not able to integrate miniprofiler, dapper and mySql to work


I am trying to get miniprofiler and Dapper on MySql to work together to profile sql statements.

I have managed to get miniprofiler and swagger to work together but the sql generated and executed by dapper are not intercepted for some reason, I believe this is because Miniprofiler.Current always returns null for some reason.

I was wondering if anyone else can help.

sample project is attached. run docker compose and run api manually. enter image description here

enter image description here

test api download => https://gofile.io/?c=Qf6boJ


Solution

  • 1st issue was that app.UseMiniprofiler() needs to come before app.UseMvc

    2nd issue was that the db connection factory must look like this:

     public IDbConnection Create()
            {
                return new StackExchange.Profiling.Data.ProfiledDbConnection(
                    new MySqlConnection(_storageSettings.ConnectionString),
                    MiniProfiler.Current
                );
            }
    

    The packages that I use are as follows:

    <PackageReference Include="Dapper" Version="2.0.30" />
    <PackageReference Include="MySqlConnector" Version="0.61.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.1.0" />