日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
如何在ASP.NetCore中使用MiniProfiler

本文轉(zhuǎn)載自微信公眾號(hào)「碼農(nóng)讀書(shū)」,作者碼農(nóng)讀書(shū) 。轉(zhuǎn)載本文請(qǐng)聯(lián)系碼農(nóng)讀書(shū)公眾號(hào)。

10年積累的做網(wǎng)站、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有雁山免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

web應(yīng)用程序的性能相信是大家普遍關(guān)心的一個(gè)問(wèn)題,也相信大家有很多工具可用來(lái)分析應(yīng)用程序的性能并能夠找到其中的瓶頸,MiniProfiler 就是這個(gè)領(lǐng)域中的一款產(chǎn)品,它是一款簡(jiǎn)單的,功能強(qiáng)大的web應(yīng)用分析工具,MiniProfiler 可用來(lái)幫助我們找到 慢查詢, 慢響應(yīng) 等問(wèn)題。

MiniProfiler 可用在 Asp.Net 和 ASP.Net Core 中,這篇文章將會(huì)討論如何使用 MiniProfiler,并通過(guò)它找到應(yīng)用程序的性能問(wèn)題。

安裝 MiniProfiler

要想使用 MiniProfiler,需要通過(guò) nuget 引用 MiniProfiler.AspNetCore.Mvc 包,可以通過(guò) Visual Studio 2019 的 NuGet package manager 可視化界面安裝 或者 通過(guò) NuGet package manager 命令行工具輸入以下命令:

 
 
 
 
  1. dotnet add package MiniProfiler.AspNetCore.Mvc 

安裝好之后,接下來(lái)就要將 MiniProfiler 注入到 ServiceCollection 容器中,如下代碼所示:

 
 
 
 
  1. // This method gets called by the runtime. Use this method to add services to the container. 
  2.        public void ConfigureServices(IServiceCollection services) 
  3.        { 
  4.            services.AddControllersWithViews(); 
  5.  
  6.            services.AddMiniProfiler(options => options.RouteBasePath = "/profiler"); 
  7.        } 

注入好之后,接下來(lái)就需要使用 UseMiniProfiler 擴(kuò)展方法將其注入到 Request Pipeline 管道中,如下代碼所示:

 
 
 
 
  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) 
  2.         { 
  3.             app.UseMiniProfiler(); 
  4.  
  5.             app.UseEndpoints(endpoints => 
  6.             { 
  7.                 endpoints.MapControllerRoute( 
  8.                     name: "default", 
  9.                     pattern: "{controller=Home}/{action=Index}/{id?}"); 
  10.             }); 
  11.         } 

然后在 _Layout.cshtml 頁(yè)面中增加如下兩行命令。

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

最后需要在 WebPage 中指定 MiniProfiler 分析窗口應(yīng)該顯示的位置,那如何做呢?在 body 標(biāo)簽內(nèi)使用 mini-profiler 標(biāo)記,如下代碼所示:

 
 
 
 
  1.  

在 ASP.Net Core MVC 中使用 MiniProfiler

MiniProfiler 會(huì)提供 頁(yè)面加載時(shí)間 和 數(shù)據(jù)庫(kù)查詢性能指標(biāo),接下來(lái)把程序跑起來(lái),你會(huì)看到如下的性能指標(biāo)圖。

有些朋友可能就要問(wèn)了,大體時(shí)間我是知道了,那如果我只想獲取某一指定代碼塊的執(zhí)行時(shí)間呢?當(dāng)然也是可以的,下面的代碼展示了如何去實(shí)現(xiàn)。

 
 
 
 
  1. public class HomeController : Controller 
  2.     { 
  3.         ILogger logger; 
  4.  
  5.         public HomeController(ILogger logger) 
  6.         { 
  7.             this.logger = logger; 
  8.         } 
  9.  
  10.         public IActionResult Index() 
  11.         { 
  12.             var miniProfiler = MiniProfiler.Current; 
  13.             List authors = new List(); 
  14.  
  15.             miniProfiler.RenderIncludes(this.HttpContext); 
  16.  
  17.             using (miniProfiler.Step("Get Authors")) 
  18.             { 
  19.                 authors.Add(new Author() { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", Address = "Hyderabad, India" }); 
  20.                 authors.Add(new Author() { Id = 2, FirstName = "Stephen", LastName = "Smith", Address = "NY, USA" }); 
  21.                 authors.Add(new Author() { Id = 3, FirstName = "Anand", LastName = "Narayanan", Address = "Chennai, India" }); 
  22.                 authors.Add(new Author() { Id = 4, FirstName = "Steve", LastName = "Jones", Address = "London, UK" }); 
  23.             } 
  24.             return View(authors); 
  25.         } 
  26.     } 
  27.  
  28.     public class Author 
  29.     { 
  30.         public int Id { get; set; } 
  31.         public string FirstName { get; set; } 
  32.         public string LastName { get; set; } 
  33.         public string Address { get; set; } 
  34.     } 

從上面的代碼中可以看到,我用 using (miniProfiler.Step("Get Authors")) 做了語(yǔ)句塊標(biāo)記,理論上 mini-profile 窗口上應(yīng)該有類似 Get Authors 指標(biāo)欄,接下來(lái)把程序跑起來(lái),一起來(lái)看看效果。

除了順向操作,你也可以指定讓某些代碼塊不要顯示在 mini-profile 中,需要做的是調(diào)用 Ignore() 即可,如下代碼所示:

 
 
 
 
  1. using (MiniProfiler.Current.Ignore()) 
  2.   // Write code here that you don't 
  3.   // want MiniProfiler to profile 

使用 MiniProfile 分析 ADO.NET 查詢

除了做一些常規(guī)的頁(yè)面分析,還可以直接對(duì) ADO.NET 查詢性能進(jìn)行分析,這就????了,要這么做的話,需要使用 ProfileDbConnection 和 ProfileDbCommand 即可,如下代碼所示:

 
 
 
 
  1. public IActionResult Index() 
  2.        { 
  3.            using (SqlConnection connection = new SqlConnection(@"Data Source=.; Initial Catalog=PYZ_L; Trusted_Connection=Yes")) 
  4.            { 
  5.                using (ProfiledDbConnection profiledDbConnection = new ProfiledDbConnection(connection, MiniProfiler.Current)) 
  6.                { 
  7.                    if (profiledDbConnection.State != System.Data.ConnectionState.Open) 
  8.                    { 
  9.                        profiledDbConnection.Open(); 
  10.                    } 
  11.  
  12.                    using (SqlCommand command = new SqlCommand("Select * From Clothes", connection)) 
  13.                    { 
  14.                        using (ProfiledDbCommand profiledDbCommand = new ProfiledDbCommand(command, connection, MiniProfiler.Current)) 
  15.                        { 
  16.                            var data = profiledDbCommand.ExecuteReader(); 
  17.                            //Write code here to populate the list of Authors 
  18.                        } 
  19.                    } 
  20.                } 
  21.            } 
  22.  
  23.            return View(); 
  24.        } 

從上圖可以看到,確實(shí)對(duì) ADO.NET 查詢有著清晰的分析,相信在幫助大家分析問(wèn)題時(shí)很有幫助。

MiniProfiler 是一個(gè)可應(yīng)用于 .NET, Ruby, Go 和 Node.js 的性能分析工具,你可以使用 MiniProfiler 去分析 Dapper,Linq2SQL,Entity Framework 所使用的sql的查詢性能,此外 MimiProfile 之所以 Mini,意味著它介入到你的應(yīng)用程序中所帶來(lái)的性能開(kāi)銷微乎其微,所以大家可放心的丟到生產(chǎn)上去吧!

譯文鏈接:https://www.infoworld.com/article/3330560/how-to-use-miniprofiler-in-aspnet-core.html


新聞名稱:如何在ASP.NetCore中使用MiniProfiler
本文鏈接:http://www.5511xx.com/article/dhocjeo.html