Created
January 7, 2014 00:54
-
-
Save droyad/8292852 to your computer and use it in GitHub Desktop.
Add to Glimpse Timeline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (Timeline.Capture("FormulaEvaluator.Evalauate")) | |
{ | |
// Code to time | |
} | |
public static class Timeline | |
{ | |
public static IDisposable Capture(string eventName) | |
{ | |
#pragma warning disable 618 | |
var timer = GlimpseConfiguration.GetConfiguredTimerStrategy()(); | |
if (timer == null) | |
return null; | |
var broker = GlimpseConfiguration.GetConfiguredMessageBroker(); | |
if (broker == null) | |
return null; | |
#pragma warning restore 618 | |
return new TimelineCapture(timer, broker, eventName); | |
} | |
} | |
public class TimelineCapture : IDisposable | |
{ | |
private readonly string _eventName; | |
private readonly IExecutionTimer _timer; | |
private readonly IMessageBroker _broker; | |
private readonly TimeSpan _startOffset; | |
public TimelineCapture(IExecutionTimer timer, IMessageBroker broker, string eventName) | |
{ | |
_timer = timer; | |
_broker = broker; | |
_eventName = eventName; | |
_startOffset = _timer.Start(); | |
} | |
public void Dispose() | |
{ | |
_broker.Publish(new TimelineMessage(_eventName, _timer.Stop(_startOffset))); | |
} | |
} | |
public class TimelineMessage : ITimelineMessage | |
{ | |
private static readonly TimelineCategoryItem DefaultCategory = new TimelineCategoryItem("MyApp", "green", "blue"); | |
public TimelineMessage(string eventName, TimerResult result) | |
{ | |
Id = Guid.NewGuid(); | |
EventName = eventName; | |
EventCategory = DefaultCategory; | |
Offset = result.Offset; | |
StartTime = result.StartTime; | |
Duration = result.Duration; | |
} | |
public Guid Id { get; private set; } | |
public TimeSpan Offset { get; set; } | |
public TimeSpan Duration { get; set; } | |
public DateTime StartTime { get; set; } | |
public string EventName { get; set; } | |
public TimelineCategoryItem EventCategory { get; set; } | |
public string EventSubText { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great Gist, saved my day :)
Had hard time configuring MiniProfiler with WebForms.. this works similar way.