Skip to content

Commit a74b9c0

Browse files
author
Lessley Dennington
committed
trace2: write exit event
Write the TRACE2 exit event, which records the application exit code and execution time in milliseconds.
1 parent 20d3891 commit a74b9c0

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

src/shared/Core/ApplicationBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ public Task<int> RunAsync(string[] args)
107107
}
108108
}
109109

110-
return RunInternalAsync(args);
110+
var exitCode = RunInternalAsync(args);
111+
return exitCode;
111112
}
112113

113114
protected abstract Task<int> RunInternalAsync(string[] args);

src/shared/Core/Trace2.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ namespace GitCredentialManager;
1515
public enum Event
1616
{
1717
Version = 0,
18-
Start = 1
18+
Start = 1,
19+
Exit = 2
1920
}
2021

2122
/// <summary>
@@ -77,6 +78,22 @@ void WriteStart(
7778
long elapsedTime,
7879
[System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
7980
[System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0);
81+
82+
/// <summary>
83+
/// Writes exit code and execution time to trace writer.
84+
/// </summary>
85+
/// <param name="code">Application exit code.</param>
86+
/// <param name="timeInMilliseconds">Elapsed time in milliseconds since GCM started.</param>
87+
/// <param name="filePath">Path of the file this method is called from.</param>
88+
/// <param name="lineNumber">Line number of file this method is called from.</param>
89+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
90+
void WriteExit(
91+
int code,
92+
long elapsedTime,
93+
[System.Runtime.CompilerServices.CallerFilePath]
94+
string filePath = "",
95+
[System.Runtime.CompilerServices.CallerLineNumber]
96+
int lineNumber = 0);
8097
}
8198

8299
public class Trace2 : DisposableObject, ITrace2
@@ -186,6 +203,26 @@ public void WriteStart(
186203
});
187204
}
188205

206+
public void WriteExit(
207+
int code,
208+
long elapsedTime,
209+
string filePath = "",
210+
int lineNumber = 0)
211+
{
212+
EnsureArgument.NotNull(code, nameof(code));
213+
214+
WriteMessage(new ExitCollectorMessage()
215+
{
216+
Event = Event.Exit,
217+
Sid = _sid,
218+
Time = DateTime.UtcNow.ToString("o"),
219+
File = filePath,
220+
Line = lineNumber,
221+
Code = code,
222+
ElapsedTime = elapsedTime
223+
});
224+
}
225+
189226
private void WriteMessage(CollectorMessage message)
190227
{
191228
ThrowIfDisposed();
@@ -262,3 +299,18 @@ public override string ToJson()
262299
new StringEnumConverter(new CamelCaseNamingStrategy()));
263300
}
264301
}
302+
303+
public class ExitCollectorMessage : CollectorMessage
304+
{
305+
[JsonProperty("t_abs")]
306+
public long ElapsedTime { get; set; }
307+
308+
[JsonProperty("code")]
309+
public int Code { get; set; }
310+
311+
public override string ToJson()
312+
{
313+
return JsonConvert.SerializeObject(this,
314+
new StringEnumConverter(new CamelCaseNamingStrategy()));
315+
}
316+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ public static void Main(string[] args)
7979
.ConfigureAwait(false)
8080
.GetAwaiter()
8181
.GetResult();
82-
82+
83+
84+
if (context.Settings.GetTrace2TracingEnabled(out string trace2Value))
85+
{
86+
context.Trace2.WriteExit(exitCode,
87+
DateTimeOffset.UtcNow.ToUnixTimeSeconds() - context.Trace2.ApplicationStartTime);
88+
}
8389
Environment.Exit(exitCode);
8490
}
8591
}

src/shared/TestInfrastructure/Objects/NullTrace.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public void WriteStart(
7373
string filePath = "",
7474
int lineNumber = 0) { }
7575

76+
public void WriteExit(
77+
int code,
78+
long elapsedTime,
79+
string filePath = "",
80+
int lineNumber = 0) { }
81+
7682
#endregion
7783

7884
#region IDisposable

0 commit comments

Comments
 (0)