Glimpse - Debugging made easy

Glimpse shows execution timings, server configuration, request data and more, all in browser, with no changes to your application code. Just install the NuGet package, and you're good to go!

20 March 2014
Christoph Keller Christoph Keller

Introduction

Glimpse is a debugging tool for every kind of ASP.NET application. There are plugins available for various systems, including MVC (different versions), ASP.NET, Entity Framework (different versions), ADO.NET. It behaves like a Firebug in Firefox or like any other web-developer tool, with the small difference that it displays all kind of server information.

Give it a try, in my opinion it is worth it!

http://getglimpse.com/

System insight

As far as I have seen, Glimpse works with two modules. First, a HttpModule handles each incoming request and attaches itself onto various system components to grab statistical and informational data. The second module is a HttpHandler (Glimpse.axd) which handles the configuration and ajax callback stuff needed to display the information at the browser.

Also the HttpModule inserts the required html needed to render the toolbar at the bottom of the screen.

Interesting and useful code-snippets

Timeline Stopwatch

After using Glimpse for debugging a performance issue, I was in need of a piece of code to attach custom messages and time measures to the Glimpse timeline output. So I decided to create a small Timeline message handler including a stop-watch.

Here is the code snippet for this:

using System;
using System.Diagnostics;
using System.Web;
using Glimpse.Core.Framework;

namespace CkSoftware.GlimpseHelpers
{
	public class GlimpseTimelineMessage : ITimelineMessage
	{
		private TimeSpan _duration;
		private Guid _id;
		private TimeSpan _offset;
		private DateTime _startTime;

		public GlimpseTimelineMessage(string itemName, string eventName, string eventSubText, TimeSpan duration, TimeSpan offset, DateTime startTime, string color)
		{
			EventCategory = new TimelineCategoryItem(itemName, color, "Yellow");
			EventName = eventName;
			EventSubText = eventSubText;
			_duration = duration;
			_offset = offset;
			_startTime = startTime;
			_id = new Guid();
		}

		public TimeSpan Duration { get { return _duration; }  set { _duration = value; } }
		public TimelineCategoryItem EventCategory { get; set; }
		public string EventName { get; set; }
		public string EventSubText { get; set; }
		public Guid Id { get { return _id; }  set { _id = value; } }
		public TimeSpan Offset { get{ return _offset; } set { _offset = value; } }
		public DateTime StartTime { get { return _startTime; } set { _startTime = value; } }
	}
	
	public class GlimpseTimelineStopWatch : IDisposable
	{
		private readonly string _moduleName;

		private readonly string _action;

		private readonly GlimpseRuntime _glimpse;

		private readonly DateTime _requestStart;

		private readonly TimeSpan _offset;

		private readonly Stopwatch _stopWatch;

		private readonly bool _enabled;

		public GlimpseTimelineStopWatch(string moduleName, string action)
		{
			var glimpseRuntimeObject = HttpContext.Current.Application.Get("__GlimpseRuntime");
			if (glimpseRuntimeObject == null)
			{
				_enabled = false;
			}

			if (_enabled)
			{
				_moduleName = moduleName;
				_action = action;
				_glimpse = (GlimpseRuntime)glimpseRuntimeObject;
				_requestStart = _glimpse.Configuration.TimerStrategy().RequestStart;
				_offset = new TimeSpan((DateTime.Now - _requestStart).Ticks);
				_stopWatch = new Stopwatch();
				_stopWatch.Start();
			}
		}

		public void Dispose()
		{
			if (_enabled)
			{
				_stopWatch.Stop();
				_glimpse.Configuration.MessageBroker.Publish(new GlimpseTimelineMessage(_moduleName, _action, string.Empty, _stopWatch.Elapsed, _offset, _requestStart, "red"));
			}
		}
	}
}

The stop-watch part itself is encapsulated in a IDisposable class, so a easy usage is possible. Here is a short example how you use the GlimpseTimelineStopWatch:

using (new GlimpseTimelineStopWatch("MyModuleName", "Executing some interesting stuff"))
{
    // ... your stuff you want to measure.
}

After executing your code, the timeline should get a new entry showing your module name and action and how long it took to complete.

That's all for the moment, hope this article helps someone :) If you have any questions or comments, please don't hesitate to write a comment or contact my using the contact form.


comments powered by Disqus