Joydip Kanjilal
Contributor

How to use Glimpse in ASP.Net Core

how-to
Mar 18, 20195 mins
C#Microsoft .NETSoftware Development

Take advantage of the Glimpse debugging and diagnostics tool to gain insight into the performance of your ASP.Net Core web application

Glimpse is a popular, open-source web debugging and diagnostics tool that can be used to gain visibility into the performance of your ASP.Net or ASP.Net MVC applications. You can also integrate Glimpse with Application Insights.

Glimpse provides an intuitive user interface that helps you examine the performance data of an application. Like MiniProfiler, Glimpse adds a widget to your web pages so that you can view performance data as you browse through the web pages of your application. You can read my article on MiniProfiler here

This article presents a discussion of how we can integrate Glimpse into an ASP.Net Core application.

Create an ASP.Net Core Web API project

First off, let’s create an ASP.Net Core Web API project. We’ll use this project to integrate Glimpse later in this article. If Visual Studio 2017 is up and running in your system, follow the steps given below to create a new ASP.Net Core Web API project in Visual Studio.

  1. Launch the Visual Studio 2017 IDE.
  2. Click on File > New > Project.
  3. Select “ASP.Net Web Application (.Net Framework)” from the list of the templates displayed.
  4. Specify a name for the project.
  5. Click OK to save the project.
  6. A new window “New ASP.Net Web Application…” is shown next.
  7. Select “Web API” as the project template. 
  8. Ensure that the check boxes “Enable Docker Compose Support” and “Add unit tests” are unchecked as we won’t be using either of these here.
  9. Ensure that “No Authentication” is selected as we won’t be using authentication either.
  10. Click OK. 

This will create a new ASP.Net Core Web API project in Visual Studio. Next, to work with Glimpse in ASP.Net Core, we will need to install Glimpse in our project. There are two ways to install Glimpse in your application, i.e., either by downloading Glimpse from www.getglimpse.com or via NuGet. We’ll use NuGet.

To install the Glimpse package in your application via NuGet, right-click on the project in the Solution Explorer window and select “Manage NuGet Packages…”. Then search for Glimpse.Mvc5 and click the Install button.

asp.net core glimpse nuget IDG

To install the Glimpse package, right-click on the project in the Solution Explorer window and select “Manage NuGet Packages…”. Then search for Glimpse.Mvc5 and click the Install button.

Start the ASP.Net Core application and turn on Glimpse

Once Glimpse has been installed and configured in your application, you can start exploring it. To enable Glimpse, start the application, go to the URL host/Glimpse.axd, and click on the “Turn Glimpse On” button in the top-right corner of the page (see figure below).

asp.net core turn glimpse on IDG

Once Glimpse has been turned on, you will see a “g” icon toward the bottom-right of the home page of your application. You’ll also see a Glimpse bar at the bottom of the web page. When you click on the “g” icon, you can see the execution details as shown in the figure below. 

asp.net core glimpse execution details IDG

View details of an application in Glimpse tabs

Glimpse contains several tabs that are used to show the details of the application, i.e., the routes that have been registered, connection strings being used, query strings being used, and much more. These tabs include the following:

  • Configuration — provides information about the machine configuration
  • Environment — provides information about the server that handles the request
  • Execution — shows execution details, e.g., the time taken for the request
  • Metadata — shows information related to the controller, action, and other metadata
  • Model Binding — shows model binding information (if model binding is used by your application)
  • Request — shows the exact data received by the server
  • Routes — shows the routes that have been registered
  • Server — shows the HTTP variables and their values
  • Session — shows session data (if session is enabled for your application)
  • Timeline — shows a timeline of the method calls that have been made in the server to render the web page
  • Trace — shows any trace information specified in your application
  • Views — shows information on the views and the view engines

Add Glimpse to your ASP.Net Core application

Once you have installed Glimpse in your ASP.Net Core project, you can write the following code in the ConfigureService method of Startup class to add Glimpse to the pipeline.

public void ConfigureServices(IServiceCollection services)
     {
        if (this.HostingEnvironment.IsDevelopment())
        {
            services.AddGlimpse();
        }
     }

Next, you should configure Glimpse in the Configure method of the Startup class as shown below.

     public void Configure(IApplicationBuilder
     applicationBuilder, ILoggerFactory loggerFactory)
     {
        if (this.HostingEnvironment.IsDevelopment())
        {
            applicationBuilder.UseGlimpse();
        }
     }

You should add a reference to the Glimpse assembly in the Startup clas. Note that both the ConfigureServices and Configure methods are called by the runtime. Here is the complete code of the Startup class for your reference.

  public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }
        public void ConfigureServices
        (IServiceCollection services)
        {
            services.AddGlimpse();
            services.AddMvc().SetCompatibilityVersion
            (CompatibilityVersion.Version_2_2);
        }
        public void Configure(IApplicationBuilder app,
        IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseGlimpse();
            }
            app.UseMvc();
        }
    }

Glimpse is a free, open-source diagnostics platform that provides a great deal of performance and diagnostics information on your ASP.Net or ASP.Net Core applications. Glimpse can inspect web requests and give you insights and tooling to make debugging much easier. 

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author