欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

.NETMVC5使用MiniProfiler

程序员文章站 2024-01-12 18:37:22
...

一、工具-NuGet包管理器-管理解决方案的NuGet程序包

.NETMVC5使用MiniProfiler

二、Global.asax.cs

protected void Application_Start()
{
    InitProfilerSettings();
}

protected void Application_BeginRequest()
{

    MiniProfiler profiler = null;
    profiler = MiniProfiler.StartNew();
}

protected void Application_EndRequest()
{
    MiniProfiler.Current?.Stop();
}

private void InitProfilerSettings()
{
  
    MiniProfilerOptions MiniProfilerOptions = 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 !DisableProfilingResults;
        },

        ResultsListAuthorize = request =>
        {
          return true;
        },

        StackMaxLength = 256, 
        TrackConnectionOpenClose = true
    }

    .ExcludeType("SessionFactory") 
    .ExcludeAssembly("NHibernate") 
    .ExcludeMethod("Flush");

    MiniProfilerOptions.IgnorePath("/__browserLink");//忽略browserLink
    MiniProfilerOptions.ShowControls = true; // 提示条上显示“m”和“c”
    MiniProfiler.Configure(MiniProfilerOptions);

    MiniProfilerEF6.Initialize();
}

三、页面中

@using StackExchange.Profiling
<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>

@MiniProfiler.Current.RenderIncludes(position: RenderPosition.Right, showTrivial: false, showTimeWithChildren: true)
</body>
</html>

四、webconfig.xml

<system.webServer>
    <modules runAllManagedModulesForAllRequests="false" />
    <handlers>
      <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
    </handlers>
</system.webServer>

备注:

1、监控不到Z.EntityFramework.Plus.EF6生成的SQL,也许有其他方法实现,你可以深入探索。

2、使用方法见官网https://miniprofiler.com/dotnet

相关标签: MiniProfiler