ASP.NET Core WebAPI中的分析工具MiniProfiler

安裝

咱們可使用Nuget來下載這個包。javascript

PM> Install-Package MiniProfiler.AspNetCore.Mvc

配置Startup.cs

MiniProfiler配置起來很簡單,只須要如下幾步css

ConfigureServices方法中添加MiniProfiler服務

public void ConfigureServices(IServiceCollection services) { services.AddMiniProfiler(options => options.RouteBasePath = "/profiler" ); }
  • 這裏是配置了MiniProfiler的路由基礎路徑,默認的路徑是/mini-profiler-resources
  • 按照當前配置,你可使用"/profiler/results"來訪問分析報告

激活中間件,啓用MiniProfiler服務

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseMiniProfiler(); }

配置須要監控分析的代碼

public class ValueController : ControllerBase { [HttpGet] public IEnumerable<string> Get() { string url1 = string.Empty; string url2 = string.Empty; using (MiniProfiler.Current.Step("Get方法")) { using (MiniProfiler.Current.Step("準備數據")) { using (MiniProfiler.Current.CustomTiming("SQL", "SELECT * FROM Config")) { // 模擬一個SQL查詢 Thread.Sleep(500); url1 = "https://www.baidu.com"; url2 = "https://www.sina.com.cn/"; } } using (MiniProfiler.Current.Step("使用從數據庫中查詢的數據,進行Http請求")) { using (MiniProfiler.Current.CustomTiming("HTTP", "GET " + url1)) { var client = new WebClient(); var reply = client.DownloadString(url1); } using (MiniProfiler.Current.CustomTiming("HTTP", "GET " + url2)) { var client = new WebClient(); var reply = client.DownloadString(url2); } } } return new string[] { "value1", "value2" }; } }

代碼解釋:html

  • MiniProfiler.Current.Step方法定義了分析的步驟,這個方法能夠接受一個String類型的參數,它會顯示在最終的報告中
  • MiniProfiler.Current.CustomTiming方法是更細粒度的對報告內容進行分類,以上代碼中定義了2種分類,一種是SQL, 一種是Http
  • 上述程序的功能: 模擬從數據庫拉取2個網站的Url, 並使用WebClient來分別請求網站的Url

查看效果

下面咱們啓動項目, 項目默認打開/api/valuesjava

而後咱們來訪問如下/profiler/results, 就會出現以下頁面git

如上圖所示,咱們能夠很清楚的看到代碼中每一部分的耗時,因爲咱們添加了2種分類SQL和Http,因此列表中會對2種分類進行彙總。github

重點: 當前頁面只會顯示最近的一次請求數據庫

從當前報告中能夠獲得如下結論json

  • 當前請求總響應時間 1723.6ms
  • SQL語句查詢耗時517.ms
  • 2次Http請求共耗時868.3ms, 其中訪問百度耗時424.6ms, 訪問新浪耗時443.7ms

如何讓MiniProfiler與Swagger集成

這裏咱們就再也不講如何在ASP.NET Core中整合Swagger。c#

MiniProfiler和Swagger是能夠集成在一塊兒的,爲了完成這個功能,咱們須要進行如下幾步api

下載Swagger自定義頁面

默認的index.html頁面能夠從以下連接下載

https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/src/Swashbuckle.AspNetCore.SwaggerUI/index.html

下載以後將這個文件放置到項目根目錄下。

接下來咱們須要在這個文件的頭部加入以下腳本代碼:

<script async="async" id="mini-profiler" src="/profiler/includes.min.js?v=4.0.138+gcc91adf599" data-version="4.0.138+gcc91adf599" data-path="/profiler/" data-current-id="4ec7c742-49d4-4eaf-8281-3c1e0efa748a" data-ids="" data-position="Left" data-authorized="true" data-max-traces="15" data-toggle-shortcut="Alt+P" data-trivial-milliseconds="2.0" data-ignored-duplicate-execute-types="Open,OpenAsync,Close,CloseAsync"> </script>

最後咱們須要配置這個index.html文件的Bulid Action爲Embedded resource

安裝自定義頁面

Startup.cs文件中,咱們須要修改UseSwaggerUI中間件的配置,這裏咱們須要添加一個InputStream配置。

app.UseSwaggerUI(c => { c.RoutePrefix = "swagger"; c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("MiniProfilerSample.index.html"); });

注意:這裏MiniProfilerSample是項目的命名空間名

最終效果

從新啓動項目,Swagger文檔頁面的左上角就出現了一個小的面板,當模擬請求一個鏈接以後,它就會顯示出當前請求的分析數據,看起來是否是很酷炫。

總結

本篇博客描述瞭如何使用MiniProfiler來監控分析你的Api。 MiniProfiler除了提供網頁顯示報告,還支持將報告結果存儲在數據庫中,後面我會補充一篇文章來講明如何將報告保存到數據庫中。

本篇源代碼: https://github.com/lamondlu/Sample_MiniProfiler

相關文章
相關標籤/搜索