Skip to content

Commit 72fd03f

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 003890e commit 72fd03f

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

src/shared/Core/ApplicationBase.cs

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

109-
return RunInternalAsync(args);
109+
var exitCode = RunInternalAsync(args);
110+
return exitCode;
110111
}
111112

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

src/shared/Core/Trace2.cs

Lines changed: 50 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>
@@ -70,6 +71,20 @@ void WriteVersion(
7071
void WriteStart(
7172
[System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
7273
[System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0);
74+
75+
/// <summary>
76+
/// Writes exit code and execution time to trace writer.
77+
/// </summary>
78+
/// <param name="code">Application exit code.</param>
79+
/// <param name="filePath">Path of the file this method is called from.</param>
80+
/// <param name="lineNumber">Line number of file this method is called from.</param>
81+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
82+
void WriteExit(
83+
int code,
84+
[System.Runtime.CompilerServices.CallerFilePath]
85+
string filePath = "",
86+
[System.Runtime.CompilerServices.CallerLineNumber]
87+
int lineNumber = 0);
7388
}
7489

7590
public class Trace2 : DisposableObject, ITrace2
@@ -177,6 +192,25 @@ public void WriteStart(
177192
});
178193
}
179194

195+
public void WriteExit(
196+
int code,
197+
string filePath = "",
198+
int lineNumber = 0)
199+
{
200+
EnsureArgument.NotNull(code, nameof(code));
201+
202+
WriteMessage(new ExitCollectorMessage()
203+
{
204+
Event = Event.Exit,
205+
Sid = _sid,
206+
Time = DateTime.UtcNow.ToString("o"),
207+
File = filePath,
208+
Line = lineNumber,
209+
Code = code,
210+
ElapsedTime = DateTimeOffset.UtcNow.ToUnixTimeMicroseconds() - _applicationStartTime
211+
});
212+
}
213+
180214
private void WriteMessage(CollectorMessage message)
181215
{
182216
ThrowIfDisposed();
@@ -253,3 +287,18 @@ public override string ToJson()
253287
new StringEnumConverter(new CamelCaseNamingStrategy()));
254288
}
255289
}
290+
291+
public class ExitCollectorMessage : CollectorMessage
292+
{
293+
[JsonProperty("t_abs")]
294+
public long ElapsedTime { get; set; }
295+
296+
[JsonProperty("code")]
297+
public int Code { get; set; }
298+
299+
public override string ToJson()
300+
{
301+
return JsonConvert.SerializeObject(this,
302+
new StringEnumConverter(new CamelCaseNamingStrategy()));
303+
}
304+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ 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+
}
8388
Environment.Exit(exitCode);
8489
}
8590
}

src/shared/TestInfrastructure/Objects/NullTrace.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public void WriteStart(
7070
string filePath = "",
7171
int lineNumber = 0) { }
7272

73+
public void WriteExit(
74+
int code,
75+
string filePath = "",
76+
int lineNumber = 0) { }
77+
7378
#endregion
7479

7580
#region IDisposable

0 commit comments

Comments
 (0)