如果您更愿意通过示例进行学习,可以使用示例应用程序。ASP.NET Core 示例在此处,其中包含Startup.cs中的重要部分。
安装和配置
- 安装 NuGet 包:MiniProfiler.AspNetCore.Mvc
- 使用 NuGet UI 进行安装
MiniProfiler.AspNetCore.Mvc
(具有所有需要的依赖项) - 或者使用包管理器控制台:
Install-Package MiniProfiler.AspNetCore.Mvc -IncludePrerelease
- 使用 NuGet UI 进行安装
编辑您
Startup.cs
以添加中间件并配置选项:public void ConfigureServices(IServiceCollection services)
{
// ...existing configuration...
// Note .AddMiniProfiler() returns a IMiniProfilerBuilder for easy intellisense
services.AddMiniProfiler(options =>
{
// All of this is optional. You can simply call .AddMiniProfiler() for all defaults
// (Optional) Path to use for profiler URLs, default is /mini-profiler-resources
options.RouteBasePath = "/profiler";
// (Optional) Control storage
// (default is 30 minutes in MemoryCacheStorage)
// Note: MiniProfiler will not work if a SizeLimit is set on MemoryCache!
// See: https://github.com/MiniProfiler/dotnet/issues/501 for details
(options.Storage as MemoryCacheStorage).CacheDuration = TimeSpan.FromMinutes(60);
// (Optional) Control which SQL formatter to use, InlineFormatter is the default
options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();
// (Optional) To control authorization, you can use the Func<HttpRequest, bool> options:
// (default is everyone can access profilers)
options.ResultsAuthorize = request => MyGetUserFunction(request).CanSeeMiniProfiler;
options.ResultsListAuthorize = request => MyGetUserFunction(request).CanSeeMiniProfiler;
// Or, there are async versions available:
options.ResultsAuthorizeAsync = async request => (await MyGetUserFunctionAsync(request)).CanSeeMiniProfiler;
options.ResultsAuthorizeListAsync = async request => (await MyGetUserFunctionAsync(request)).CanSeeMiniProfilerLists;
// (Optional) To control which requests are profiled, use the Func<HttpRequest, bool> option:
// (default is everything should be profiled)
options.ShouldProfile = request => MyShouldThisBeProfiledFunction(request);
// (Optional) Profiles are stored under a user ID, function to get it:
// (default is null, since above methods don't use it by default)
options.UserIdProvider = request => MyGetUserIdFunction(request);
// (Optional) Swap out the entire profiler provider, if you want
// (default handles async and works fine for almost all applications)
options.ProfilerProvider = new MyProfilerProvider();
// (Optional) You can disable "Connection Open()", "Connection Close()" (and async variant) tracking.
// (defaults to true, and connection opening/closing is tracked)
options.TrackConnectionOpenClose = true;
// (Optional) Use something other than the "light" color scheme.
// (defaults to "light")
options.ColorScheme = StackExchange.Profiling.ColorScheme.Auto;
// The below are newer options, available in .NET Core 3.0 and above:
// (Optional) You can disable MVC filter profiling
// (defaults to true, and filters are profiled)
options.EnableMvcFilterProfiling = true;
// ...or only save filters that take over a certain millisecond duration (including their children)
// (defaults to null, and all filters are profiled)
// options.MvcFilterMinimumSaveMs = 1.0m;
// (Optional) You can disable MVC view profiling
// (defaults to true, and views are profiled)
options.EnableMvcViewProfiling = true;
// ...or only save views that take over a certain millisecond duration (including their children)
// (defaults to null, and all views are profiled)
// options.MvcViewMinimumSaveMs = 1.0m;
// (Optional) listen to any errors that occur within MiniProfiler itself
// options.OnInternalError = e => MyExceptionLogger(e);
// (Optional - not recommended) You can enable a heavy debug mode with stacks and tooltips when using memory storage
// It has a lot of overhead vs. normal profiling and should only be used with that in mind
// (defaults to false, debug/heavy mode is off)
//options.EnableDebugMode = true;
});
}
使用您想要的选项配置 MiniProfiler:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IMemoryCache cache)
{
// ...existing configuration...
app.UseMiniProfiler();
// The call to app.UseMiniProfiler must come before the call to app.UseMvc
app.UseMvc(routes =>
{
// ...
});
}
注意:以上大部分都是可选的。一个配置可以是最小的
app.UseMiniProfiler(new MiniProfilerOptions()));
在 中添加标签助手
_ViewImports.cshtml
:
@using StackExchange.Profiling
@addTagHelper *, MiniProfiler.AspNetCore.Mvc
- 将 MiniProfiler 添加到您的视图布局(Shared/_Layout.cshtml默认情况下):
<mini-profiler />
注:<mini-profiler>
有一个像许多选项max-traces
,position
,color-scheme
,nonce
,等你可以在代码中找到它们在这里。 注意#2:上面的标签助手注册可能会在 ASP.NET Core 的未来版本中消失,他们正在研究更流畅的替代方案。
剖析
现在您已准备好进行概要分析。除了用于分析代码部分的常用usingwrap 方法之外,ASP.NET Core 还包括一个标签助手,您可以在如下视图中使用:
<profile name="My Profiling Step via a <profile> Tag">
@{ SomethingExpensive(); }
<span>Hello Mars!</span>
</profile>
路线
MiniProfiler
有 2 个用户端点。根由MiniProfilerOptions.RouteBasePath
(默认为/mini-profiler-resources
,但可以更改)确定:
/<base>/results-index
:最近的分析器列表,需要通过.ResultsAuthorize
(或.ResultsAuthorizeAsync
)和.ResultsListAuthorize
(或.ResultsListAuthorizeAsync
)授权/<base>/results
:查看当前用户的最后一个探查器或特定探查器?id={guid}
,通过.ResultsAuthorize
(或.ResultsAuthorizeAsync
)需要授权