Skip to content

Commit 028ad46

Browse files
author
Lessley Dennington
committed
trace2: write exit event
Add TRACE2 exit event, following the pattern established with the version and start events.
1 parent ddb7591 commit 028ad46

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/shared/Core/Trace2.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public enum Trace2Event
2222
Version = 0,
2323
[EnumMember(Value = "start")]
2424
Start = 1,
25+
[EnumMember(Value = "exit")]
26+
Exit = 2
2527
}
2628

2729
public class Trace2Settings
@@ -49,6 +51,16 @@ void Start(TextWriter error,
4951
string appPath,
5052
[System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
5153
[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);
5264
}
5365

5466
public class Trace2 : DisposableObject, ITrace2
@@ -93,6 +105,12 @@ public void Start(TextWriter error,
93105
WriteStart(appPath, filePath, lineNumber);
94106
}
95107

108+
public void Stop(int exitCode, string filePath, int lineNumber)
109+
{
110+
WriteExit(exitCode, filePath, lineNumber);
111+
ReleaseManagedResources();
112+
}
113+
96114
protected override void ReleaseManagedResources()
97115
{
98116
lock (_writersLock)
@@ -233,6 +251,22 @@ private void WriteStart(
233251
});
234252
}
235253

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+
236270
private void AddWriter(ITrace2Writer writer)
237271
{
238272
ThrowIfDisposed();
@@ -362,3 +396,27 @@ public override string ToNormalString()
362396
return BuildNormalString(string.Join(" ", Argv));
363397
}
364398
}
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+
}

src/shared/Git-Credential-Manager/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public static void Main(string[] args)
7575
.GetAwaiter()
7676
.GetResult();
7777

78+
context.Trace2.Stop(exitCode);
7879
Environment.Exit(exitCode);
7980
}
8081
}

src/shared/TestInfrastructure/Objects/NullTrace.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public void Start(TextWriter error,
5959
string filePath = "",
6060
int lineNumber = 0) { }
6161

62+
public void Stop(int exitCode, string fileName, int lineNumber) { }
63+
6264
#endregion
6365

6466
#region IDisposable

0 commit comments

Comments
 (0)