要開始使用庫,請安裝Metrics.NET NuGet包,使用包管理UI或包管理控制檯運行:
Install-Package Metrics.NET
在您的應用程序的初始化代碼中添加以下Metrics.NET配置代碼:
using Metrics;
Metric.Config
.WithHttpEndpoint("http://localhost:1234/")
.WithAllCounters();
運行該應用程序並將Web瀏覽器指向http://localhost:1234/
Metrics可視化應用程序應該已經有了從各個性能計數器捕獲的多個Gauges。
你現在可以開始測量了:
public class SampleMetrics
{
private readonly Timer timer = Metric.Timer("Requests", Unit.Requests);
private readonly Counter counter = Metric.Counter("ConcurrentRequests", Unit.Requests);
public void Request(int i)
{
this.counter.Increment();
using (this.timer.NewContext()) // measure until disposed
{
// do some work
}
this.counter.Decrement();
}
}
譯自:https://github.com/Recognos/Metrics.NET/wiki/Getting-Started