MiniProfiler .NET 文档

如果您更愿意通过示例进行学习,可以使用示例应用程序。ASP.NET Core 示例在此处,其中包含Startup.cs中的重要部分。

安装和配置

  • 安装 NuGet 包:MiniProfiler.AspNetCore.Mvc
    • 使用 NuGet UI 进行安装MiniProfiler.AspNetCore.Mvc(具有所有需要的依赖项)
    • 或者使用包管理器控制台:Install-Package MiniProfiler.AspNetCore.Mvc -IncludePrerelease
  • 编辑您Startup.cs以添加中间件并配置选项:

    1. public void ConfigureServices(IServiceCollection services)
    2. {
    3. // ...existing configuration...
    4. // Note .AddMiniProfiler() returns a IMiniProfilerBuilder for easy intellisense
    5. services.AddMiniProfiler(options =>
    6. {
    7. // All of this is optional. You can simply call .AddMiniProfiler() for all defaults
    8. // (Optional) Path to use for profiler URLs, default is /mini-profiler-resources
    9. options.RouteBasePath = "/profiler";
    10. // (Optional) Control storage
    11. // (default is 30 minutes in MemoryCacheStorage)
    12. // Note: MiniProfiler will not work if a SizeLimit is set on MemoryCache!
    13. // See: https://github.com/MiniProfiler/dotnet/issues/501 for details
    14. (options.Storage as MemoryCacheStorage).CacheDuration = TimeSpan.FromMinutes(60);
    15. // (Optional) Control which SQL formatter to use, InlineFormatter is the default
    16. options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();
    17. // (Optional) To control authorization, you can use the Func<HttpRequest, bool> options:
    18. // (default is everyone can access profilers)
    19. options.ResultsAuthorize = request => MyGetUserFunction(request).CanSeeMiniProfiler;
    20. options.ResultsListAuthorize = request => MyGetUserFunction(request).CanSeeMiniProfiler;
    21. // Or, there are async versions available:
    22. options.ResultsAuthorizeAsync = async request => (await MyGetUserFunctionAsync(request)).CanSeeMiniProfiler;
    23. options.ResultsAuthorizeListAsync = async request => (await MyGetUserFunctionAsync(request)).CanSeeMiniProfilerLists;
    24. // (Optional) To control which requests are profiled, use the Func<HttpRequest, bool> option:
    25. // (default is everything should be profiled)
    26. options.ShouldProfile = request => MyShouldThisBeProfiledFunction(request);
    27. // (Optional) Profiles are stored under a user ID, function to get it:
    28. // (default is null, since above methods don't use it by default)
    29. options.UserIdProvider = request => MyGetUserIdFunction(request);
    30. // (Optional) Swap out the entire profiler provider, if you want
    31. // (default handles async and works fine for almost all applications)
    32. options.ProfilerProvider = new MyProfilerProvider();
    33. // (Optional) You can disable "Connection Open()", "Connection Close()" (and async variant) tracking.
    34. // (defaults to true, and connection opening/closing is tracked)
    35. options.TrackConnectionOpenClose = true;
    36. // (Optional) Use something other than the "light" color scheme.
    37. // (defaults to "light")
    38. options.ColorScheme = StackExchange.Profiling.ColorScheme.Auto;
    39. // The below are newer options, available in .NET Core 3.0 and above:
    40. // (Optional) You can disable MVC filter profiling
    41. // (defaults to true, and filters are profiled)
    42. options.EnableMvcFilterProfiling = true;
    43. // ...or only save filters that take over a certain millisecond duration (including their children)
    44. // (defaults to null, and all filters are profiled)
    45. // options.MvcFilterMinimumSaveMs = 1.0m;
    46. // (Optional) You can disable MVC view profiling
    47. // (defaults to true, and views are profiled)
    48. options.EnableMvcViewProfiling = true;
    49. // ...or only save views that take over a certain millisecond duration (including their children)
    50. // (defaults to null, and all views are profiled)
    51. // options.MvcViewMinimumSaveMs = 1.0m;
    52. // (Optional) listen to any errors that occur within MiniProfiler itself
    53. // options.OnInternalError = e => MyExceptionLogger(e);
    54. // (Optional - not recommended) You can enable a heavy debug mode with stacks and tooltips when using memory storage
    55. // It has a lot of overhead vs. normal profiling and should only be used with that in mind
    56. // (defaults to false, debug/heavy mode is off)
    57. //options.EnableDebugMode = true;
    58. });
    59. }
  • 使用您想要的选项配置 MiniProfiler:

    1. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IMemoryCache cache)
    2. {
    3. // ...existing configuration...
    4. app.UseMiniProfiler();
    5. // The call to app.UseMiniProfiler must come before the call to app.UseMvc
    6. app.UseMvc(routes =>
    7. {
    8. // ...
    9. });
    10. }

    注意:以上大部分都是可选的。一个配置可以是最小的app.UseMiniProfiler(new MiniProfilerOptions()));

  • 在 中添加标签助手_ViewImports.cshtml

@using StackExchange.Profiling
@addTagHelper *, MiniProfiler.AspNetCore.Mvc

  • 将 MiniProfiler 添加到您的视图布局(Shared/_Layout.cshtml默认情况下):<mini-profiler />

注:<mini-profiler>有一个像许多选项max-tracespositioncolor-schemenonce,等你可以在代码中找到它们在这里。 注意#2:上面的标签助手注册可能会在 ASP.NET Core 的未来版本中消失,他们正在研究更流畅的替代方案。

剖析

现在您已准备好进行概要分析。除了用于分析代码部分的常用usingwrap 方法之外,ASP.NET Core 还包括一个标签助手,您可以在如下视图中使用:

  1. <profile name="My Profiling Step via a <profile> Tag">
  2. @{ SomethingExpensive(); }
  3. <span>Hello Mars!</span>
  4. </profile>

路线

MiniProfiler 有 2 个用户端点。根由MiniProfilerOptions.RouteBasePath(默认为/mini-profiler-resources,但可以更改)确定:

  • /<base>/results-index:最近的分析器列表,需要通过.ResultsAuthorize(或.ResultsAuthorizeAsync)和.ResultsListAuthorize(或.ResultsListAuthorizeAsync)授权
  • /<base>/results:查看当前用户的最后一个探查器或特定探查器?id={guid},通过.ResultsAuthorize(或.ResultsAuthorizeAsync)需要授权