A Glimpse into the Gut of your ASP.NET application

Posted by: Suprotim Agarwal , on 7/30/2013, in Category ASP.NET
Views: 66078
Abstract: Today we explore the Glimpse package that helps you look at the internal working of your ASP.NET Web Application, in the browser.

Some of us may have heard or used MiniProfiler a profiling library for your ASP.NET applications, from the StackExchange team. We carried an article on dotnetcurry.com on it. Today we are going to review another OSS project called Glimpse from a team led by Anthony vander Hoorn and Nik Molnar.

Glimpse recently launched v1.4 with a new Heads up Display (HUD) for the profile information!

On the outset, Glimpse looks to have the same or overlapping functionality as MiniProfiler, but after playing with it for about 5 minutes, I realized, Glimpse provides significantly more insight into the happening of your Application than MiniProfiler. So without further ado, let’s dive in and see what Glimpse can do for us.

This article is published from the DNC .NET Magazine – A Free High Quality Digital Magazine for .NET professionals published once every two months. Subscribe to this eMagazine for Free!


Getting Started with Glimpse

To start off with, we wanted a project with some functionality already built in. We thought it would be good to use the same project that we used for our MiniProfiler for a fair comparison. So we went ahead and downloaded the Source code as Zip from the Github Project.

Glimpse is distributed via Nuget packages, so the easiest way to get started with it is to fire up the Manage Nuget Packages dialog and search for ‘Glimpse’.

add-package-reference

Depending on the type of project you want to add Glimpse to, you can select MVC3, MVC4 or in case of WebForms, Glimpse ASP.NET. In our case, I selected Glimpse MVC4, this installs the Glimpse ASP.NET and Glimpse Core packages as well. The following packages get installed




Glimpse Changes under the hood

The Glimpse architecture is based on HttpHandlers and HttpModules. So Glimpse installation basically implies registering the required modules. Let’s look at the web.config changes

New Glimpse section: We have a new Glimpse section for glimpse specific settings.


  …
 



The Handler and Module Registration: Next we have the Glimpse module and the Glimpse.axd registered.


 
   
 

 
   
 




 




The Glimpse Policy section: We will look at policies in a bit. This section is for configuring Glimpse specific settings that include when should Glimpse record data and when not, as well as when should the data be displayed, so on and so forth.





From the configuration point of view, Glimpse is all set, unlike MiniProfiler we don’t have to get it going with a call in the Global.asax. So far zero code required.

Starting Glimpse

We can run the application now and navigate to /Glimpse.axd. This will give us the following page

glimpse-axd

Starting Glimpse is as simple as hitting the ‘Turn Glimpse On’ button.

Now when we navigate back to the home page, we should see the Glimpse HUD at the bottom of the page. If it’s not expanded, click on the ‘g’ logo to expand it.

glimpse-enabled-hud

The HUD

As we can see above, the Heads Up Display panel from Glimpse gives us a quick view of what’s going on with our Request (Glimpse as it’s subtly obvious, works per-HTTP Request). In the panel, we see three sections

1. HTTP: Shows the request took 973 ms to complete, of which 3ms was ‘on the wire’ meaning the time between the client requesting and server receiving the request. Next 22 milliseconds were spent by the server to process the request and finally 948 seconds by the client to receive and render the Server’s response.

2. Host: Shows details of activities on the server. We can see here that it took almost no time to resolve the correct Action and the View rendering took 3 ms. If we hover over it, we’ll see a little more details

glimpse-hud-host

3. AJAX: The final section shows the number of AJAX requests. This page had 0 requests but when we do have AJAX requests, hovering over the AJAX section will show us more details for each AJAX request made.

So that was the HUD for Glimpse and we have gotten off to a good start. Still it looks similar to MiniProfiler. We’ll if MiniProfiler blew you away, Glimpse is going to put you in Orbit!

The Glimpse Panel

image

From the HUD display, if we click on the Glimpse logo at the bottom right corner, the Glimpse panel is launched! This is not a browser plugin rather a dynamically injected CSS/JS panel full or metrics collected about your ASP.NET Application. Blown away yet?

Well, at first glance the data shown looks similar to what the Firebug plugin shows us, but there is a lot more than Firebug. Fact is Glimpse by the virtue of being on the Server and the Client, digs out way more data than Firebug and except for Request information, there is hardly any overlap. Of the default Tabs above, we will look at Configuration, Environment, Routes, Timeline, Trace and Views in details today.

With MVCs convention-over-configuration approach, a lot of times things work ‘magically’ and we are not sure about the exact mechanics. Glimpse serves as a valuable tool to see what is happening behind the scenes by unravelling how the conventions are interpreted.

Configuration

glimpse-panel-configuration

The Configuration ‘tab’ pulls data out of our application’s Web.config as well as Machine.config. Essentially every configuration setting applicable to the app is pulled in. For example, above I see that once upon a time I had installed MySqlServer on my machine and it seems to have registered a provider on my system.

Among other things that it grabs, you can see the Custom Error settings, HTTP Modules and Handlers configured.

Environment

The environment gives us details of the Machine, Web Server, .NET Framework, Process ID as well as Assemblies in use among other things. For the assemblies loaded, it shows, version, culture, whether it’s from GAC and if they Full Trust.

Routes

The Routes tab shows us how the routing framework arrived at the route. Essentially it shows the success path traversed by the routing algorithm. For example in the snapshot below, we can see it tried to match the DefaultApi route, failed, skipped the next one that excludes axds and finally hit the successful route. If you have seen Phil Haack’s Route Debugger, this feature is similar.

glimpse-panel-routes

Timeline

The timeline feature breaks down the time required for the request to complete. There are actually two views, one a hierarchical view as follows

glimpse-panel-timeline-hierarchical

Second view is flattened and it’s a flattened out view. The Categories that are color coded can be unselected to show a filtered list.

What’s more interesting is that you have two markers that can be moved to select a smaller cross section of the categories to zoom-in.

glimpse-panel-timeline-flat

Views

This is probably the most insightful view for someone new to MVC. This shows how MVC selects a particular View. For a single request that has one partial view, we see how MVC queries the WebFormsViewEngine first and then the RazorViewEngine. It first searches the cache and if it doesn’t find it, the cache requests the view from the Disk, in green.

Essentially, the MVC view resolution code walks through all the View engines asking them if they were going to serve up the requested view.

As is the theme for all the tabs, the successful hit is highlighted

glimpse-panel-views

Trace

Finally we come to the Trace tab. This where custom tracing information is displayed. Custom as in explicit Trace statements that you may have writing the application. For example, if we update the Home Controller’s Index action method with the following trace statements:

public ActionResult Index()
{
System.Diagnostics.Trace.Write("Entered Index Action", "HomeController");
System.Diagnostics.Trace.TraceInformation("Adding some Trace Info", "HomeController");
System.Diagnostics.Trace.TraceWarning("Warning: May explode anytime");
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
System.Diagnostics.Trace.TraceError("Boom Boom");
return View();
}

The Trace Tab looks as follows:

glimpse-panel-trace

So essentially all custom tracing info that we put is displayed in this tab, with helpful color coding based on the type of Trace.

The Glimpse Eco-System

Now that we have got a whiff of what Glimpse does, we must be wondering umm, what about tracing DB calls? For such an elaborate profiling system, surely there’s a way to database calls? Well, this is a good time to introduce you to the Glimpse Eco-System

glimpse-hud-plugins

Well this is only a partial list of the Extensions available. As we can see, we have EntityFramework, RavenDB, heck we even have an extension for Miniprofiler.

Profiling Entity Framework Database Calls

To get started with Database Profiling, when you use EF in your Data Layer is to install the correct version of the EF extension. In our case I installed EF5 from the Nuget Package Manager Console as follows:

PM> Install-Package Glimpse.EF5

Once installed, we run the application and navigate to TimeCard index page. We case that a new SQL tab has been added to the Glimpse panel. The following set of queries surprised me.

glimpse-panel-ef-migration

As you can see, there are two additional queries that I didn’t anticipate, on to the INFORMATION_SCHEMA and two to the __MigrationHistory table. These three are strictly EF related, and happen only on first run of the application. If you refresh the page, only the call to TimeCards table will be registered.

Security and Profiles in Glimpse

The information that Glimpse displays is potentially sensitive and should be ‘exposed’ very carefully. Inadvertent exposure of data like Connnection strings etc. can be disastrous from a security perspective. However, Glimpse is safe by default.

The Web.config’s Glimpse section is by default defined as follows:


 
 

The defaultRuntimePolicy=”On” sets up Glimpse to work locally ONLY. To disable this, we have to add an (commented out above) and add the Glimpse.AspNet.Policy.LocalPolicy assembly name.

Custom Policies

However if we want to customize our policies, we could create a Custom Policy file as well. Glimpse very helpfully adds a commented out Class called GlimpseSecurityPolicy. The Execute method in this class receives the HTTP context and we can use the context to determine if Glimpse should be turned on or off for the given request. One example specified is to check if the given User is a part of the Administrator group and if not, turn off Glimpse.

Other Glimpse Features

Glimpse has this neat feature where it can track requesting clients. So if you have enabled Glimpse for multiple clients, you can sit at one browser and monitor the activities of the other browser. This can be ideally used for remote debugging. Combined with a Custom Policy we could create a ‘Debugger’ group in our ACL and add users who are having problems to the Debugger group if they have a problem. Then request them to reproduce the error at their end while you monitor the information thrown up by Glimpse for that user.

In the screenshot below, we have the hypothetical scenario where the second user is on a different machine using Firefox.

glimpse-history

On our browser in IE, we click on the History tab to see there are two clients connected. When we click on Firefox, the data displayed switches to Firefox and we can see what’s going on at the other client.

That completes our round up of the hottest features of Glimpse. At this point, you should be sufficiently equipped to start poking around and inspecting your app with Glimpse.

Conclusion

Glimpse is truly an awesome addition to an ASP.NET developer’s toolbox. It provides deep insights into the working of an ASP.NET application without too many invasive code changes. Thanks to Redgate’s sponsorship, the project is placed well with project leaders devoting 100% of time to it. Over the last two years it has got impressive community support.

Today we primarily looked at the ‘out-of-the-box’ features. Someday in future we can investigate the extension points and how to add our own extensions to Glimpse.

Download the entire source code from our GitHub Repository at bit.ly/dncm7-gli

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Anupam Singh on Wednesday, May 28, 2014 6:51 AM
nice article Suprotim .very informative..