asp.net-mvc-3 - MVC 迷你分析器 - 手动添加步骤

标签 asp.net-mvc-3 mvc-mini-profiler

我有很多小操作(每页 10-30 个),并且它们是递归调用的一部分,因此我无法轻松地将它们算作分析器中的单个步骤。当我只需要总数时,我得到 30 个简短且不同的计数器。

是否有一种简单直接的方法可以手动向迷你分析器添加步骤?或者让它总结同名的步骤?

此外,是否有可能以某种方式将文本信息放在那里,而不仅仅是时间?

最佳答案

我也需要这个,所以我做了这个小东西。它运作得很好。 MiniProfiler 中的许多有用属性都是内部的或类似的,因此一些代码是从 MiniProfiler 源中复制的。另请注意,由于它适用于内部循环,因此不会舍入为 1/10 毫秒。

public class GroupTiming : IDisposable
{
    private Timing _timing;
    private MiniProfiler _profiler;
    private readonly Stopwatch _sw;
    private Decimal? _previousDuration;

    public GroupTiming( MiniProfiler profiler, Timing timing )
    {
        _timing = timing;
        _previousDuration = _timing.DurationMilliseconds ?? 0;
        _timing.DurationMilliseconds = null;
        _profiler = profiler;
        _profiler.Head = _timing;
        _sw = new Stopwatch();
        _sw.Start();
    }

    public void Dispose()
    {
        _timing.DurationMilliseconds = _previousDuration + ((decimal)(1000 * _sw.ElapsedTicks) / Stopwatch.Frequency);
        _profiler.Head = _timing.ParentTiming;
    }
}

public static class MiniProfilerExtensions
{
    public static IDisposable StepGroup( this MiniProfiler profiler, string name )
    {
        return profiler == null ? null : profiler.StepGroupImpl( name );
    }

    private static IDisposable StepGroupImpl( this MiniProfiler profiler, string name )
    {
        Timing timing = null;

        if( profiler.Head.Children != null )
        {
            timing = profiler.Head.Children.SingleOrDefault( _ => _.Name == name );
        }

        if( timing == null )
        {
            using( timing = new Timing( profiler, profiler.Head, name ) ) { }
        }

        return new GroupTiming( profiler, timing );
    }
}

关于asp.net-mvc-3 - MVC 迷你分析器 - 手动添加步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9757591/

相关文章:

c# - MiniProfiler 结果 UI 显示空名称,因为使用了 SqlServerStorage

asp.net - "Unable to determine the provider name"mvc-mini-profiler 1.9 错误

c# - MvcMiniProfiler 结果请求在 Asp.Net MVC 应用程序中给出 404

c# - 仅对特定用户和角色启用 mini profiler

c# - 如何使用knockout js指定name属性

javascript - 单元测试 MVC3 客户端功能

asp.net-mvc-3 - ASP.NET MVC ActionFilter-确定是否AJAX请求

jquery - 使用 ASP MVC 和 jquery 不显眼的验证处理 textarea 元素中的换行符

html - 为特定浏览器创建布局

webforms - MiniProfiler 和 WebForms