@@ -22,6 +22,8 @@ public enum Trace2Event
22
22
Version = 0 ,
23
23
[ EnumMember ( Value = "start" ) ]
24
24
Start = 1 ,
25
+ [ EnumMember ( Value = "exit" ) ]
26
+ Exit = 2
25
27
}
26
28
27
29
public class Trace2Settings
@@ -49,6 +51,16 @@ void Start(TextWriter error,
49
51
string appPath ,
50
52
[ System . Runtime . CompilerServices . CallerFilePath ] string filePath = "" ,
51
53
[ System . Runtime . CompilerServices . CallerLineNumber ] int lineNumber = 0 ) ;
54
+
55
+ /// <summary>
56
+ /// Shut down TRACE2 tracing by writing Exit event and disposing of writers.
57
+ /// </summary>
58
+ /// <param name="exitCode">The exit code of the GCM application.</param>
59
+ /// <param name="filePath">Path of the file this method is called from.</param>
60
+ /// <param name="lineNumber">Line number of file this method is called from.</param>
61
+ void Stop ( int exitCode ,
62
+ [ System . Runtime . CompilerServices . CallerFilePath ] string filePath = "" ,
63
+ [ System . Runtime . CompilerServices . CallerLineNumber ] int lineNumber = 0 ) ;
52
64
}
53
65
54
66
public class Trace2 : DisposableObject , ITrace2
@@ -93,6 +105,12 @@ public void Start(TextWriter error,
93
105
WriteStart ( appPath , filePath , lineNumber ) ;
94
106
}
95
107
108
+ public void Stop ( int exitCode , string filePath , int lineNumber )
109
+ {
110
+ WriteExit ( exitCode , filePath , lineNumber ) ;
111
+ ReleaseManagedResources ( ) ;
112
+ }
113
+
96
114
protected override void ReleaseManagedResources ( )
97
115
{
98
116
lock ( _writersLock )
@@ -233,6 +251,22 @@ private void WriteStart(
233
251
} ) ;
234
252
}
235
253
254
+ private void WriteExit ( int code , string filePath = "" , int lineNumber = 0 )
255
+ {
256
+ EnsureArgument . NotNull ( code , nameof ( code ) ) ;
257
+
258
+ WriteMessage ( new ExitMessage ( )
259
+ {
260
+ Event = Trace2Event . Exit ,
261
+ Sid = _sid ,
262
+ Time = DateTimeOffset . Now ,
263
+ File = Path . GetFileName ( filePath ) . ToLower ( ) ,
264
+ Line = lineNumber ,
265
+ Code = code ,
266
+ ElapsedTime = ( DateTimeOffset . UtcNow - _applicationStartTime ) . TotalSeconds
267
+ } ) ;
268
+ }
269
+
236
270
private void AddWriter ( ITrace2Writer writer )
237
271
{
238
272
ThrowIfDisposed ( ) ;
@@ -362,3 +396,27 @@ public override string ToNormalString()
362
396
return BuildNormalString ( string . Join ( " " , Argv ) ) ;
363
397
}
364
398
}
399
+
400
+ public class ExitMessage : Trace2Message
401
+ {
402
+ [ JsonProperty ( "t_abs" , Order = 7 ) ]
403
+ public double ElapsedTime { get ; set ; }
404
+
405
+ [ JsonProperty ( "code" , Order = 8 ) ]
406
+ public int Code { get ; set ; }
407
+
408
+ public override string ToJson ( )
409
+ {
410
+ return JsonConvert . SerializeObject ( this ,
411
+ new StringEnumConverter ( ) ,
412
+ new IsoDateTimeConverter ( )
413
+ {
414
+ DateTimeFormat = TimeFormat
415
+ } ) ;
416
+ }
417
+
418
+ public override string ToNormalString ( )
419
+ {
420
+ return BuildNormalString ( $ "elapsed:{ ElapsedTime } code:{ Code } ") ;
421
+ }
422
+ }
0 commit comments