使用MiniProfiler捕获慢速请求

时间:2017-09-18 10:55:05

标签: c# asp.net .net asp.net-core-mvc miniprofiler

我想使用MiniProfiler在设定的时间限制过后调用一个函数。这就是MiniProfiler的设置方式。在那之后,我已经包含了我们用来描述任何需求分析的分析脚本。我的问题是创建某种中间件,当时间大于1000毫秒时,它可以拦截这个“MiniProfiler.Current.Step”调用。

        app.UseMiniProfiler(new MiniProfilerOptions
        {
            ResultsAuthorize = x => false,
            ResultsListAuthorize = x => false,
            Storage = new SqlServerStorage(Configuration.GetConnectionString("MiniProfiler"))
        });
        MiniProfilerEF6.Initialize();

        app.Use(async (context, next) =>
        {
            MiniProfiler.Current.Name = context.Request.GetDisplayUrl();
            await next.Invoke();
        });

    /// <summary>
    /// The main profiler, useage:
    /// using(this.Profile("more context"))
    /// {
    ///     Do things that needs profiling, and you may nest it.
    /// }
    /// </summary>
    /// <param name="profiled">The object that is profiled</param>
    /// <param name="subSectionName">More context for the output result</param>
    /// <param name="methodName">Possible override for the method name called</param>
    /// <param name="profiledTypeName">Possible override for the profiled type</param>
    /// <returns>An IDisposable, to help with scoping</returns>
    public static IDisposable Profile(this object profiled,
                                      string subSectionName = null,
                                      [CallerMemberName] string methodName = "",
                                      string profiledTypeName = null)
    {
        if (profiled == null)
            throw new ArgumentNullException(nameof(profiled));

        var profiledType = profiledTypeName ?? profiled.GetType().Name;
        return Profile(methodName, profiledType, subSectionName);
    }


    public static IDisposable Profile(string methodName,
                                      string profiledTypeName,
                                      string subSectionName = null)
    {
        var name = subSectionName != null
            ? $"{profiledTypeName}.{methodName}:{subSectionName}"
            : $"{profiledTypeName}.{methodName}";

        return MiniProfiler.Current?.Step(name);
    }

0 个答案:

没有答案