代码改变世界

MiniProfiler在MVC5下的使用记录

2021-03-10 08:20  景裔  阅读(109)  评论(0编辑  收藏  举报

1、Nuget安装MiniProfiler.MVC5

2、web.config的handlers中添加<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />

3、Application_Start中增加

            MiniProfiler.Configure(new MiniProfilerOptions
            {
                Storage = new MultiStorageProvider(new MemoryCacheStorage(new TimeSpan(1, 0, 0))),

                PopupRenderPosition = RenderPosition.Left,  // defaults to left
                PopupMaxTracesToShow = 10,                   // defaults to 15

                ResultsAuthorize = request =>
                {
                    if ("/home/resultsauthorization".Equals(request.Url.LocalPath, StringComparison.OrdinalIgnoreCase))
                    {
                        return (request.Url.Query).IndexOf("isauthorized", StringComparison.OrdinalIgnoreCase) >= 0;
                    }

                    return true;
                },

                ResultsListAuthorize = request =>
                {
                    return true;
                },

                StackMaxLength = 256,
                TrackConnectionOpenClose = true

            }
            // Optional settings to control the stack trace output in the details pane, examples:
            .ExcludeType("SessionFactory")  // Ignore any class with the name of SessionFactory)
            .ExcludeAssembly("NHibernate")  // Ignore any assembly named NHibernate
            .ExcludeMethod("Flush")         // Ignore any method with the name of Flush
            .AddViewProfiling()              // Add MVC view profiling (you want this)
                                             // If using EntityFrameworkCore, here's where it'd go.
                                             // .AddEntityFramework()        // Extension method in the MiniProfiler.EntityFrameworkCore package
            );

            // If we're using EntityFramework 6, here's where it'd go.
            // This is in the MiniProfiler.EF6 NuGet package.
            // MiniProfilerEF6.Initialize();

4、另增加Application_BeginRequest、Application_EndRequest,分别为:

        protected void Application_BeginRequest()
        {
            // You can decide whether to profile here, or it can be done in ActionFilters, etc.
            // We're doing it here so profiling happens ASAP to account for as much time as possible.
            if (Request.IsLocal) // Example of conditional profiling, you could just call MiniProfiler.StartNew();
            {
                MiniProfiler.StartNew();
            }
        }

        protected void Application_EndRequest()
        {
            MiniProfiler.Current?.Stop(); // Be sure to stop the profiler!
        }

5、Views中的_Layout.cshtml(也可是其它文件)内容中增加

@using StackExchange.Profiling; //文件头部
@MiniProfiler.Current.RenderIncludes(position: RenderPosition.Right, showTrivial: false, showTimeWithChildren: true) //body中

6、如果需要统计细分步骤的时工,在Action中根据需要添加:

MiniProfiler.Current.Step("阶段描述文字");

7、访问页面效果