共用方式為


.NET 中的網路事件計數器

EventCounters 是用於輕量型、跨平臺和近乎即時效能計量集合的 .NET API。

網路元件會經過檢測,以使用 EventCounters 發佈基本診斷資訊。 它們包含如下的資訊:

  • System.Net.Http > requests-started
  • System.Net.Http > requests-failed
  • System.Net.Http > http11-connections-current-total
  • System.Net.Security > all-tls-sessions-open
  • System.Net.Sockets > outgoing-connections-established
  • System.Net.NameResolution > dns-lookups-duration

小提示

如需完整清單,請參閱 已知的計數器

小提示

在以 .NET 8+ 為目標的專案上,請考慮使用較新且功能更豐富的 網路計量 ,而不是 EventCounters。

供應商

網路資訊會分散到下列提供者:

  • System.Net.HttpHttpClientSocketsHttpHandler
  • System.Net.NameResolutionDns
  • System.Net.SecuritySslStream
  • System.Net.Sockets
  • Microsoft.AspNetCore.Hosting
  • Microsoft-AspNetCore-Server-Kestrel

啟用時,遙測可能會對效能造成一些額外負擔,因此請務必只訂閱您真正有興趣的服務提供者。

從進程外部監視事件計數器

dotnet-counters

dotnet-counters 是一種跨平臺效能監視工具,可用於臨機作健康情況監視和一級效能調查。

dotnet tool install --global dotnet-counters
dotnet-counters monitor --counters System.Net.Http,System.Net.Security --process-id 1234

此命令會以最即時的數字不斷刷新控制台。

[System.Net.Http]
    Current Http 1.1 Connections                       3
    Current Http 2.0 Connections                       1
    Current Http 3.0 Connections                       0
    Current Requests                                   4
    HTTP 1.1 Requests Queue Duration (ms)              0
    HTTP 2.0 Requests Queue Duration (ms)              0
    HTTP 3.0 Requests Queue Duration (ms)              0
    Requests Failed                                    0
    Requests Failed Rate (Count / 1 sec)               0
    Requests Started                                 470
    Requests Started Rate (Count / 1 sec)             18

如需所有可用的命令和參數,請參閱 dotnet-counter 檔

Application Insights

根據預設,Application Insights 不會收集事件計數器。 如需自定義您感興趣的計數器集合的相關信息,請參閱 AppInsights EventCounters 檔

例如:

services.ConfigureTelemetryModule<EventCounterCollectionModule>((module, options) =>
{
    module.Counters.Add(new EventCounterCollectionRequest("System.Net.Http", "current-requests"));
    module.Counters.Add(new EventCounterCollectionRequest("System.Net.Http", "requests-failed"));
    module.Counters.Add(new EventCounterCollectionRequest("System.Net.Http", "http11-connections-current-total"));
    module.Counters.Add(new EventCounterCollectionRequest("System.Net.Security", "all-tls-sessions-open"));
});

如需如何訂閱許多運行時間和 ASP.NET 事件計數器的範例,請參閱 RuntimeEventCounters 範例。 只要為每個條目新增EventCounterCollectionRequest

foreach (var (eventSource, counters) in RuntimeEventCounters.EventCounters)
{
    foreach (string counter in counters)
    {
        module.Counters.Add(new EventCounterCollectionRequest(eventSource, counter));
    }
}

取用處理程序內的事件計數器

Yarp.Telemetry.Consumption 庫可讓您輕鬆地在進程內取用事件計數器。 雖然套件目前會維護為 YARP 專案的一部分,但它可用於任何 .NET 應用程式中。

若要使用它,請實作 IMetricsConsumer<TMetrics> 介面:

public sealed class MyMetricsConsumer : IMetricsConsumer<SocketsMetrics>
{
    public void OnMetrics(SocketsMetrics previous, SocketsMetrics current)
    {
        var elapsedTime = (current.Timestamp - previous.Timestamp).TotalMilliseconds;
        Console.WriteLine($"Received {current.BytesReceived - previous.BytesReceived} bytes in the last {elapsedTime:N2} ms");
    }
}

然後向 DI 容器註冊實作:

services.AddSingleton<IMetricsConsumer<SocketsMetrics>, MyMetricsConsumer>();
services.AddTelemetryListeners();

程式庫提供以下強類型度量類型:

需要更多遙測嗎?

如果您有其他可能透過事件或計量公開之實用信息的建議,請建立 dotnet/runtime 問題

如果您使用 Yarp.Telemetry.Consumption 函式庫並有任何建議,請提交 microsoft/reverse-proxy 問題