sharplib/logging/GC.cs
Marc Hernandez 32d6c29443 feat: Add RuntimeGCEventsPrinter class for monitoring GC events
x) Implement PrintRuntimeGCEvents method to monitor GC events in a specified process
x) Refactor logs for structured logging using interpolated strings
x) Introduce TraceLogMonitor class for real-time monitoring of exceptions and module loads
2024-05-27 01:13:43 -07:00

54 lines
1.6 KiB
C#

using Microsoft.Diagnostics.NETCore.Client;
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.EventPipe;
using Microsoft.Diagnostics.Tracing.Parsers;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.Tracing;
public class RuntimeGCEventsPrinter
{
public static void PrintRuntimeGCEvents(int processId)
{
var providers = new List<EventPipeProvider>()
{
new EventPipeProvider("Microsoft-Windows-DotNETRuntime",
EventLevel.Informational, (long)ClrTraceEventParser.Keywords.Default)
};
var client = new DiagnosticsClient(processId);
using (EventPipeSession session = client.StartEventPipeSession(providers, false))
{
var source = new EventPipeEventSource(session.EventStream);
var blacklist = ImmutableHashSet.Create( "Method/ILToNativeMap", "GC/BulkMovedObjectRanges", "GC/BulkSurvivingObjectRanges" );
var fnFilter = (TraceEvent te) => {
//Console.WriteLine(obj.ToString());
if( blacklist.Contains( te.EventName ) ) return;
log.info( $"{te}", cat: "clr" );
};
source.Clr.All += fnFilter;
source.Kernel.All += fnFilter;
source.Dynamic.All += fnFilter;
try
{
source.Process();
}
catch (Exception e)
{
Console.WriteLine("Error encountered while processing events");
Console.WriteLine(e.ToString());
}
}
}
}