@@ -19,6 +19,8 @@ public enum Trace2Event
19
19
ChildStart = 3 ,
20
20
ChildExit = 4 ,
21
21
Error = 5 ,
22
+ RegionEnter = 6 ,
23
+ RegionLeave = 7 ,
22
24
}
23
25
24
26
/// <summary>
@@ -54,6 +56,40 @@ public class PerformanceFormatSpan
54
56
public int EndPadding { get ; set ; }
55
57
}
56
58
59
+ /// <summary>
60
+ /// Class that manages regions.
61
+ /// </summary>
62
+ public class Region : DisposableObject
63
+ {
64
+ private readonly ITrace2 _trace2 ;
65
+ private readonly string _category ;
66
+ private readonly string _label ;
67
+ private readonly string _filePath ;
68
+ private readonly int _lineNumber ;
69
+ private readonly string _message ;
70
+ private readonly DateTimeOffset _startTime ;
71
+
72
+ public Region ( ITrace2 trace2 , string category , string label , string filePath , int lineNumber , string message = "" )
73
+ {
74
+ _trace2 = trace2 ;
75
+ _category = category ;
76
+ _label = label ;
77
+ _filePath = filePath ;
78
+ _lineNumber = lineNumber ;
79
+ _message = message ;
80
+
81
+ _startTime = DateTimeOffset . UtcNow ;
82
+
83
+ _trace2 . WriteRegionEnter ( _category , _label , _message , _filePath , _lineNumber ) ;
84
+ }
85
+
86
+ protected override void ReleaseManagedResources ( )
87
+ {
88
+ double relativeTime = ( DateTimeOffset . UtcNow - _startTime ) . TotalSeconds ;
89
+ _trace2 . WriteRegionLeave ( relativeTime , _category , _label , _message , _filePath , _lineNumber ) ;
90
+ }
91
+ }
92
+
57
93
/// <summary>
58
94
/// Represents the application's TRACE2 tracing system.
59
95
/// </summary>
@@ -139,6 +175,59 @@ void WriteError(
139
175
string filePath = "" ,
140
176
[ System . Runtime . CompilerServices . CallerLineNumber ]
141
177
int lineNumber = 0 ) ;
178
+
179
+ /// <summary>
180
+ /// Creates a region and manages entry/leaving.
181
+ /// </summary>
182
+ /// <param name="category">Category of region.</param>
183
+ /// <param name="label">Description of region.</param>
184
+ /// <param name="message">Message associated with entering region.</param>
185
+ /// <param name="filePath">Path of the file this method is called from.</param>
186
+ /// <param name="lineNumber">Line number of file this method is called from.</param>
187
+ Region CreateRegion (
188
+ string category ,
189
+ string label ,
190
+ string message = "" ,
191
+ [ System . Runtime . CompilerServices . CallerFilePath ]
192
+ string filePath = "" ,
193
+ [ System . Runtime . CompilerServices . CallerLineNumber ]
194
+ int lineNumber = 0 ) ;
195
+
196
+ /// <summary>
197
+ /// Writes a region enter message to the trace writer.
198
+ /// </summary>
199
+ /// <param name="category">Category of region.</param>
200
+ /// <param name="label">Description of region.</param>
201
+ /// <param name="message">Message associated with entering region.</param>
202
+ /// <param name="filePath">Path of the file this method is called from.</param>
203
+ /// <param name="lineNumber">Line number of file this method is called from.</param>
204
+ void WriteRegionEnter (
205
+ string category ,
206
+ string label ,
207
+ string message = "" ,
208
+ [ System . Runtime . CompilerServices . CallerFilePath ]
209
+ string filePath = "" ,
210
+ [ System . Runtime . CompilerServices . CallerLineNumber ]
211
+ int lineNumber = 0 ) ;
212
+
213
+ /// <summary>
214
+ /// Writes a region leave message to the trace writer.
215
+ /// </summary>
216
+ /// <param name="relativeTime">Time of region execution.</param>
217
+ /// <param name="category">Category of region.</param>
218
+ /// <param name="label">Description of region.</param>
219
+ /// <param name="message">Message associated with entering region.</param>
220
+ /// <param name="filePath">Path of the file this method is called from.</param>
221
+ /// <param name="lineNumber">Line number of file this method is called from.</param>
222
+ void WriteRegionLeave (
223
+ double relativeTime ,
224
+ string category ,
225
+ string label ,
226
+ string message = "" ,
227
+ [ System . Runtime . CompilerServices . CallerFilePath ]
228
+ string filePath = "" ,
229
+ [ System . Runtime . CompilerServices . CallerLineNumber ]
230
+ int lineNumber = 0 ) ;
142
231
}
143
232
144
233
public class Trace2 : DisposableObject , ITrace2
@@ -307,6 +396,64 @@ public void WriteError(
307
396
} ) ;
308
397
}
309
398
399
+ public Region CreateRegion (
400
+ string category ,
401
+ string label ,
402
+ string message ,
403
+ string filePath ,
404
+ int lineNumber )
405
+ {
406
+ return new Region ( this , category , label , filePath , lineNumber , message ) ;
407
+ }
408
+
409
+ public void WriteRegionEnter (
410
+ string category ,
411
+ string label ,
412
+ string message = "" ,
413
+ string filePath = "" ,
414
+ int lineNumber = 0 )
415
+ {
416
+ WriteMessage ( new RegionEnterMessage ( )
417
+ {
418
+ Event = Trace2Event . RegionEnter ,
419
+ Sid = _sid ,
420
+ Time = DateTimeOffset . UtcNow ,
421
+ Category = category ,
422
+ Label = label ,
423
+ Message = message == "" ? label : message ,
424
+ Thread = BuildThreadName ( ) ,
425
+ File = Path . GetFileName ( filePath ) ,
426
+ Line = lineNumber ,
427
+ ElapsedTime = ( DateTimeOffset . UtcNow - _applicationStartTime ) . TotalSeconds ,
428
+ Depth = ProcessManager . Depth
429
+ } ) ;
430
+ }
431
+
432
+ public void WriteRegionLeave (
433
+ double relativeTime ,
434
+ string category ,
435
+ string label ,
436
+ string message = "" ,
437
+ string filePath = "" ,
438
+ int lineNumber = 0 )
439
+ {
440
+ WriteMessage ( new RegionLeaveMessage ( )
441
+ {
442
+ Event = Trace2Event . RegionLeave ,
443
+ Sid = _sid ,
444
+ Time = DateTimeOffset . UtcNow ,
445
+ Category = category ,
446
+ Label = label ,
447
+ Message = message == "" ? label : message ,
448
+ Thread = BuildThreadName ( ) ,
449
+ File = Path . GetFileName ( filePath ) ,
450
+ Line = lineNumber ,
451
+ ElapsedTime = ( DateTimeOffset . UtcNow - _applicationStartTime ) . TotalSeconds ,
452
+ RelativeTime = relativeTime ,
453
+ Depth = ProcessManager . Depth
454
+ } ) ;
455
+ }
456
+
310
457
protected override void ReleaseManagedResources ( )
311
458
{
312
459
lock ( _writersLock )
0 commit comments