零、序言
- 1、介绍
MiniProfiler is a library and UI for profiling your application. By letting you see where you time is spent, which queries are run, and any other custom timings you want to add, MiniProfiler helps you debug issues and optimize performance.
MiniProfiler 可以基于action(request)记录每个阶段的耗时,并且支持coding以自定义step信息输出。支持.net core
- 2、官方说明
地址点此查看
一、项目文件
使用 OAuth2 的项目:github 地址
二、修改 Client
- 1、Nuget 安装包
MiniProfiler.AspNetCore.Mvc
- 2、
Startup
类的方法ConfigureServices
添加代码:
// 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)
(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;
// (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 appliations)
// 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;
});
Configure
方法添加代码
// ...existing configuration... 需要将此句放在 useMVC 前
app.UseMiniProfiler();
- 3、修改
ViewImports.cshtml
,添加 tagHelper
@using StackExchange.Profiling
@addTagHelper *, MiniProfiler.AspNetCore.Mvc
- 4、
_Layout.cshtml
添加 miniprofiler 的使用
<mini-profiler />
三、运行
image.png界面左上角会出现一个访问列表,点击可以查看每次访问的具体耗时情况
- 在controller 中修改代码为如下:
var profiler = MiniProfiler.Current;
var url = "api/images";
HttpResponseMessage response;
using (profiler.Step("HTTP GET " + url))
{
response = await httpClient.GetAsync(url).ConfigureAwait(false);
}
再次运行,可以看到如下信息:
image.png
四、监控 ef
- 首先修改
Client solution
, 添加Dbcontext、Models、Repository
-
Nuget
安装packageMiniProfiler.EntityFrameworkCore
-
Startup
类的方法ConfigureServices
添加代码:
services.AddMiniProfiler().AddEntityFramework();
- 然后在
Controller
的Index
方法中添加对repository
方法的调用 -
运行
image.png
可以看到sql语句的耗时,点击sql列,查看详细信息如下:
image.png
五、代码commit
github commit 点此查看
网友评论