Scott Hanselman

NuGet Package of the Week #5 - Debugging ASP.NET MVC applications with Glimpse

April 20, 2011 Comment on this post [19] Posted in ASP.NET | ASP.NET MVC | NuGet | NuGetPOW
Sponsored By

Hey, have you implemented the NuGet Action Plan? Get on it, it'll take only 5 minutes: NuGet Action Plan - Upgrade to 1.2, Setup Automatic Updates, Get NuGet Package Explorer. NuGet 1.2 is out, so make sure you're set to automatically update!

The Backstory: I was thinking since the NuGet .NET package management site is starting to fill up that I should start looking for gems (no pun intended) in there. You know, really useful stuff that folks might otherwise not find. I'll look for mostly open source projects, ones I think are really useful. I'll look at how they built their NuGet packages, if there's anything interesting about the way the designed the out of the box experience (and anything they could do to make it better) as well as what the package itself does.

A Client Side Glimpse into your Server

Glimpse is like the FireBug client side debugger, except it's implemented in JavaScript on the client side with hooks in to ASP.NET on the Server Side.

image

Glimpse includes not only better Route Debugging support than Phil Haack's original Route Debugger, but also excellent support for the little used but totally useful Trace.Write().

Let's just see it in action. You can do this with me now, in less than 5 minutes.

From a new ASP.NET MVC application (Or WebForms, for that matter), install Glimpse.

Add Library Package Reference

Before I run my app, I'm going to sprinkle in some Trace.Write and Trace.TraceError just to make the point.

public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
Trace.Write("Hey, this is nice");
Trace.TraceWarning("Oh, this could be bad.");
return View();
}

public ActionResult About()
{
Trace.TraceError("Oh, no! That's bad");
return View();
}

Now, I'll run my app. I can turn Glimpse on with a Bookmarklet, but I like visiting http://localhost:portname/Glimpse/Config and pressing the big "Turn Glimpse On" button. I'll drag the bookmarklet onto my toolbar one day.

 

Glimpse Config

Next, when I visit my site, notice the Glimpse control panel at the bottom! Insane. It's NOT a browser plugin, Dear Reader, that's all JavaScript.

Gimpse DIV up on the default ASP.NET MVC page

*Mind blown* see my Server-side Trace information in the Trace tab?  Isn't that nicer than Trace.axd?

If I switch to the Routes tab, I can see which Route matched my /Home/About request.

Using Glimpse to Debug Routes

Here's the Execution tab of Glimpse. I can see the methods that were executed, including those that weren't. Insane.

The Execution Tab of Glimpse

Glimpse, along with ELMAH,  is officially my favorite add-on to ASP.NET MVC. I'll be using it every day and I recommend you do as well.

I'm only touching the surface of what Glimpse can do, but I'll leave that extended tour their great site at http://www.getglimpse.com.

Enjoy.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
April 20, 2011 4:04
Scott, thank you for yet another little slice of awesome. My chocolatey goodness meter has actually just become self aware.
April 20, 2011 10:35
How do I secure the Gravitar content so it's available to any admin user on my site, but can't be enabled by the casual passer-by or the hacker intent on inflicting pain on my server?
Rob
April 20, 2011 10:36
... that'll learn me from hitting send so fast. The question has nothing to do with gravitar, but rather with Glimpse. A corrected comment:

How do I secure the Glimpse content so it's available to any admin user on my site, but can't be enabled by the casual passer-by or the hacker intent on inflicting pain on my server?
Rob
April 20, 2011 11:15
I agree with Brian, this looks pretty awesome; thanks for pointing this one out.
April 20, 2011 12:27
@ BrianHartung

How did you get your meter to become self aware, mine talks back to me and swears a lot, much like my other half (which I put down to lack-of-coffee-coding-practices).

Thanks Scott, this will be very useful (I am assuming it works with all flavours of ASP.NET?)

Adios!
April 20, 2011 14:11
RE security... Currently you can turn glimpse on or off. Hence in a production scenario its not something that you would forced to have on all the time. Secondly, built into the system at the moment is the ability to filter by IP addresses - not that IP filtering is totally fool proof but it was easy to implement. Lastly, even though its not really security related but more just for clarity, you need to have the cookie present to see any data, hence casual users wouldn't be seeing your data.

Beyond this we have plans for a pluggable security pipeline. We thought about doing more out of the box, but there are just so many different ways we could go, we plans to do the following. The idea would be using MEF (thanks @gblock) you would simply implement a basic glimpse security interface (i.e. has 1 method like bool CanAccessGlimpse();), add the appropriate export that attribute and from that point you would be able to have what ever logic you wanted (i.e. only people who are logged in and are in the developer group can access glimpse).

We don't yet have a time frame on this as its not really a feature people have been asking for short term. But if this is something that you think is required please give us feedback.

Cheers Anthony
April 20, 2011 17:17
WOW.
I wish it had existed when I was doing some MVC. And now that it exists, I wish I was doing some MVC...
April 27, 2011 23:51
This is sweet! Thanks
May 20, 2011 21:29
I think it is worth mentioning that glimpse requires jquery to be included in your page. There is nothing about that in the documentation.
May 24, 2011 22:16
@adam The dependency that Glimpse has on jQuery has been fixed and wont be an issue come release 0.82. It was never fully documented because we didn't want people to have to worry about it. But now, if you don't use jQuery or you are using a newer/older version of jQuery to what we have built on its not an issue. It will work seamlessly.
May 25, 2011 20:30
Its a great featured tool !
June 08, 2011 20:15
Looks like a great addition for development.

It's probably something that I'd like in a development environment but not perhaps being published to a production server(Security).

If i just didn't deploy the glimpse dlls in production do you see this causing any problems? Are there any hooks in code etc which might break?
Zac
June 16, 2011 23:17
Nope, they won't be loaded. You'll want to remove them from production web.config though.
July 05, 2011 5:04
You really should mention you need to Install-Package Glimpse.Mvc3 to see the Route and other mvc related tabs. Took me 10 minutes to figure out what was going on :)
September 06, 2011 21:41
I ran into an issue installing Glimpse 0.85:

Successfully installed 'Glimpse 0.85'.
Successfully uninstalled 'Glimpse 0.85'.
Install failed. Rolling back...
Install-Package : Unable to find assembly references that are compatible with the target framework '.NETFramework,Version=v3.5'.
At line:1 char:16
+ Install-Package <<<< Glimpse

Do I have to be running .NET 4.0 to run Glimpse? Seems odd that I would need that.
September 06, 2011 21:56
Ah, I see now: Glimpse only works with .NET 4.0.
October 04, 2011 16:10
If I already use, let's say, NLog for logging, do I need to duplicate the trace calls?

I haven't looked at the Glimpse code, but is it extensible so that a NLog target can be developed?
November 15, 2011 12:47
It is very useful to us.
December 01, 2011 21:06
Scott, gotta say this saved me from going crazy when I was trying to debug my custom route entries. You just earned 1000 brownie points in my diary :)

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.