nuget中搜索metrics,如圖:
class Program
{
static void Main(string[] args)
{
Metric.Config
// Web監視儀表板,提供Metrics.NET度量值圖表
.WithHttpEndpoint("http://localhost:1234/metrics/")
// 配置報表輸出
.WithReporting((rc)=>{
// 報表輸出到控制台
rc.WithConsoleReport(TimeSpan.FromSeconds(5));
});
Console.ReadLine();
}
}
static void GaugeTest() { Metric.Gauge("test.gauge", () => ran.NextDouble() * 1000, Unit.None); }
static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds);
static void CounterTest()
{
var counter = Metric.Counter("test.counter", Unit.Custom("並發"));
Action doWork = () => { System.Threading.Thread.Sleep(ran.Next(10, 300)); };
Action idlesse = () => { System.Threading.Thread.Sleep(ran.Next(0, 500)); };
for (var i = 0; i < 20; i++)
{
Task.Run(() =>
{
while (true)
{
counter.Increment();
doWork();
counter.Decrement();
idlesse();
}
});
}
}
static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds);
static void HistogramTest()
{
var histogram = Metric.Histogram("test.histogram", Unit.Custom("歲"), SamplingType.LongTerm);
Task.Run(() =>
{
while (true)
{
histogram.Update(ran.Next(10, 80), ran.Next(0, 2) > 0 ? "男" : "女");
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
}
});
}
static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds);
static void MeterTest()
{
var meter = Metric.Meter("test.meter", Unit.Calls, TimeUnit.Seconds);
Action idlesse = () => { System.Threading.Thread.Sleep(ran.Next(20, 50)); };
Task.Run(() => {
while(true)
{
meter.Mark();
idlesse();
}
});
}
static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds);
static void TimerTest()
{
var timer = Metric.Timer("test.meter", Unit.None, SamplingType.FavourRecent, TimeUnit.Seconds, TimeUnit.Microseconds);
Action doWork = () => { System.Threading.Thread.Sleep(ran.Next(10, 300)); };
Action idlesse = () => { System.Threading.Thread.Sleep(ran.Next(0, 500)); };
for (var i = 0; i < 20; i++)
{
Task.Run(() =>
{
while (true)
{
timer.Time(doWork);
idlesse();
}
});
}
}
static Random ran = new Random(DateTime.Now.TimeOfDay.Milliseconds);
static void HealthCheckTest()
{
HealthChecks.RegisterHealthCheck("test.healthcheck", () =>
{
return ran.Next(100) < 5 ? HealthCheckResult.Unhealthy() : HealthCheckResult.Healthy();
});
}
健康狀況檢查很簡單,輸出就兩個值:健康(healthy)、不健康(unhealthy)。
{
"Count" : "long"
"Items" : [{
"Item" : "string",
"Count" : "long",
"Percent" : "double"
}]
}
HistogramValue
{ "Count" : "long", "LastValue" : "double", "LastUserValue" : "string", "Max" : "double", "MaxUserValue" : "string", "Mean" : "double", "Min" : "double"; "MinUserValue" : "string", "StdDev" : "double", "Median" : "double", "Percentile75" : "double", "Percentile95" : "double", "Percentile98" : "double", "Percentile99" : "double", "Percentile999" : "double", "SampleSize" : "int" }
MeterValue
{ "Count" : "long", "MeanRate" : "double", "OneMinuteRate" : "double", "FiveMinuteRate" : "double", "FifteenMinuteRate" : "double", "RateUnit" : "TimeUnit", "Items" : [{ "Item" : "string", "Percent" : "double", "Value" : { "Count" : "long", "MeanRate" : "double", "OneMinuteRate" : "double", "FiveMinuteRate" : "double", "FifteenMinuteRate" : "double", "RateUnit" : "TimeUnit" } }] }
TimerValue
{ "Rate" : "MeterValue", "Histogram" : "HistogramValue", "ActiveSessions" : "long" }
使用時讀取,無需額外函數支持
Increase(long value=1, string item=null) : void //增加計數
Decrease(long value=1, string item=null) : void //減少計數
Update(double value, string userValue=null) : void
Mark(long count=1, string item=null) : void
Record(long duration, TimeUnit unit, string userValue=null) : void
Timer(Action action, string userValue=null) : void
Timer<T>(Func<T> action, string userValue=null) : T
MetricsDemo.rar