Created
April 7, 2021 19:07
-
-
Save shirhatti/80a2cc15ff74dd0e4fe5d3e0772a2de5 to your computer and use it in GitHub Desktop.
Activities via EventPipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Diagnostics.NETCore.Client" Version="0.2.217401" /> | |
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="2.0.66" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Diagnostics.NETCore.Client; | |
using System.Diagnostics.Tracing; | |
using System.Collections.Generic; | |
using Microsoft.Diagnostics.Tracing; | |
using System; | |
using System.Collections; | |
const long DiagnosticSourceKeywords_Messages = 0x1; | |
const long DiagnosticSourceKeywords_Events = 0x2; | |
//const string DiagnosticFilterString = "\"" + | |
// "Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Start@Activity1Start:-" + | |
// "Request.Scheme" + | |
// ";Request.Host" + | |
// ";Request.PathBase" + | |
// ";Request.QueryString" + | |
// ";Request.Path" + | |
// ";Request.Method" + | |
// ";ActivityStartTime=*Activity.StartTimeUtc.Ticks" + | |
// ";ActivityParentId=*Activity.ParentId" + | |
// ";ActivityId=*Activity.Id" + | |
// ";ActivitySpanId=*Activity.SpanId" + | |
// ";ActivityTraceId=*Activity.TraceId" + | |
// ";ActivityParentSpanId=*Activity.ParentSpanId" + | |
// ";ActivityIdFormat=*Activity.IdFormat" + | |
// "\r\n" + | |
//"Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop@Activity1Stop:-" + | |
// "Response.StatusCode" + | |
// ";ActivityDuration=*Activity.Duration.Ticks" + | |
// ";ActivityId=*Activity.Id" + | |
//"\r\n" + | |
//"HttpHandlerDiagnosticListener/System.Net.Http.HttpRequestOut@Event:-" + | |
//"\r\n" + | |
//"HttpHandlerDiagnosticListener/System.Net.Http.HttpRequestOut.Start@Activity2Start:-" + | |
// "Request.RequestUri" + | |
// ";Request.Method" + | |
// ";Request.RequestUri.Host" + | |
// ";Request.RequestUri.Port" + | |
// ";ActivityStartTime=*Activity.StartTimeUtc.Ticks" + | |
// ";ActivityId=*Activity.Id" + | |
// ";ActivitySpanId=*Activity.SpanId" + | |
// ";ActivityTraceId=*Activity.TraceId" + | |
// ";ActivityParentSpanId=*Activity.ParentSpanId" + | |
// ";ActivityIdFormat=*Activity.IdFormat" + | |
// ";ActivityId=*Activity.Id" + | |
// "\r\n" + | |
//"HttpHandlerDiagnosticListener/System.Net.Http.HttpRequestOut.Stop@Activity2Stop:-" + | |
// ";ActivityDuration=*Activity.Duration.Ticks" + | |
// ";ActivityId=*Activity.Id" + | |
//"\r\n" + | |
//"\""; | |
const string DiagnosticFilterString = "Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Start@Activity1Start:-TraceIdentifier;Request.Method;Request.Host;Request.Path;Request.QueryString\r\n" + | |
"Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop@Activity1Stop:-TraceIdentifier;Response.StatusCode"; | |
const int pid = 32128; | |
var provider = new EventPipeProvider( | |
name: "Microsoft-Diagnostics-DiagnosticSource", | |
eventLevel: EventLevel.Verbose, | |
keywords: DiagnosticSourceKeywords_Messages | | |
DiagnosticSourceKeywords_Events, | |
arguments: new Dictionary<string, string> { | |
{ | |
"FilterAndPayloadSpecs", DiagnosticFilterString | |
} | |
} | |
); | |
var reader = new DiagnosticsClient(pid); | |
var session = reader.StartEventPipeSession(provider, requestRundown: false); | |
using var source = new EventPipeEventSource(session.EventStream); | |
source.Dynamic.All += (eventData) => | |
{ | |
Console.WriteLine($"Provider = {eventData.ProviderName} ID = {eventData.ID} Name = {eventData.EventName}"); | |
for (var i = 0; i < eventData.PayloadNames.Length; i++) | |
{ | |
if (eventData.PayloadNames[i] == "Arguments") | |
{ | |
Console.WriteLine($"\t{eventData.PayloadNames[i]}:"); | |
foreach (var value in (IEnumerable)eventData.PayloadValue(i)) | |
{ | |
Console.WriteLine($"\t\t{value}"); | |
} | |
} | |
else | |
{ | |
Console.WriteLine($"\t{eventData.PayloadNames[i]}: {eventData.PayloadValue(i)}"); | |
} | |
} | |
}; | |
Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e) | |
{ | |
session.Dispose(); | |
e.Cancel = true; | |
}; | |
source.Process(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment