1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics ;
4
+ using System . Diagnostics . Tracing ;
4
5
using System . Management . Automation ;
5
6
using System . Management . Automation . Language ;
6
7
7
8
namespace Profiler
8
9
{
10
+ [ EventSource ( Name = "Profiler" ) ]
11
+ public class ProfilerEventSource : EventSource
12
+ {
13
+ private ProfilerEventSource ( ) { }
14
+
15
+ public void WriteStart ( int index , string functionName , string text )
16
+ {
17
+ WriteEvent ( 1 , index , AsNonNull ( functionName ) , Max100Chars ( text ) ) ;
18
+ }
19
+
20
+ public void WriteStop ( int index , string functionName , string text )
21
+ {
22
+ WriteEvent ( 2 , index , AsNonNull ( functionName ) , Max100Chars ( text ) ) ;
23
+ }
24
+
25
+ public static ProfilerEventSource Log { get ; } = new ProfilerEventSource ( ) ;
26
+
27
+ private static string AsNonNull ( string functionName )
28
+ {
29
+ return functionName ?? string . Empty ;
30
+ }
31
+
32
+ private static string Max100Chars ( string text )
33
+ {
34
+ text = AsNonNull ( text ) ;
35
+ if ( text . Length > 100 )
36
+ {
37
+ return text . Substring ( 0 , 100 ) ;
38
+ }
39
+
40
+ return text ;
41
+ }
42
+ }
43
+
9
44
public class ProfilerTracer : ITracer
10
45
{
11
46
// timespan ticks are 10k per millisecond, but the stopwatch can have different resolution
@@ -41,9 +76,9 @@ public void Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level, stri
41
76
if ( _index > 0 )
42
77
{
43
78
SetSelfDurationAndAddToHits ( ref _previousHit , timestamp ) ;
79
+ ProfilerEventSource . Log . WriteStop ( _previousHit . Index , _previousHit . Function , _previousHit . Text ) ;
44
80
}
45
81
46
-
47
82
#if ! POWERSHELL3
48
83
if ( ! ScriptBlocks . ContainsKey ( scriptBlock . Id ) )
49
84
{
@@ -59,7 +94,6 @@ public void Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level, stri
59
94
}
60
95
}
61
96
#endif
62
-
63
97
// overwrite the previous event because we already scraped it
64
98
_previousHit = new Hit ( ) ;
65
99
_previousHit . StartTime = TimeSpan . FromTicks ( timestamp ) ;
@@ -85,6 +119,7 @@ public void Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level, stri
85
119
_previousHit . Level = level ;
86
120
87
121
_index ++ ;
122
+ ProfilerEventSource . Log . WriteStart ( _previousHit . Index , _previousHit . Function , _previousHit . Text ) ;
88
123
}
89
124
90
125
private void SetSelfDurationAndAddToHits ( ref Hit eventRecord , long timestamp )
0 commit comments