-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTracePeekController.cs
92 lines (80 loc) · 3.81 KB
/
TracePeekController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using Microsoft.Diagnostics.Tracing.Session;
using Microsoft.Diagnostics.Tracing;
using System.Threading.Tasks;
using System.Threading;
using System.Management.Automation;
using System.Diagnostics.Tracing;
namespace TracePeek
{
public class TracePeekController : IDisposable
{
public TracePeekController(string sessionName = "TracePeek_DefaultSession")
{
SessionName = sessionName;
ThisTraceEventSession = new TraceEventSession(sessionName);
}
private static EventSource log = new EventSource("TracePeek");
public TraceEventSession ThisTraceEventSession {get;}
public string SessionName{ get;}
public bool IsCancellationRequested { get => isCancellationRequested; }
private bool isCancellationRequested = false;
public void EnableProviders(ICollection<string> providers)
{
foreach(var providerIdentifier in providers)
{
var tokens = providerIdentifier.Split(':');
var keywords = tokens.Length > 1 ? Convert.ToUInt64(tokens[1], 16) : ulong.MaxValue;
var level = tokens.Length > 2 ? (TraceEventLevel)Convert.ToUInt16(tokens[2], 16) : TraceEventLevel.Always;
ThisTraceEventSession.EnableProvider(tokens[0], level, keywords);
}
}
public delegate void OnEvent(PSObject tracePeekEventProjection);
public event OnEvent OnTracePeekEvent;
public void Dispose()
{
StopPeek();
}
public Task StartPeek(TracePeekProjectionStyle projectionStyle = TracePeekProjectionStyle.NamedPayloadProperties)
{
if(projectionStyle == TracePeekProjectionStyle.NamedPayloadProperties)
ThisTraceEventSession.Source.Dynamic.All += traceEvent => {
if(OnTracePeekEvent != null) OnTracePeekEvent(TracePeekEvent.CreateWithNamedPayloadProperties(traceEvent));};
else if(projectionStyle == TracePeekProjectionStyle.NumberedPayloadProperties)
ThisTraceEventSession.Source.Dynamic.All += traceEvent => {
if(OnTracePeekEvent != null) OnTracePeekEvent(TracePeekEvent.CreateWithNumberedPayloadProperties(traceEvent));};
else if(projectionStyle == TracePeekProjectionStyle.NumberedNestedPayloadProperties)
ThisTraceEventSession.Source.Dynamic.All += traceEvent => {
if(OnTracePeekEvent != null) OnTracePeekEvent(TracePeekEvent.CreateWithNumberedNestedPayloadProperties(traceEvent));};
else if(projectionStyle == TracePeekProjectionStyle.WithoutPayloadProperties)
ThisTraceEventSession.Source.Dynamic.All += traceEvent => {
if(OnTracePeekEvent != null) OnTracePeekEvent(new PSObject(TracePeekEvent.CreateWithoutPayloadProperties(traceEvent)));};
else throw new ArgumentOutOfRangeException(nameof(projectionStyle));
return Task.Run( () => {
Task.Run(() => {
while(!isCancellationRequested)
{
log.Write("Hearbeat", new { timestampAtCallsite = DateTime.UtcNow });
Task.Delay(5000).Wait();
}
});
ThisTraceEventSession.Source.Process();
});
}
public void StopPeek()
{
isCancellationRequested = true;
if(ThisTraceEventSession != null)
ThisTraceEventSession.StopOnDispose = true;
ThisTraceEventSession.Dispose();
}
}
public enum TracePeekProjectionStyle{
None = 0,
WithoutPayloadProperties,
NamedPayloadProperties,
NumberedPayloadProperties,
NumberedNestedPayloadProperties
}
}