本文適用於: ✔️ dotnet-counters 3.0.47001 版和更新版本。
您可以從執行 .NET 5 或更新版本的應用程式讀取計數器。
安裝
下載和使用 dotnet-counters方式有以下三種:
一次性執行(推薦):
從 .NET 10.0.100 開始,您可以使用以下命令執行
dotnet-counters,dnx而無需永久安裝:dnx dotnet-counters [options]例如:
dnx dotnet-counters monitor --process-id 1234這種方法會自動下載並運行最新版本,而不會永久修改您的系統。
dotnet 全域工具:
若要安裝
dotnet-counters的最新版本以供經常使用,請使用 dotnet tool install 命令:dotnet tool install --global dotnet-counters此命令會
dotnet-counters將二進位檔安裝至 .NET SDK 工具路徑,您可以將其新增至 PATH,以輕鬆叫用全域安裝的工具。直接下載:
下載適用於您平台的工具可執行檔:
OS 平台 Windows 86 | 號64 | 號臂 | 手臂-x64 Linux 64 | 號臂 | 手臂64 | 穆斯爾-x64 | 穆斯爾臂64
注意
若要在 x86 應用程式上使用 dotnet-counters,就需要對應的 x86 版工具。
概要
dotnet-counters [-h|--help] [--version] <command>
描述
dotnet-counters 是效能監視工具,適用於臨機操作的狀況監控和第一層級的效能調查。 其可觀察透過 EventCounter API 或 Meter API 發佈的效能計數器值。 舉例來說,您可以快速監控 CPU 使用率或在 .NET Core 應用程式中擲回例外狀況的頻率,以便在使用 PerfView 或 dotnet-trace 進行更認真的深入效能調查之前,確認是否有任何可疑的項目。
選項
--version顯示公用程式的
dotnet-counters版本。-h|--help顯示命令列說明。
命令
| Command |
|---|
| dotnet-counters 收集 |
| dotnet-counters 監視器 |
| dotnet-counters PS |
dotnet-counters 收集
定期收集選取的計數器值,並將其匯出為指定的檔案格式進行後續處理。
概要
dotnet-counters collect [-h|--help] [-p|--process-id] [-n|--name] [--diagnostic-port] [--refresh-interval] [--counters <COUNTERS>] [--format] [-o|--output] [-- <command>]
選項
-p|--process-id <PID>要從中收集計數器資料的處理序識別碼。
注意
在 Linux 和 macOS 上,使用此選項需要目標應用程式並
dotnet-counters共用相同的TMPDIR環境變數。 否則,命令將會逾時。-n|--name <name>要從中收集計數器資料的處理序名稱。
注意
在 Linux 和 macOS 上,使用此選項需要目標應用程式並
dotnet-counters共用相同的TMPDIR環境變數。 否則,命令將會逾時。--diagnostic-port <port-address[,(listen|connect)]>設定用來與要監視的進程通訊的 診斷埠 。
dotnet-counters目標進程內的 .NET 執行階段必須同意埠位址,其中一個接聽,另一個連線。dotnet-counters使用 OR--process-id--name選項連接時,或使用選項啟動-- <command>程序時,自動判斷正確的連接埠。 通常只需要在等待未來將啟動的進程,或與不屬於目前進程命名空間的容器內執行的進程通訊時,才需要明確指定埠。與
port-addressOS 不同:- Linux 和 macOS - Unix 網域套接字的路徑,例如
/foo/tool1.socket。 - Windows - 具名管道的路徑,例如
\\.\pipe\my_diag_port1。 - Android、iOS 和 tvOS - IP:port,例如
127.0.0.1:9000。
依預設,
dotnet-counters在指定的位址接聽。 您可以透過在位址後附加dotnet-counters來請求,connect連線。 例如,--diagnostic-port /foo/tool1.socket,connect會連線到接聽 Unix 網域套接字的/foo/tool1.socket.NET 運行時間進程。如需如何使用此選項從應用程式啟動啟動監視計數器的相關信息,請參閱 使用診斷埠。
- Linux 和 macOS - Unix 網域套接字的路徑,例如
--refresh-interval <SECONDS>每次更新所顯示之計數器之間的延遲秒數
--counters <COUNTERS>計數器的逗號分隔清單。 計數器可獲指定
provider_name[:counter_name]。 如果在沒有計數器限定清單的情況下使用provider_name,則會顯示來自提供者的所有計數器。 若要探索提供者和計數器名稱,請參閱內建 指標。 針對 EventCounters,provider_name是 EventSource 的名稱,而針對 Meters,provider_name是 Meter 的名稱。--format <csv|json>要匯出的格式。 目前可用:csv、json。
-o|--output <output>輸出檔的名稱。
-- <command>集合組態參數之後,使用者可以附加
--後面接著命令來啟動 .NET 應用程式。dotnet-counters使用提供的命令啟動程序並收集請求的指標。 這通常有助於收集應用程式啟動路徑的指標,並可用於診斷或監控在主要進入點之前或之後不久發生的問題。注意
使用此選項會監視第一個 .NET 進程,以傳回工具,這表示如果您的命令啟動多個 .NET 應用程式,它只會收集第一個應用程式。 因此,建議您在獨立的應用程式上使用此選項,或使用該
dotnet exec <app.dll>選項。注意
如果您透過
dotnet-counters啟動 .NET 可執行檔,其輸入/輸出將會重新導向,而且您將無法與其 stdin/stdout 互動。 您可以透過 Ctrl+C 或 SIGTERM 結束工具,以安全地結束工具和子進程。 如果子處理序在工具之前結束,工具也會結束。 如果您需要使用 stdin/stdout,可以使用--diagnostic-port選項。 如需詳細資訊,請參閱 使用診斷埠。
注意
若要使用 dotnet-counters 收集計量,其執行身分必須與執行目標處理序的使用者或根使用者相同。 否則,此工具將無法與目標處理序建立連線。
範例
以重新整理間隔 3 秒收集所有計數器,並產生 csv 作為輸出:
> dotnet-counters collect --process-id 1902 --refresh-interval 3 --format csv --counters is unspecified. Monitoring System.Runtime counters by default. Starting a counter session. Press Q to quit.以子處理序啟動
dotnet mvc.dll,並從啟動開始收集執行階段計數器和 ASP.NET Core 裝載計數器,並將其儲存為 JSON 輸出:> dotnet-counters collect --format json --counters System.Runtime,Microsoft.AspNetCore.Hosting -- dotnet mvc.dll Starting a counter session. Press Q to quit. File saved to counter.json
dotnet-counters 監視器
顯示所選計數器的定期重新整理值。
概要
dotnet-counters monitor [-h|--help] [-p|--process-id] [-n|--name] [--diagnostic-port] [--refresh-interval] [--counters] [-- <command>]
選項
-p|--process-id <PID>要監控之處理序的識別碼。
-n|--name <name>要監控之處理序的名稱。
--diagnostic-port要建立的診斷連接埠名稱。 請參閱使用診斷連接埠,了解如何使用此選項從應用程式啟動開始監視計數器。
--refresh-interval <SECONDS>每次更新所顯示之計數器之間的延遲秒數
--counters <COUNTERS>計數器的逗號分隔清單。 計數器可獲指定
provider_name[:counter_name]。 如果在沒有計數器限定清單的情況下使用provider_name,則會顯示來自提供者的所有計數器。 若要探索提供者和計數器名稱,請參閱內建 指標。 針對 EventCounters,provider_name是 EventSource 的名稱,而針對 Meters,provider_name是 Meter 的名稱。-- <command>在集合組態參數之後,您可以附加
--後面的命令來啟動 .NET 應用程式。dotnet-counters將以提供的命令啟動流程,並監控要求的計量。 這通常有助於收集應用程式啟動路徑的指標,並可用於診斷或監控在主要進入點之前或之後不久發生的問題。注意
使用此選項會監視第一個 .NET 進程,以傳回工具,這表示如果您的命令啟動多個 .NET 應用程式,它只會收集第一個應用程式。 因此,建議您在獨立的應用程式上使用此選項,或使用該
dotnet exec <app.dll>選項。注意
啟動 .NET 可執行檔 via
dotnet-counters會重新導向其輸入/輸出,而且您將無法與其 stdin/stdout 互動。 您可以透過 Ctrl+C 或 SIGTERM 結束工具,以安全地結束工具和子進程。 如果子處理序在工具之前結束,工具也會結束。 如果您需要使用 stdin/stdout,可以使用--diagnostic-port選項。 如需詳細資訊,請參閱 使用診斷埠。
注意
在 Linux 和 macOS 上,此命令需要目標應用程式與 dotnet-counters 共用相同的 TMPDIR 環境變數。
注意
若要使用 dotnet-counters 監控計量,其執行身分必須與執行目標處理序的使用者或根使用者相同。
注意
如果您看到類似下列錯誤訊息: [ERROR] System.ComponentModel.Win32Exception (299): A 32 bit processes cannot access modules of a 64 bit process.,則您正在嘗試使用 dotnet-counters 與目標進程位元不相符的錯誤訊息。 請務必在安裝連結中下載正確位元的工具。
範例
以 3 秒為重新整理間隔,監控所有來自
System.Runtime的計數器:> dotnet-counters monitor --process-id 1902 --refresh-interval 3 --counters System.Runtime Press p to pause, r to resume, q to quit. Status: Running Name Current Value [System.Runtime] dotnet.assembly.count ({assembly}) 115 dotnet.gc.collections ({collection}) gc.heap.generation ------------------ gen0 5 gen1 1 gen2 1 dotnet.gc.heap.total_allocated (By) 1.6947e+08 dotnet.gc.last_collection.heap.fragmentation.size (By) gc.heap.generation ------------------ gen0 0 gen1 348,248 gen2 0 loh 32 poh 0 dotnet.gc.last_collection.heap.size (By) gc.heap.generation ------------------ gen0 0 gen1 18,010,920 gen2 5,065,600 loh 98,384 poh 3,407,048 dotnet.gc.last_collection.memory.committed_size (By) 66,842,624 dotnet.gc.pause.time (s) 0.05 dotnet.jit.compilation.time (s) 1.317 dotnet.jit.compiled_il.size (By) 574,886 dotnet.jit.compiled_methods ({method}) 6,008 dotnet.monitor.lock_contentions ({contention}) 194 dotnet.process.cpu.count ({cpu}) 16 dotnet.process.cpu.time (s) cpu.mode -------- system 4.953 user 6.266 dotnet.process.memory.working_set (By) 1.3217e+08 dotnet.thread_pool.queue.length ({work_item}) 0 dotnet.thread_pool.thread.count ({thread}) 133 dotnet.thread_pool.work_item.count ({work_item}) 71,188 dotnet.timer.count ({timer}) 124注意
如果應用程式使用 .NET 8 版或更低版本,則 System.Runtime Meter 不存在於這些版本中,而且
dotnet-counters會回復以改為顯示較舊的 System.Runtime EventCounters 。 UI 看起來略有不同,如下所示。[System.Runtime] % Time in GC since last GC (%) 0 Allocation Rate (B / 1 sec) 5,376 CPU Usage (%) 0 Exception Count (Count / 1 sec) 0 GC Fragmentation (%) 48.467 GC Heap Size (MB) 0 Gen 0 GC Count (Count / 1 sec) 1 Gen 0 Size (B) 24 Gen 1 GC Count (Count / 1 sec) 1 Gen 1 Size (B) 24 Gen 2 GC Count (Count / 1 sec) 1 Gen 2 Size (B) 272,000 IL Bytes Jitted (B) 19,449 LOH Size (B) 19,640 Monitor Lock Contention Count (Count / 1 sec) 0 Number of Active Timers 0 Number of Assemblies Loaded 7 Number of Methods Jitted 166 POH (Pinned Object Heap) Size (B) 24 ThreadPool Completed Work Item Count (Count / 1 sec) 0 ThreadPool Queue Length 0 ThreadPool Thread Count 2 Working Set (MB) 19僅監控垃圾收集和垃圾收集堆
System.Runtime配置:> dotnet-counters monitor --process-id 1902 --counters System.Runtime[dotnet.gc.collections,dotnet.gc.heap.total_allocated] Press p to pause, r to resume, q to quit. Status: Running Name Current Value [System.Runtime] dotnet.gc.collections ({collection}) gc.heap.generation ------------------ gen0 0 gen1 0 gen2 0 dotnet.gc.heap.total_allocated (By) 9,943,384監視來自使用者定義之
EventCounter的EventSource值。 如需詳細資訊,請參閱教學課程:在 .NET Core 中使用 EventCounters 測量效能。> dotnet-counters monitor --process-id 1902 --counters Samples-EventCounterDemos-Minimal Press p to pause, r to resume, q to quit. request 100啟動
my-aspnet-server.exe並監視從啟動載入的元件數目:> dotnet-counters monitor --counters System.Runtime[dotnet.assembly.count] -- my-aspnet-server.exe Press p to pause, r to resume, q to quit. Status: Running Name Current Value [System.Runtime] dotnet.assembly.count ({assembly}) 11以
my-aspnet-server.exe和arg1作為命令行自變數啟動arg2,並從其啟動時監視其工作集和 GC 堆積大小:> dotnet-counters monitor --counters System.Runtime[dotnet.process.memory.working_set,dotnet.gc.last_collection.heap.size] -- my-aspnet-server.exe arg1 arg2Name Current Value [System.Runtime] dotnet.gc.last_collection.heap.size (By) gc.heap.generation ------------------ gen0 560 gen1 462,720 gen2 0 loh 0 poh 8,184 dotnet.process.memory.working_set (By) 48,431,104
dotnet-counters PS
列出可由 dotnet-counters 監控的 dotnet 處理序。
dotnet-counters 6.0.320703 版和更新版本也會顯示每個進程啟動時使用的命令列引數 (如果有的話)。
概要
dotnet-counters ps [-h|--help]
範例
假設您使用 dotnet run --configuration Release 命令,啟動了一個長時間執行的應用程式。 在另一個視窗中,您執行了 dotnet-counters ps 命令。 您看到的輸出如下。 若為 dotnet-counters 6.0.320703 版和更新版本,會顯示命令列引數 (如果有的話)。
> dotnet-counters ps
21932 dotnet C:\Program Files\dotnet\dotnet.exe run --configuration Release
36656 dotnet C:\Program Files\dotnet\dotnet.exe
使用診斷連接埠
診斷埠 是一項運行時間功能,可讓您從應用程式啟動開始監視或收集計數器。 若要使用 dotnet-counters來執行此動作,您可以依先前範例所述使用 dotnet-counters <collect|monitor> -- <command> ,或使用 選項 --diagnostic-port 。
如要快速地從應用程式啟動開始加以監控,那麼使用 dotnet-counters <collect|monitor> -- <command> 將應用程式作為子處理序啟動是最簡單的方法。
不過,當您想更精細地控制受監控的應用程式存留期 (例如只監控應用程式前 10 分鐘並繼續執行),或是需要使用 CLI 與應用程式互動時,使用 --diagnostic-port 選項可讓您同時控制監控的目標應用程式與 dotnet-counters。
下列命令會建立
dotnet-counters名為myport.sock的診斷通訊端,並等候連線。dotnet-counters collect --diagnostic-port myport.sock輸出:
Waiting for connection on myport.sock Start an application with the following environment variable: DOTNET_DiagnosticPorts=/home/user/myport.sock在另一個主控台中,使用環境變數
DOTNET_DiagnosticPorts(設定為dotnet-counters輸出中的值) 來啟動目標應用程式。export DOTNET_DiagnosticPorts=/home/user/myport.sock ./my-dotnet-app arg1 arg2這樣就可以
dotnet-counters開始收集計數器my-dotnet-app:Waiting for connection on myport.sock Start an application with the following environment variable: DOTNET_DiagnosticPorts=myport.sock Starting a counter session. Press Q to quit.重要
啟動應用程式
dotnet run可能會有問題,因為 dotnet CLI 可能會產生許多非你應用程式的子程序,且這些子程序可能先於dotnet-counters應用程式連線,導致應用程式在執行時被暫停。 建議您直接使用獨立版本的應用程式或用於dotnet exec啟動應用程式。