A Professional ASP.NET Core API - MiniProfiler

MiniProfiler is a library and UI for profiling your application. By letting you see where your time is spent, which queries are run, and any other custom timings you want to add, MiniProfiler helps you debug issues and optimize performance.

Install the below package

1
2
3
Install-Package MiniProfiler.AspNetCore.Mvc -Version 4.2.1
dotnet add package MiniProfiler.AspNetCore.Mvc --version 4.2.1
<PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.2.1" />

ASP.NET Core

Edit your Startup.cs to add the middleware and configure options:

1
2
3
4
5
6
7
8
9
10
11
12
// Startup.ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
// The services.AddMemoryCache(); code is required
// There is a bug in MiniProfiler, if we have not configured MemoryCache, it will fail.
services.AddMemoryCache();
// HERE
services.AddMiniProfiler(options => options.RouteBasePath = "/profiler");

services.AddControllers();
}

Once, you configure the RouteBasePath property, We are able access to

  • /profiler/results-index: Get list of all requests.
  • /profiler/results: Get the current request.
  • /profiler/results-list: Get list of all requests as JSON.

Next we need add the MiniProfiler middleware, You can do like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Startup.Configure

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();

// HERE
app.UseMiniProfiler();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}

Custom profiling

If you want to your own code profiling you can use the MiniProfiler.Current object, like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public IActionResult Put([FromRoute]int id, [FromBody]WeatherForecast weatherForecast)
{
// HERE
using (MiniProfiler.Current.Step("PUT method for WeatherForecast controller"))
{
WeatherForecast weatherForecastById = null;
using (MiniProfiler.Current.Step("Getting Weather Forecase for the Id"))
{
weatherForecastById = GetWeatherForecast(id);
}

if (weatherForecastById == null)
{
return NotFound();
}

if (weatherForecastById.Id != id)
{
return BadRequest();
}
using (MiniProfiler.Current.Step("Updating the Data"))
{
_databaseContext.Entry(weatherForecast).State = EntityState.Modified;
_databaseContext.SaveChanges();
}
return NoContent();
}
}

Entity Framework Core

Hooking up profiling to Entity Framework Core is easy to do:

Install the below package

1
2
3
Install-Package MiniProfiler.EntityFrameworkCore -Version 4.2.1
dotnet add package MiniProfiler.EntityFrameworkCore --version 4.2.1
<PackageReference Include="MiniProfiler.EntityFrameworkCore" Version="4.2.1" />

And, In your Startup.cs, call AddEntityFramework():

1
2
3
4
5
6
7
8
9
10
11
// Startup.ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddMiniProfiler(options => options.RouteBasePath = "/profiler")
.AddEntityFramework(); // HERE

services.AddEntityFrameworkSqlite().AddDbContext<DatabaseContext>();
services.AddControllers();
}

Reference(s)

Most of the information in this article has gathered from various references.