1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . ComponentModel ;
3
4
using System . IO ;
4
5
using System . IO . Pipes ;
6
+ using System . Reflection . Emit ;
5
7
using System . Text ;
6
8
using System . Threading ;
7
9
@@ -19,6 +21,8 @@ public enum Trace2Event
19
21
ChildStart = 3 ,
20
22
ChildExit = 4 ,
21
23
Error = 5 ,
24
+ RegionEnter = 6 ,
25
+ RegionLeave = 7 ,
22
26
}
23
27
24
28
/// <summary>
@@ -54,6 +58,42 @@ public class PerformanceFormatSpan
54
58
public int EndPadding { get ; set ; }
55
59
}
56
60
61
+ /// <summary>
62
+ /// Class that manages regions.
63
+ /// </summary>
64
+ public class Region : DisposableObject
65
+ {
66
+ private readonly ITrace2 _trace2 ;
67
+ private readonly string _category ;
68
+ private readonly string _label ;
69
+ private readonly string _filePath ;
70
+ private readonly int _lineNumber ;
71
+ private readonly string _message ;
72
+ private readonly string _thread ;
73
+ private readonly DateTimeOffset _startTime ;
74
+
75
+ public Region ( ITrace2 trace2 , string category , string label , string filePath , int lineNumber , string message = "" )
76
+ {
77
+ _trace2 = trace2 ;
78
+ _category = category ;
79
+ _label = label ;
80
+ _filePath = filePath ;
81
+ _lineNumber = lineNumber ;
82
+ _message = message ;
83
+
84
+ _startTime = DateTimeOffset . UtcNow ;
85
+ _thread = ThreadHelpers . BuildThreadName ( ) ;
86
+
87
+ _trace2 . WriteRegionEnter ( _category , _label , _thread , _message , _filePath , _lineNumber ) ;
88
+ }
89
+
90
+ protected override void ReleaseManagedResources ( )
91
+ {
92
+ double relativeTime = ( DateTimeOffset . UtcNow - _startTime ) . TotalSeconds ;
93
+ _trace2 . WriteRegionLeave ( relativeTime , _category , _label , _thread , _message , _filePath , _lineNumber ) ;
94
+ }
95
+ }
96
+
57
97
/// <summary>
58
98
/// Represents the application's TRACE2 tracing system.
59
99
/// </summary>
@@ -150,6 +190,51 @@ void WriteError(
150
190
string filePath = "" ,
151
191
[ System . Runtime . CompilerServices . CallerLineNumber ]
152
192
int lineNumber = 0 ) ;
193
+
194
+ Region CreateRegion (
195
+ string category ,
196
+ string label ,
197
+ [ System . Runtime . CompilerServices . CallerFilePath ]
198
+ string filePath = "" ,
199
+ [ System . Runtime . CompilerServices . CallerLineNumber ]
200
+ int lineNumber = 0 ,
201
+ string message = "" ) ;
202
+
203
+ /// <summary>
204
+ /// Writes a region enter message to the trace writer.
205
+ /// </summary>
206
+ /// <param name="category">Category of region.</param>
207
+ /// <param name="label">Description of region.</param>
208
+ /// <param name="threadName">Name of the currently-executing thread.</param>
209
+ /// <param name="message">Message associated with entering region.</param>
210
+ /// <param name="filePath">Path of the file this method is called from.</param>
211
+ /// <param name="lineNumber">Line number of file this method is called from.</param>
212
+ void WriteRegionEnter (
213
+ string category ,
214
+ string label ,
215
+ string threadName ,
216
+ string message = "" ,
217
+ string filePath = "" ,
218
+ int lineNumber = 0 ) ;
219
+
220
+ /// <summary>
221
+ /// Writes a region leave message to the trace writer.
222
+ /// </summary>
223
+ /// <param name="relativeTime">Time of region execution.</param>
224
+ /// <param name="category">Category of region.</param>
225
+ /// <param name="label">Description of region.</param>
226
+ /// <param name="threadName">Name of the currently-executing thread.</param>
227
+ /// <param name="message">Message associated with entering region.</param>
228
+ /// <param name="filePath">Path of the file this method is called from.</param>
229
+ /// <param name="lineNumber">Line number of file this method is called from.</param>
230
+ void WriteRegionLeave (
231
+ double relativeTime ,
232
+ string category ,
233
+ string label ,
234
+ string threadName ,
235
+ string message = "" ,
236
+ string filePath = "" ,
237
+ int lineNumber = 0 ) ;
153
238
}
154
239
155
240
public class Trace2 : DisposableObject , ITrace2
@@ -322,6 +407,66 @@ public void WriteError(
322
407
} ) ;
323
408
}
324
409
410
+ public Region CreateRegion (
411
+ string category ,
412
+ string label ,
413
+ string filePath ,
414
+ int lineNumber ,
415
+ string message = "" )
416
+ {
417
+ return new Region ( this , category , label , filePath , lineNumber , message ) ;
418
+ }
419
+
420
+ public void WriteRegionEnter (
421
+ string category ,
422
+ string label ,
423
+ string threadName ,
424
+ string message = "" ,
425
+ string filePath = "" ,
426
+ int lineNumber = 0 )
427
+ {
428
+ WriteMessage ( new RegionEnterMessage ( )
429
+ {
430
+ Event = Trace2Event . RegionEnter ,
431
+ Sid = _sid ,
432
+ Time = DateTimeOffset . UtcNow ,
433
+ Category = category ,
434
+ Label = label ,
435
+ Message = message == "" ? label : message ,
436
+ Thread = threadName ,
437
+ File = Path . GetFileName ( filePath ) ,
438
+ Line = lineNumber ,
439
+ ElapsedTime = ( DateTimeOffset . UtcNow - _applicationStartTime ) . TotalSeconds ,
440
+ Depth = ProcessManager . Depth
441
+ } ) ;
442
+ }
443
+
444
+ public void WriteRegionLeave (
445
+ double relativeTime ,
446
+ string category ,
447
+ string label ,
448
+ string threadName ,
449
+ string message = "" ,
450
+ string filePath = "" ,
451
+ int lineNumber = 0 )
452
+ {
453
+ WriteMessage ( new RegionLeaveMessage ( )
454
+ {
455
+ Event = Trace2Event . RegionLeave ,
456
+ Sid = _sid ,
457
+ Time = DateTimeOffset . UtcNow ,
458
+ Category = category ,
459
+ Label = label ,
460
+ Message = message == "" ? label : message ,
461
+ Thread = threadName ,
462
+ File = Path . GetFileName ( filePath ) ,
463
+ Line = lineNumber ,
464
+ ElapsedTime = ( DateTimeOffset . UtcNow - _applicationStartTime ) . TotalSeconds ,
465
+ RelativeTime = relativeTime ,
466
+ Depth = ProcessManager . Depth
467
+ } ) ;
468
+ }
469
+
325
470
protected override void ReleaseManagedResources ( )
326
471
{
327
472
lock ( _writersLock )
0 commit comments