Profiling ASP.NET Core With Stackify Prefix

It is a bit alarming how many developers I see who do not run any type of profiling against their applications. Occasionally I see some selective production profiling or some very thinly sliced use-cases at the end of development. An example of this would be stress testing.

I would argue that if you're not using profiling often and early in development, you are "doing it wrong". Proper profiling not only offers a means of catching errors and exceptions early in dev, it also allows you to have a forward looking view on some performance issues. For example, bad ORM execution plans can be identified up front and resolved before they have a significant impact on your codebase.

Profiling in .NET

There are a couple profiling tools in the .NET arena. One such being Glimpse. Glimpse is awesome on so many levels but as of recent I have been using Stackify Prefix.

View your logs by request, find hidden exceptions, quickly spot suspicious behaviors, view SQL queries, to name a few... Stackify Prefix

For a comparison between the two, check our Stackify's article - Prefix Vs Glimpse For ASP.NET.

That being said, let's get Stackify Prefix up and running on our local development machines.

Install

The way I see it, installing software on a Windows environment these days come in two flavors.

Traditional

  1. Head over to the Stackify Prefix website and download the Prefix installer.
  2. Launch the installer and follow the prompts to complete the install.
    Prefix Installer

Chocolatey

These days if you're not using Chocolatey, you are missing out on a lot.

  1. If you don't already have Chocolatey installed, open up a command prompt with administrative rights and run the following.
    @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
  2. Once Chocolatey is installed, run the following to install Prefix.
    choco install prefix

At this point (depending on your install method) a browser window should automatically open.

Prefix Blank Browser

If the browser did not launch, you can enable Prefix and launch it in a browser from the system tray.

Prefix System Tray

Setting up your project

Now that we have Prefix up and running, it is time to configure our ASP.NET Core project to work with it.

Nuget

Prefix needs to be injected into the pipeline via Middleware. To do this, we need to obtain the Stackify Middleware package by running the following from your Nuget command prompt.

PM> Install-Package StackifyMiddleware

If you are working in Visual Studio, you can use the Nuget Package Manager instead. Search for "StackifyMiddleware".

Prefix in Visual Studio Nuget Package Manager

Service

Now that we have the Stackify Middleware referenced in our project, we need to configure it inside our Startup.Configure method located in the Startup.cs file. When adding Middleware in this method, the request in the ASP.NET Core pipeline passes through Middleware in the order in which your projects Middleware is configured. Because we want our profiling to happen before the request hits the actual MVC Middleware, it has to be setup early on in this function. More specifically, it needs to be setup prior to the MVC Middleware.

In addition, we don't want to be running our request through the Stackify Middleware when in production. That being the case, we also want to ensure our configuration takes into account our environment by using ASP.NET Core Environment Variables.

Following is an example of how I accomplish this.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ServiceMapperConfig.Config();
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    if (env.IsDevelopment())
    {
        app.UseMiddleware<StackifyMiddleware.RequestTracerMiddleware>();
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }
    app.UseStaticFiles(new StaticFileOptions
    {
        // Static File Configuration
    });
    app.UseIdentity();
    app.UseMvc(routes =>
    {
        // Route Configuration
    });
}

Lets Party!

That is it! Super simple setup, right? Let's start profiling.

  1. Enable and launch Prefix in your browser.
  2. Open up your project, compile and start it.
    • If you do this prior to launching Prefix, you will need to restart your project.
  3. Run your first request through the pipeline and watch Prefix work its magic!

Following is an example of a request made for an MVC loaded page that is backed by Entity Framework operations.

Prefix Running

Look at all that awesomeness! Let me know your thoughts below in the comments section.