the next door (so called) geek @rakkimk | your next door geek | friend | blogs mostly on technology, and gadgets.

Extending Glimpse for ASP.NET to include custom Timeline messages

23. August 2013 20:15 by the next door (so called) geek in ASP.NET, Glimpse

If you are debugging any ASP.NET MVC or WebForms, and you haven’t used Glimpse yet, you are missing big! This also has nice extensions to other frameworks like EF, nHibernate, etc. This collects the server side data, and presents an overlay in the client side. Information such as the configuration, environment details, request details, server variables, traces, and most importantly the timeline of events. I’ll let you explore more about Glimpse, and it’s features from their website. Here is an example of how the timeline looks for a sample MVC4 application:

image

Adding your own message in the timeline seems to be a feature that’s getting worked upon by the Glimpse team. Until they release an easier way, here is what you can do to get your custom messages in the timeline. Here are the easy steps.

  • You need to create a new Class deriving from Glimpse.Core.Messaging.ITimelineMessage, and implement it’s properties, and add a new constructor which you will call from your code later to initialize a message to be added to the Glimpse timeline for the request.
  • Grab the Glimpse Runtime in the code.
  • Measure the time between your calls.
  • Publish the message via GlimpseRuntime.Configuration.MessageBroker.Publish<T>(message) method.

Here is the sample code for the above steps:

// grab the glimpse runtime
Glimpse.Core.Framework.GlimpseRuntime gr = (Glimpse.Core.Framework.GlimpseRuntime)HttpContext.Application.Get("__GlimpseRuntime");
 
// get hold of the timespan offset
TimeSpan t1 = new TimeSpan((DateTime.Now - gr.Configuration.TimerStrategy().RequestStart).Ticks);
 
// start a stopwatch
Stopwatch sw = new Stopwatch();
sw.Start();
 
// your function that you want to measure the time
System.Threading.Thread.Sleep(1000);
 
// stop the stopwatch, and get the elapsed timespan
sw.Stop();
TimeSpan t2 = sw.Elapsed;
 
// create the message with grouping text as appropriate
MyTimelineMessage msg1 = new MyTimelineMessage("WebService Calls", "first call to fetch x", "something here too", t2, t1, gr.Configuration.TimerStrategy().RequestStart, "red");
// publish the message to appear in the glimpse timeline
gr.Configuration.MessageBroker.Publish<MyTimelineMessage>(msg1);
 

Once this message is published, this will appear like below (depending on when you have called this in the request processing):

image

Here is my sample MyTimelineMessage.cs that you can use:

using Glimpse.Core.Message;
using System;
using System.Collections.Generic;
 
/// <summary>
/// Summary description for MyTimelineMessage
/// </summary>
public class MyTimelineMessage : ITimelineMessage
{
    TimelineCategoryItem _timeLineCategoryItem;
string _eventName, _eventSubText;
    TimeSpan _duration, _offset;
    DateTime _startTime;
    Guid _id;
public MyTimelineMessage(string itemName, string eventName, string eventSubText, TimeSpan duration, TimeSpan offset, DateTime startTime, string color)
    {
        _timeLineCategoryItem = new TimelineCategoryItem(itemName, color, "Yellow");
        _eventName = eventName;
        _eventSubText = eventSubText;
        _duration = duration;
        _offset = offset;
        _startTime = startTime;
        _id = new Guid();
    }
 
public TimelineCategoryItem EventCategory
    {
        get { return _timeLineCategoryItem; }
        set { _timeLineCategoryItem = value; }
    }
 
public string EventName
    {
        get { return _eventName; }
        set { _eventName = value; }
    }
 
public string EventSubText
    {
        get { return _eventSubText;  }
        set { _eventSubText = value; }
    }
 
public TimeSpan Duration
    {
        get { return _duration; }
        set { _duration = value; }
    }
 
public TimeSpan Offset
    {
        get { return _offset; }
        set { _offset = value; }
    }
 
public DateTime StartTime
    {
        get { return _startTime; }
        set { _startTime = value; }
    }
 
public Guid Id
    {
        get { return _id; }
        set { _id = value; }
    }
}

Hope this helps!