自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在 ASP.Net Core 中使用 MiniProfiler

開發(fā) 前端
MiniProfiler 可用在 Asp.Net 和 ASP.Net Core 中,這篇文章將會討論如何使用 MiniProfiler,并通過它找到應用程序的性能問題。

 [[380275]]

本文轉載自微信公眾號「碼農讀書」,作者碼農讀書 。轉載本文請聯(lián)系碼農讀書公眾號。

web應用程序的性能相信是大家普遍關心的一個問題,也相信大家有很多工具可用來分析應用程序的性能并能夠找到其中的瓶頸,MiniProfiler 就是這個領域中的一款產品,它是一款簡單的,功能強大的web應用分析工具,MiniProfiler 可用來幫助我們找到 慢查詢, 慢響應 等問題。

MiniProfiler 可用在 Asp.Net 和 ASP.Net Core 中,這篇文章將會討論如何使用 MiniProfiler,并通過它找到應用程序的性能問題。

安裝 MiniProfiler

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

  1. dotnet add package MiniProfiler.AspNetCore.Mvc 

安裝好之后,接下來就要將 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.        } 

注入好之后,接下來就需要使用 UseMiniProfiler 擴展方法將其注入到 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 頁面中增加如下兩行命令。

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

最后需要在 WebPage 中指定 MiniProfiler 分析窗口應該顯示的位置,那如何做呢?在 body 標簽內使用 mini-profiler 標記,如下代碼所示:

  1. <mini-profiler position="@RenderPosition.Right" max-traces="5" /> 

在 ASP.Net Core MVC 中使用 MiniProfiler

MiniProfiler 會提供 頁面加載時間 和 數(shù)據庫查詢性能指標,接下來把程序跑起來,你會看到如下的性能指標圖。

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

  1. public class HomeController : Controller 
  2.     { 
  3.         ILogger<HomeController> logger; 
  4.  
  5.         public HomeController(ILogger<HomeController> logger) 
  6.         { 
  7.             this.logger = logger; 
  8.         } 
  9.  
  10.         public IActionResult Index() 
  11.         { 
  12.             var miniProfiler = MiniProfiler.Current
  13.             List<Author> authors = new List<Author>(); 
  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")) 做了語句塊標記,理論上 mini-profile 窗口上應該有類似 Get Authors 指標欄,接下來把程序跑起來,一起來看看效果。

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

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

使用 MiniProfile 分析 ADO.NET 查詢

除了做一些常規(guī)的頁面分析,還可以直接對 ADO.NET 查詢性能進行分析,這就????了,要這么做的話,需要使用 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.        } 

從上圖可以看到,確實對 ADO.NET 查詢有著清晰的分析,相信在幫助大家分析問題時很有幫助。

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

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

 

責任編輯:武曉燕 來源: 碼農讀書
相關推薦

2021-03-17 09:45:31

LazyCacheWindows

2021-02-02 16:19:08

Serilog日志框架

2021-02-06 21:40:13

SignalR通訊TypeScript

2021-01-31 22:56:50

FromServiceASP

2021-03-03 22:37:16

MediatR中介者模式

2021-03-10 09:40:43

LamarASP容器

2021-02-28 20:56:37

NCache緩存框架

2021-01-28 22:39:35

LoggerMessa開源框架

2021-01-07 07:39:07

工具接口 Swagger

2021-02-07 17:29:04

監(jiān)視文件接口

2024-09-10 08:13:16

Asp項目輕量級

2021-06-22 16:59:56

微軟.NETC# 軟件開發(fā)

2021-01-26 14:57:00

中間件應用模塊化

2021-04-12 07:03:10

輕量級模塊化框架

2021-01-04 05:44:54

框架日志

2017-10-20 08:52:11

內存緩存并發(fā)模式Linux

2022-08-01 08:00:00

開發(fā)工具跟蹤偵聽器

2009-02-05 14:02:46

SmtpMail發(fā)送郵件ASP.NET

2021-11-01 14:52:38

ElasticSear索引SQL

2021-04-14 07:35:12

Json格式化日期
點贊
收藏

51CTO技術棧公眾號