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

接下來(lái)配置MiniProfiler,總共分三步:
第一步,來(lái)到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);
// 當(dāng)然這個(gè)方法還可以添加一個(gè)lambda表達(dá)式作為參數(shù),從而做一些自定義的配置:
services.AddMiniProfiler(options =>
{
// 設(shè)定彈出窗口的位置是左下角
options.PopupRenderPosition = RenderPosition.BottomLeft;
// 設(shè)定在彈出的明細(xì)窗口里會(huì)顯式Time With Children這列
options.PopupShowTimeWithChildren = true;
});
}第二步,來(lái)到來(lái)到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)
{
...
// 最重要的一點(diǎn)是就是配置中間件在管道中的位置,一定要把它放在UseMvc()方法之前。
app.UseMiniProfiler();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}第三步,將MiniProfiler的Tag Helper放到頁(yè)面上
- _ViewImports 頁(yè)面引入 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>
@* 其實(shí)放在頁(yè)面的任意地方都應(yīng)該可以,但是由于它會(huì)加載一些腳本文件,所以建議放在footer下面: *@@* 其實(shí)放在頁(yè)面的任意地方都應(yīng)該可以,但是由于它會(huì)加載一些腳本文件,所以建議放在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>運(yùn)行應(yīng)用,可以看到左下角就是MiniProfiler:

點(diǎn)擊它之后會(huì)彈出窗口,里面有每個(gè)步驟具體的耗用時(shí)間。

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

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

自定義分析 CustomTiming
有時(shí)候,分析一些例如請(qǐng)求外部動(dòng)作的時(shí)候,上面講的做法可能不太靈光,這里我們就可以使用CustomTime()方法
public async Task<IActionResult> Privacy()
{
// 這個(gè)例子里,我們使用 MiniProfiler.Current.CustomTiming() 方法。
// 第一個(gè)參數(shù)是一個(gè)用于分類(lèi)的字符串,這里我用的是http請(qǐng)求,所以寫(xiě)了http;
// 第二個(gè)參數(shù)是命令字符串,這里我用不上,暫時(shí)留空;
// 第三個(gè)參數(shù)是執(zhí)行類(lèi)型,這里我用的是Get請(qǐng)求,所以寫(xiě)了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();
}- 運(yùn)行程序,可以看到窗口的右側(cè)出現(xiàn)了
http這一列:

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

三、案例源碼:
到此這篇關(guān)于ASP.NET Core使用MiniProfiler分析應(yīng)用的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
.net控件dropdownlist動(dòng)態(tài)綁定數(shù)據(jù)具體過(guò)程分解
一、在頁(yè)面初始化時(shí)候?qū)⒓辖壎ǖ紻ropDownList;二、在頁(yè)面初始化的時(shí)候向DropDownList添加數(shù)據(jù);三、將DataReader讀取的數(shù)據(jù)動(dòng)態(tài)綁定到DropDownList等等2013-05-05
Asp.net 頁(yè)面導(dǎo)航的幾種方法與比較 分享
在ASP.NET應(yīng)用中,Web表單之間的導(dǎo)航有多種方式:用超級(jí)鏈接,用Response.Redirect,用Server.Transfer,或者用Server.Execute。本文將分析這四種導(dǎo)航方式的異同及其優(yōu)缺點(diǎn),幫助你選擇最佳的導(dǎo)航方式。2013-07-07
.NET 9 中 LINQ 新增功能實(shí)現(xiàn)過(guò)程
文章介紹了.NET 9中LINQ新增功能,包括CountBy、AggregateBy和Index方法,并提供了相關(guān)代碼示例和輸出結(jié)果,感興趣的朋友跟隨小編一起看看吧2024-11-11
把字符串轉(zhuǎn)為HtmlTable演示動(dòng)畫(huà)
怎樣將字符串轉(zhuǎn)為.cs頁(yè)面中的HtmlTable,在論壇上看到了這樣一個(gè)問(wèn)題,想試著把它解決下,感興趣的朋友可以觀看下本文的動(dòng)畫(huà),或許對(duì)你有所幫助2013-03-03
ubuntu16.4下用jexus部署ASP.NET Core環(huán)境
這篇文章主要以圖文結(jié)合的方式介紹了ubuntu16.4下ASP.NET Core部署環(huán)境搭建步驟,感興趣的小伙伴們可以參考一下2016-07-07
.NET CORE動(dòng)態(tài)調(diào)用泛型方法詳解
這篇文章主要為大家詳細(xì)介紹了.NET CORE動(dòng)態(tài)調(diào)用泛型方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

