1

一旦设定的时间限制过去,我想使用 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);
    }
4

0 回答 0