MiniProfiler for .NET Documentation

ASP.NET Core

If you’d rather learn by example, sample apps are available. The ASP.NET Core sample is here, with the important bits in Startup.cs.

Installation and Configuration

  • Install the NuGet Package: MiniProfiler.AspNetCore.Mvc
    • Either use the NuGet UI to install MiniProfiler.AspNetCore.Mvc (which has all needed dependencies)
    • Or use the Package Manager Console:
Install-Package MiniProfiler.AspNetCore.Mvc -IncludePrerelease
  • Edit your Startup.cs to add the middleware and configure options:
public void ConfigureServices(IServiceCollection services)
{
    // ...existing configuration...
    
    // Note .AddMiniProfiler() returns a IMiniProfilerBuilder for easy intellisense
    services.AddMiniProfiler();

    // Make sure you have memory cache available unless you're using another storage provider
    services.AddMemoryCache();
}
  • Configure MiniProfiler with the options you want:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IMemoryCache cache)
{
    // ...existing configuration...

    app.UseMiniProfiler(new MiniProfilerOptions
    {
        // Path to use for profiler URLs
        RouteBasePath = "~/profiler",

        // (Optional) Control which SQL formatter to use
        // (default is no formatter)
        SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter(),

        // (Optional) Control storage
        // (default is 30 minutes in MemoryCacheStorage)
        Storage = new MemoryCacheStorage(cache, TimeSpan.FromMinutes(60)),

        // (Optional)  To control which requests are profiled, use the Func<HttpRequest, bool> option:
        // (default is everything should be profiled)
        ShouldProfile = request => MyShouldThisBeProfiledFunction(request),

        // (Optional) To control authorization, you can use the Func<HttpRequest, bool> options:
        // (default is everyone can access profilers)
        ResultsAuthorize = request => MyGetUserFunction(request).CanSeeMiniProfiler,
        ResultsListAuthorize = request => MyGetUserFunction(request).CanSeeMiniProfiler,

        // (Optional)  Profiles are stored under a user ID, function to get it:
        // (default is null, since above methods don't use it by default)
        UserIdProvider =  request => MyGetUserIdFunction(request),

        // (Optional) Swap out the entire profiler provider, if you want
        // (default handles async and works fine for almost all appliations)
        ProfilerProvider = new MyProfilerProvider(),
    });
}

Note: most of the above are optional. A config can be as minimal as app.UseMiniProfiler(new MiniProfilerOptions()));

  • Add Tag Helpers in _ViewImports.cshtml:
@addTagHelper *, MiniProfiler.AspNetCore.Mvc
  • Add MiniProfiler to your master layout (Shared/_Layout.cshtml by default):
<mini-profiler />

Note: <mini-profiler> has many options like max-traces, position, etc. You can find them in code here.
Note #2: The above tag helper registration may go away in future versions of ASP.NET Core, they’re wokring on smoother alternatives here.

Profiling

Now you’re ready to profile. In addition to the usual using wrap method for profiling sections of code, ASP.NET Core includes a tag helper you can use in views like this:

<profile name="My Profiling Step via a <profile> Tag">
    @{ SomethingExpnsive(); }
    <span>Hello Mars!</span>
</profile>