Skip to content

Instantly share code, notes, and snippets.

View SourceGetter.cs
public ETWTraceEventSource Source
{
get
{
if (m_source == null)
{
... // long code
m_source = new ETWTraceEventSource(SessionName, TraceEventSourceType.Session);
}
return m_source;
View IsMonitoredEvent.cs
private bool IsMonitoredEvent(TraceEvent traceEvent)
{
var isMonitored = traceEvent.ProcessID == _monitoredPid;
if (isMonitored)
{
NotifyProcessedEvent(traceEvent);
}
return isMonitored;
}
View ListenToETW.cs
private void ListenToEtw(TraceEventSession etwSession)
{
try
{
// this call is blocking... until ewtSession.Stop is called (done in Dispose)
etwSession.Source.Process();
}
finally
{
etwSession.Dispose();
View GetGenerationSizes.cs
private long[] GetGenerationSizes(TraceGC gc, bool before)
{
var sizes = new long[4];
if (gc.PerHeapHistories == null)
{
return sizes;
}
for (int heap = 0; heap < gc.PerHeapHistories.Count; heap++)
{
View OnGarbageCollection.cs
private void OnGarbageCollection(object sender, GarbageCollectionArgs e)
{
_line.Clear();
_line.AppendFormat("{0},", e.StartRelativeMSec.ToString());
_line.AppendFormat("{0},", e.Number.ToString());
_line.AppendFormat("{0},", e.Generation.ToString());
_line.AppendFormat("{0},", e.Type);
_line.AppendFormat("{0},", e.Reason);
_line.AppendFormat("{0},", e.IsCompacting.ToString());
_line.AppendFormat("{0},", e.SuspensionDuration.ToString());
View OnStartStop.cs
protected override void OnStart()
{
string sessionName = $"GcLogEtwSession_{_pid.ToString()}_{Guid.NewGuid().ToString()}";
Console.WriteLine($"Starting {sessionName}...\r\n");
_userSession = new TraceEventSession(sessionName, TraceEventSessionOptions.Create);
Task.Run(() =>
{
// only want to receive GC event
ClrEventsManager manager = new ClrEventsManager(_userSession, _pid, EventFilter.GC);
View GetProcessGcLog.cs
public static EtwGcLog GetProcessGcLog(int pid)
{
EtwGcLog gcLog = null;
try
{
var process = Process.GetProcessById(pid);
process.Dispose();
gcLog = new EtwGcLog(pid);
}
View GetUniqueFilename.cs
private static string GetUniqueFilename(int pid)
{
var now = DateTime.Now;
string filename = Path.Combine(Environment.CurrentDirectory,
$"{pid.ToString()}_{now.Year}{now.Month}{now.Day}_{now.Hour}{now.Minute}{now.Second}.csv"
);
return filename;
}
View MainForGcLog.cs
EtwGcLog gcLog = EtwGcLog.GetProcessGcLog(pid);
var filename = GetUniqueFilename(pid);
gcLog.Start(filename);
// in a simple Console application, wait for the user to press ENTER.
// in a more realistic case, keep track of the EtwLog instance and
// call Stop to end the processing of events when needed.
gcLog.Stop();
View GcLogBase.cs
public abstract class GcLogBase : IGcLog
{
protected string Filename;
private StreamWriter _fileWriter;
public void Start(string filename)
{
if (string.IsNullOrEmpty(filename))
throw new ArgumentNullException(nameof(filename));
You can’t perform that action at this time.