Glimpse中的自定义时间轴项目

时间:2014-04-03 03:48:43

标签: asp.net-mvc-3 profiling glimpse

我刚刚开始寻找MVC3应用程序的分析。

我首先找到了MiniProfiler,然后找到了使用Glimpse的建议。两者看起来都很棒,我宁愿使用Glimpse,但我想在时间轴上添加条目以进行特定操作。

MiniProfiler有一个很好的功能,你可以抓住当前的MiniProfiler上下文并使用using()命令添加一个步骤。 Glimpse有类似的东西吗?

我确实找到了一个有人解释如何操作的条目the long way,但是想知道从那以后是否有更短的方法可以做到这一点。

3 个答案:

答案 0 :(得分:5)

感谢this stackoverflow question,我发现a gist为Glimpse v1实现了等同于Glimpse v2' GlimpseTimeline

来自您的代码电话:

using (Timeline.Capture("FormulaEvaluator.Evalauate"))
{
    // Code to time
}

这里是Timeline.Capture的实现:

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; }
}

答案 1 :(得分:1)

您提到的Google群组主题目前是Glimpse第2版中正在开发的方法。在那里,您可以找到符合您需要的GlimpseTimeline课程。

您可以在等待v2发布时将此类复制到您自己的项目中。请记住,您需要对其进行一些修改,因为MessageBroker等某些内容需要从GlimpseConfiguration.GetConfiguredMessageBroker()等处进行检索...

但它现在可能适合您的需求......

答案 2 :(得分:0)

我从v2制作了droyads gist和GlimpseTimeline的混搭。 GlimpseTimeline v1 gist

public class TimelineMessage : ITimelineMessage
{
    public TimelineMessage()
    {
        Id = Guid.NewGuid();
    }

    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; }
}

public static class GlimpseTimeline
{
    private static readonly TimelineCategoryItem DefaultCategory = new TimelineCategoryItem( "User", "green", "blue" );

    public static OngoingCapture Capture( string eventName )
    {
        return Capture( eventName, null, DefaultCategory, new TimelineMessage() );
    }

    public static OngoingCapture Capture( string eventName, string eventSubText )
    {
        return Capture( eventName, eventSubText, DefaultCategory, new TimelineMessage() );
    }

    internal static OngoingCapture Capture( string eventName, TimelineCategoryItem category )
    {
        return Capture( eventName, null, category, new TimelineMessage() );
    }

    internal static OngoingCapture Capture( string eventName, TimelineCategoryItem category, ITimelineMessage message )
    {
        return Capture( eventName, null, category, message );
    }

    internal static OngoingCapture Capture( string eventName, ITimelineMessage message )
    {
        return Capture( eventName, null, DefaultCategory, message );
    }

    internal static OngoingCapture Capture( string eventName, string eventSubText, TimelineCategoryItem category, ITimelineMessage message )
    {
        if (string.IsNullOrEmpty( eventName ))
        {
            throw new ArgumentNullException( "eventName" );
        }

        #pragma warning disable 618
        var executionTimer = GlimpseConfiguration.GetConfiguredTimerStrategy()();
        var messageBroker = GlimpseConfiguration.GetConfiguredMessageBroker();
        #pragma warning restore 618

        if (executionTimer == null || messageBroker == null)
        {
            return OngoingCapture.Empty();
        }

        return new OngoingCapture( executionTimer, messageBroker, eventName, eventSubText, category, message );
    }

    public static void CaptureMoment( string eventName )
    {
        CaptureMoment( eventName, null, DefaultCategory, new TimelineMessage() );
    }

    public static void CaptureMoment( string eventName, string eventSubText )
    {
        CaptureMoment( eventName, eventSubText, DefaultCategory, new TimelineMessage() );
    }

    internal static void CaptureMoment( string eventName, TimelineCategoryItem category )
    {
        CaptureMoment( eventName, null, category, new TimelineMessage() );
    }

    internal static void CaptureMoment( string eventName, TimelineCategoryItem category, ITimelineMessage message )
    {
        CaptureMoment( eventName, null, category, message );
    }

    internal static void CaptureMoment( string eventName, ITimelineMessage message )
    {
        CaptureMoment( eventName, null, DefaultCategory, message );
    }

    internal static void CaptureMoment( string eventName, string eventSubText, TimelineCategoryItem category, ITimelineMessage message )
    {
        if (string.IsNullOrEmpty( eventName ))
        {
            throw new ArgumentNullException( "eventName" );
        }

        #pragma warning disable 618
        var executionTimer = GlimpseConfiguration.GetConfiguredTimerStrategy()();
        var messageBroker = GlimpseConfiguration.GetConfiguredMessageBroker();
        #pragma warning restore 618

        if (executionTimer == null || messageBroker == null)
        {
            return;
        }

        message
            .AsTimelineMessage( eventName, category, eventSubText )
            .AsTimedMessage( executionTimer.Point() );

        messageBroker.Publish( message );
    }

    public class OngoingCapture : IDisposable
    {
        public static OngoingCapture Empty()
        {
            return new NullOngoingCapture();
        }

        private OngoingCapture()
        {
        }

        public OngoingCapture( IExecutionTimer executionTimer, IMessageBroker messageBroker, string eventName, string eventSubText, TimelineCategoryItem category, ITimelineMessage message )
        {
            Offset = executionTimer.Start();
            ExecutionTimer = executionTimer;
            Message = message.AsTimelineMessage( eventName, category, eventSubText );
            MessageBroker = messageBroker;
        }

        private ITimelineMessage Message { get; set; }

        private TimeSpan Offset { get; set; }

        private IExecutionTimer ExecutionTimer { get; set; }

        private IMessageBroker MessageBroker { get; set; }

        public virtual void Stop()
        {
            var timerResult = ExecutionTimer.Stop( Offset );

            MessageBroker.Publish( Message.AsTimedMessage( timerResult ) );
        }

        public void Dispose()
        {
            Stop();
        }

        private class NullOngoingCapture : OngoingCapture
        {
            public override void Stop()
            {
            }
        }
    }
}