ASP.NET?Core使用MiniProfiler分析應用
MiniProfiler(https://miniprofiler.com/)是一個輕量級且簡單易用的分析工具庫,它可以用來分析ASP.NET Core應用。
優(yōu)點
針對ASP.NET Core MVC應用,使用MiniProfiler的優(yōu)點是:它會把結果直接放在頁面的左下角,隨時可以點擊查看;這樣的話就可以感知出你的程序運行的怎么樣;同時這也意味著,在你開發(fā)新功能的同時,可以很快速的得到反饋。
一、安裝配置MiniProfiler
在現(xiàn)有的ASP.NET Core MVC項目里,通過Nuget安裝MiniProfiler :
Install-Package MiniProfiler.AspNetCore.Mvc
當然也可以通過Nuget Package Manager可視化工具安裝

接下來配置MiniProfiler,總共分三步:
第一步,來到Startup.cs的ConfigureServices方法里,添加services.AddMiniProfiler();
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// 當然這個方法還可以添加一個lambda表達式作為參數(shù),從而做一些自定義的配置:
services.AddMiniProfiler(options =>
{
// 設定彈出窗口的位置是左下角
options.PopupRenderPosition = RenderPosition.BottomLeft;
// 設定在彈出的明細窗口里會顯式Time With Children這列
options.PopupShowTimeWithChildren = true;
});
}第二步,來到來到Startup.cs的Configure方法里,添加app.UseMiniProfiler();
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
// 最重要的一點是就是配置中間件在管道中的位置,一定要把它放在UseMvc()方法之前。
app.UseMiniProfiler();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}第三步,將MiniProfiler的Tag Helper放到頁面上
- _ViewImports 頁面引入 MiniProfiler 的 Tag Helper :
...
@using StackExchange.Profiling
...
@addTagHelper *, MiniProfiler.AspNetCore.Mvc- 將 MiniProfiler 的Tag Helper 放入 _Layout.cshtml 中:
...
<footer class="border-top footer text-muted">
<div class="container">
© 2019 - MiniProfilerCoreDemo - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
@* 其實放在頁面的任意地方都應該可以,但是由于它會加載一些腳本文件,所以建議放在footer下面: *@@* 其實放在頁面的任意地方都應該可以,但是由于它會加載一些腳本文件,所以建議放在footer下面: *@
<mini-profiler />
<environment include="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.js"></script>
</environment>
...
@RenderSection("Scripts", required: false)
</body>
</html>運行應用,可以看到左下角就是MiniProfiler:

點擊它之后會彈出窗口,里面有每個步驟具體的耗用時間。

二、MiniProfiler 具體使用
分析局部代碼
前面的例子里,我們使用MiniProfiler分析了頁面整個流程的時間。而MiniProfiler也可以用來分析一段代碼所耗用的時間??蠢樱?/p>
public async Task<IActionResult> Index()
{
#if !DEBUG
// 這里我們使用了using語句,里面使用了 MiniProfiler 類的 Current 屬性,在該屬性上面有一個Step()方法,
// 它可以用來分析using語句里面的代碼,在Step方法里,要提供一個具有描述性的名稱來表示該段代碼做的是什么動作,這個名稱會顯示在結果里。
using (MiniProfiler.Current.Step("計算第一步"))
{
var users = await _context.Users.ToListAsync();
return View(users);
}
#else
// 通常,我會使用 using 語句塊來嵌套著使用
using(MiniProfiler.Current.Step("第1步"))
{
// ... 相關操作
Thread.Sleep(30);
using(MiniProfiler.Current.Step("第1.1步"))
{
// ... 相關操作
Thread.Sleep(30);
}
using(MiniProfiler.Current.Step("第1.2步"))
{
// ... 相關操作
Thread.Sleep(30);
}
}
// 但是如果你只想分析一句話,那么使用using語句就顯得太麻煩了,這種情況下可以使用 Inline() 方法:
var users = await MiniProfiler.Current.Inline(async () => await _context.Users.ToListAsync(), "計算第一步");
return View(users);
#endif
}使用 using 語句塊嵌套結果展示:

使用 Inline() 方法結果展示:

自定義分析 CustomTiming
有時候,分析一些例如請求外部動作的時候,上面講的做法可能不太靈光,這里我們就可以使用CustomTime()方法
public async Task<IActionResult> Privacy()
{
// 這個例子里,我們使用 MiniProfiler.Current.CustomTiming() 方法。
// 第一個參數(shù)是一個用于分類的字符串,這里我用的是http請求,所以寫了http;
// 第二個參數(shù)是命令字符串,這里我用不上,暫時留空;
// 第三個參數(shù)是執(zhí)行類型,這里我用的是Get請求,所以寫了GET;
using (CustomTiming timing = MiniProfiler.Current.CustomTiming("http", string.Empty, "GET"))
{
var url = "http://27.24.159.155";
var httpClient = new HttpClient
{
BaseAddress = new Uri(url)
};
httpClient.DefaultRequestHeaders.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await httpClient.GetAsync("/system/resource/code/news/click/dynclicks.jsp?clickid=1478&owner=1220352265&clicktype=wbnews");
timing.CommandString = $"URL:{url}\n\r Response Code:{response.StatusCode}";
if (!response.IsSuccessStatusCode)
{
throw new Exception("Error fetching data from API");
}
var clickTimes = await response.Content.ReadAsStringAsync();
ViewData["clickTimes"] = clickTimes;
}
return View();
}- 運行程序,可以看到窗口的右側出現(xiàn)了
http這一列:

- 點擊 http 所在列的
153.1 (1),這就是使用CustomTiming分析的那段代碼,它請求的URL和返回碼都顯示了出來。

三、案例源碼:
到此這篇關于ASP.NET Core使用MiniProfiler分析應用的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
一文帶你了解.Net基于Threading.Mutex實現(xiàn)互斥鎖
互斥鎖是一個互斥的同步對象,意味著同一時間有且僅有一個線程可以獲取它。這篇文章主要介紹了一文帶你了解.Net基于Threading.Mutex實現(xiàn)互斥鎖,感興趣的可以了解一下2021-06-06
asp.net Javascript獲取CheckBoxList的value
最近在做一個BS的小項目,記得自己搞asp.net的時候,還是兩年以前,大部分的東西只是有點印象,忘得差不多了,所以這次也算是溫習的過程吧,一邊學習,一邊趕工,呵呵呵。。。。2009-12-12
動態(tài)加載用戶控件至DataList并為用戶控件賦值實例演示
本文借用使用通用的新聞例子演示動態(tài)加載用戶控件至DataList并為用戶控件賦值,感興趣的朋友可以了解下2013-01-01
ASP.NET Core Kestrel 中使用 HTTPS (SSL)
這篇文章主要為大家詳細介紹了ASP.NET Core Kestrel 中使用 HTTPS(SSL)的相關資料,感興趣的小伙伴們可以參考一下2016-09-09

