取消

MiniProfiler监控Asp.Net MVC5和EF性能


1. 安装依赖包

在web项目打开nuget包管理器搜索 MiniProfiler.Mvc5和MiniProfiler.EF6安装。

2. 在Global.asax中添加配置代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
protected void Application_Start()
{
    MiniProfiler.Configure(new MiniProfilerOptions
    {
        // Sets up the route to use for MiniProfiler resources:
        // Here, ~/profiler is used for things like /profiler/mini-profiler-includes.js
        RouteBasePath = "~/profiler",

        // Example of using SQLite storage instead
        Storage = new SqliteMiniProfilerStorage(ConnectionString),

        // Different RDBMS have different ways of declaring sql parameters - SQLite can understand inline sql parameters just fine.
        // By default, sql parameters will be displayed.
        //SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter(),

        // These settings are optional and all have defaults, any matching setting specified in .RenderIncludes() will
        // override the application-wide defaults specified here, for example if you had both:
        //    PopupRenderPosition = RenderPosition.Right;
        //    and in the page:
        //    @MiniProfiler.Current.RenderIncludes(position: RenderPosition.Left)
        // ...then the position would be on the left on that page, and on the right (the application default) for anywhere that doesn't
        // specified position in the .RenderIncludes() call.
        PopupRenderPosition = RenderPosition.Right,  // defaults to left
        PopupMaxTracesToShow = 10,                   // defaults to 15

        // ResultsAuthorize (optional - open to all by default):
        // because profiler results can contain sensitive data (e.g. sql queries with parameter values displayed), we
        // can define a function that will authorize clients to see the JSON or full page results.
        // we use it on http://stackoverflow.com to check that the request cookies belong to a valid developer.
        ResultsAuthorize = request => request.IsLocal,

        // ResultsListAuthorize (optional - open to all by default)
        // the list of all sessions in the store is restricted by default, you must return true to allow it
        ResultsListAuthorize = request =>
        {
            // you may implement this if you need to restrict visibility of profiling lists on a per request basis
            return true; // all requests are legit in this example
        },

        // Stack trace settings
        StackMaxLength = 256, // default is 120 characters
        
        // (Optional) You can disable "Connection Open()", "Connection Close()" (and async variant) tracking.
        // (defaults to true, and connection opening/closing is tracked)
        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();
}

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!
} 

3. 在web.config中添加js配置

1
<add name="MiniProfiler" path="profiler/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />

4. 在需要的页面添加显示监控代码(所有页面可以在layout.cshtml中添加)

4.1 添加命名空间

@using StackExchange.Profiling;

4.2 在body块最后添加显示代码

@MiniProfiler.Current.RenderIncludes(position: RenderPosition.Right, showTrivial: false, showTimeWithChildren: true)

本文会经常更新,请阅读原文: https://dashenxian.github.io/post/MiniProfiler%E7%9B%91%E6%8E%A7Asp.Net-MVC5%E5%92%8CEF%E6%80%A7%E8%83%BD ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 小神仙 (包含链接: https://dashenxian.github.io ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 (125880321@qq.com)

登录 GitHub 账号进行评论