要开始使用库,请安装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