Skip to content

Instantly share code, notes, and snippets.

@itn3000
Created October 2, 2020 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itn3000/69560c6b889677d6a42e24f289396f1c to your computer and use it in GitHub Desktop.
Save itn3000/69560c6b889677d6a42e24f289396f1c to your computer and use it in GitHub Desktop.
reading events from EventPipe example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
using Microsoft.Diagnostics.NETCore.Client;
using System.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Parsers;
using Microsoft.Diagnostics.Tracing;
namespace eventpipeclienttest
{
class Program
{
static async Task Main(string[] args)
{
var client = new DiagnosticsClient(int.Parse(args[0]));
var providers = new[]
{
new EventPipeProvider("Microsoft-Diagnostics-DiagnosticSource", EventLevel.Informational, 0x3)
};
Console.WriteLine($"jkl");
using (var session = client.StartEventPipeSession(providers, false))
{
Console.WriteLine($"ghi");
using (var evsrc = new EventPipeEventSource(session.EventStream))
{
Console.WriteLine($"abc");
evsrc.Dynamic.All += (traceevt) =>
{
try
{
Console.WriteLine($"{traceevt.ActivityID}, {traceevt.Channel}, {traceevt.ContainerID}, {traceevt.EventName}, {traceevt.ProviderName}, {traceevt.TaskName}, {traceevt.EventDataLength}, {traceevt.PayloadNames.Length}");
var sb = new StringBuilder();
// traceevt.ToXml(sb);
// Console.WriteLine($"{sb.ToString()}");
foreach (var pname in traceevt.PayloadNames)
{
var pobj = traceevt.PayloadByName(pname);
Console.WriteLine($" {pname} = '{pobj}'");
if (pobj is IDictionary<string, object>[] par)
{
// foreach(var tevname in tev.PayloadNames)
// {
// Console.WriteLine($" {tevname} = {tev.PayloadByName(tevname)}");
// }
foreach (var pdic in par)
{
// Console.WriteLine(" {0}", string.Join(",", pdic.Keys));
Console.WriteLine(" {0}",
string.Join(" : ", pdic.Select(x => x.Key + "=" + x.Value.ToString())));
}
}
}
}
catch (Exception e)
{
Console.WriteLine($"error in dynamic all:{e}");
}
};
Console.WriteLine($"def");
await Task.WhenAll(
Task.Run(() =>
{
Console.WriteLine($"begin process, press enter then exit");
Console.ReadLine();
evsrc.StopProcessing();
session.Stop();
Console.WriteLine($"stop process done");
}),
Task.Run(() =>
{
var ret = evsrc.Process();
Console.WriteLine($"process ret: {ret}");
})
).ConfigureAwait(false);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment