Skip to content

Instantly share code, notes, and snippets.

@CarlosLanderas
Created February 5, 2020 18:38
Show Gist options
  • Save CarlosLanderas/16b8aeb483f915f076a8aee8a70271b3 to your computer and use it in GitHub Desktop.
Save CarlosLanderas/16b8aeb483f915f076a8aee8a70271b3 to your computer and use it in GitHub Desktop.
Dotnet Counters with DiagnosticClient
using Microsoft.Diagnostics.NETCore.Client;
using Microsoft.Diagnostics.Tracing;
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Threading.Tasks;
namespace DotnetDiagnostics
{
class Program
{
static async Task Main(string[] args)
{
var client = new DiagnosticsClient(int.Parse(args[0]));
var providers = new List<EventPipeProvider>
{
new EventPipeProvider(args[1], EventLevel.Verbose, 0xFFFFFFFF, new Dictionary<string,string> { ["EventCounterIntervalSec"] = "1" })
};
using var session = client.StartEventPipeSession(providers);
var source = new EventPipeEventSource(session.EventStream);
source.Dynamic.All += Dynamic_All; ;
source.Process();
}
private static void Dynamic_All(TraceEvent obj)
{
var payload = (IDictionary<string, object>)obj.PayloadValue(0);
var payloadValues = (IDictionary<string, object>)payload["Payload"];
var display = payloadValues["DisplayName"];
var value = payloadValues["Mean"];
Console.WriteLine($"Counter {display} : {value}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment