正在应用的示例位于 https://learn.microsoft.com/en-us/dotnet/core/diagnostics/metrics-collection 但是,这些指标在 dotnet 计数器下不可见。
如何查看指标?
程序编译时
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Threading;
namespace ConsoleApp7
{
internal class Program
{
static void Main(string[] args)
{
Meter s_meter = new("HatCo.HatStore", "1.0.0");
Counter<int> s_hatsSold = s_meter.CreateCounter<int>("hats-sold");
using MeterListener meterListener = new();
meterListener.InstrumentPublished = (instrument, listener) =>
{
if (instrument.Meter.Name is "HatCo.HatStore")
{
listener.EnableMeasurementEvents(instrument);
}
};
meterListener.SetMeasurementEventCallback<int>(OnMeasurementRecorded);
// Start the meterListener, enabling InstrumentPublished callbacks.
meterListener.Start();
var rand = Random.Shared;
Console.WriteLine("Press any key to exit");
while (!Console.KeyAvailable)
{
//// Simulate hat selling transactions.
Thread.Sleep(rand.Next(100, 2500));
s_hatsSold.Add(rand.Next(0, 1000));
}
static void OnMeasurementRecorded<T>(
Instrument instrument,
T measurement,
ReadOnlySpan<KeyValuePair<string, object?>> tags,
object? state)
{
Console.WriteLine($"{instrument.Name} recorded measurement {measurement}");
}
}
}
}
hte程序的输出窗口显示
hats-sold recorded measurement 253
hats-sold recorded measurement 168
hats-sold recorded measurement 473
hats-sold recorded measurement 790
但是,如输出所示,ditbet 计数器无法找到它
C:\Users\me>dotnet-counters monitor -n metric-instr
There is no active process with the given name: metric-instr
感谢@alexanderBurov,如上所述 dotnet-计数器监视器-n ConsoleApp7