-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
BinaryLogger.cs
514 lines (448 loc) · 22.5 KB
/
BinaryLogger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.IO.Compression;
using Microsoft.Build.Framework;
using Microsoft.Build.Framework.Telemetry;
using Microsoft.Build.Shared;
using Microsoft.Build.Shared.FileSystem;
#nullable disable
namespace Microsoft.Build.Logging
{
/// <summary>
/// A logger that serializes all incoming BuildEventArgs in a compressed binary file (*.binlog). The file
/// can later be played back and piped into other loggers (file, console, etc) to reconstruct the log contents
/// as if a real build was happening. Additionally, this format can be read by tools for
/// analysis or visualization. Since the file format preserves structure, tools don't have to parse
/// text logs that erase a lot of useful information.
/// </summary>
/// <remarks>The logger is public so that it can be instantiated from MSBuild.exe via command-line switch.</remarks>
public sealed class BinaryLogger : ILogger
{
// version 2:
// - new BuildEventContext.EvaluationId
// - new record kinds: ProjectEvaluationStarted, ProjectEvaluationFinished
// version 3:
// - new ProjectImportedEventArgs.ImportIgnored
// version 4:
// - new TargetSkippedEventArgs
// - new TargetStartedEventArgs.BuildReason
// version 5:
// - new EvaluationFinished.ProfilerResult
// version 6:
// - Ids and parent ids for the evaluation locations
// version 7:
// - Include ProjectStartedEventArgs.GlobalProperties
// version 8:
// - This was used in a now-reverted change but is the same as 9.
// version 9:
// - new record kinds: EnvironmentVariableRead, PropertyReassignment, UninitializedPropertyRead
// version 10:
// - new record kinds:
// * String - deduplicate strings by hashing and write a string record before it's used
// * NameValueList - deduplicate arrays of name-value pairs such as properties, items and metadata
// in a separate record and refer to those records from regular records
// where a list used to be written in-place
// version 11:
// - new record kind: TaskParameterEventArgs
// version 12:
// - add GlobalProperties, Properties and Items on ProjectEvaluationFinished
// version 13:
// - don't log Message where it can be recovered
// - log arguments for LazyFormattedBuildEventArgs
// - TargetSkippedEventArgs: added OriginallySucceeded, Condition, EvaluatedCondition
// version 14:
// - TargetSkippedEventArgs: added SkipReason, OriginalBuildEventContext
// version 15:
// - new record kind: ResponseFileUsedEventArgs
// version 16:
// - AssemblyLoadBuildEventArgs
// version 17:
// - Added extended data for types implementing IExtendedBuildEventArgs
// version 18:
// - Making ProjectStartedEventArgs, ProjectEvaluationFinishedEventArgs, AssemblyLoadBuildEventArgs equal
// between de/serialization roundtrips.
// - Adding serialized events lengths - to support forward compatible reading
// version 19:
// - GeneratedFileUsedEventArgs exposed for brief period of time (so let's continue with 20)
// version 20:
// - TaskStartedEventArgs: Added TaskAssemblyLocation property
// version 21:
// - TaskParameterEventArgs: Added ParameterName and PropertyName properties
// version 22:
// - extend EnvironmentVariableRead with location where environment variable was used.
// version 23:
// - new record kinds: BuildCheckMessageEvent, BuildCheckWarningEvent, BuildCheckErrorEvent,
// BuildCheckTracingEvent, BuildCheckAcquisitionEvent, BuildSubmissionStartedEvent
// version 24:
// - new record kind: BuildCanceledEventArgs
// MAKE SURE YOU KEEP BuildEventArgsWriter AND StructuredLogViewer.BuildEventArgsWriter IN SYNC WITH THE CHANGES ABOVE.
// Both components must stay in sync to avoid issues with logging or event handling in the products.
// This should be never changed.
// The minimum version of the binary log reader that can read log of above version.
internal const int ForwardCompatibilityMinimalVersion = 18;
// The current version of the binary log representation.
// Changes with each update of the binary log format.
internal const int FileFormatVersion = 24;
// The minimum version of the binary log reader that can read log of above version.
// This should be changed only when the binary log format is changed in a way that would prevent it from being
// read by older readers. (changing of the individual BuildEventArgs or adding new is fine - as reader can
// skip them if they are not known to it. Example of change requiring the increment would be the introduction of strings deduplication)
internal const int MinimumReaderVersion = 18;
private Stream stream;
private BinaryWriter binaryWriter;
private BuildEventArgsWriter eventArgsWriter;
private ProjectImportsCollector projectImportsCollector;
private string _initialTargetOutputLogging;
private bool _initialLogImports;
private string _initialIsBinaryLoggerEnabled;
/// <summary>
/// Describes whether to collect the project files (including imported project files) used during the build.
/// If the project files are collected they can be embedded in the log file or as a separate zip archive.
/// </summary>
public enum ProjectImportsCollectionMode
{
/// <summary>
/// Don't collect any files during the build.
/// </summary>
None,
/// <summary>
/// Embed all project files directly in the log file.
/// </summary>
Embed,
/// <summary>
/// Create an external .ProjectImports.zip archive for the project files.
/// </summary>
ZipFile,
}
/// <summary>
/// Gets or sets whether to capture and embed project and target source files used during the build.
/// </summary>
public ProjectImportsCollectionMode CollectProjectImports { get; set; } = ProjectImportsCollectionMode.Embed;
internal string FilePath { get; private set; }
/// <summary> Gets or sets the verbosity level.</summary>
/// <remarks>
/// The binary logger Verbosity is always maximum (Diagnostic). It tries to capture as much
/// information as possible.
/// </remarks>
public LoggerVerbosity Verbosity { get; set; } = LoggerVerbosity.Diagnostic;
/// <summary>
/// Gets or sets the parameters. The only supported parameter is the output log file path (for example, "msbuild.binlog").
/// </summary>
public string Parameters { get; set; }
/// <summary>
/// Optional expander of wildcard(s) within the LogFile path parameter of a binlog <see cref="Parameters"/>.
/// Wildcards can be used in the LogFile parameter in a form for curly brackets ('{}', '{[param]}').
/// Currently, the only supported wildcard is '{}', the optional parameters within the curly brackets
/// are not currently supported, however the string parameter to the <see cref="PathParameterExpander"/> func
/// is reserved for this purpose.
/// </summary>
internal Func<string, string> PathParameterExpander { private get; set; } = ExpandPathParameter;
/// <summary>
/// Initializes the logger by subscribing to events of the specified event source and embedded content source.
/// </summary>
public void Initialize(IEventSource eventSource)
{
_initialTargetOutputLogging = Environment.GetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING");
_initialLogImports = Traits.Instance.EscapeHatches.LogProjectImports;
_initialIsBinaryLoggerEnabled = Environment.GetEnvironmentVariable("MSBUILDBINARYLOGGERENABLED");
Environment.SetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING", "true");
Environment.SetEnvironmentVariable("MSBUILDLOGIMPORTS", "1");
Environment.SetEnvironmentVariable("MSBUILDBINARYLOGGERENABLED", bool.TrueString);
Traits.Instance.EscapeHatches.LogProjectImports = true;
bool logPropertiesAndItemsAfterEvaluation = Traits.Instance.EscapeHatches.LogPropertiesAndItemsAfterEvaluation ?? true;
ProcessParameters(out bool omitInitialInfo);
var replayEventSource = eventSource as IBinaryLogReplaySource;
try
{
string logDirectory = null;
try
{
logDirectory = Path.GetDirectoryName(FilePath);
}
catch (Exception)
{
// Directory creation is best-effort; if finding its path fails don't create the directory
// and possibly let the FileStream constructor below report the failure
}
if (logDirectory != null)
{
Directory.CreateDirectory(logDirectory);
}
stream = new FileStream(FilePath, FileMode.Create);
if (CollectProjectImports != ProjectImportsCollectionMode.None && replayEventSource == null)
{
projectImportsCollector = new ProjectImportsCollector(FilePath, CollectProjectImports == ProjectImportsCollectionMode.ZipFile);
projectImportsCollector.FileIOExceptionEvent += EventSource_AnyEventRaised;
}
if (eventSource is IEventSource3 eventSource3)
{
eventSource3.IncludeEvaluationMetaprojects();
}
if (logPropertiesAndItemsAfterEvaluation && eventSource is IEventSource4 eventSource4)
{
eventSource4.IncludeEvaluationPropertiesAndItems();
}
}
catch (Exception e)
{
string errorCode;
string helpKeyword;
string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out errorCode, out helpKeyword, "InvalidFileLoggerFile", FilePath, e.Message);
throw new LoggerException(message, e, errorCode, helpKeyword);
}
stream = new GZipStream(stream, CompressionLevel.Optimal);
// wrapping the GZipStream in a buffered stream significantly improves performance
// and the max throughput is reached with a 32K buffer. See details here:
// https://github.com/dotnet/runtime/issues/39233#issuecomment-745598847
stream = new BufferedStream(stream, bufferSize: 32768);
binaryWriter = new BinaryWriter(stream);
eventArgsWriter = new BuildEventArgsWriter(binaryWriter);
if (projectImportsCollector != null)
{
eventArgsWriter.EmbedFile += EventArgsWriter_EmbedFile;
}
if (replayEventSource != null)
{
if (CollectProjectImports == ProjectImportsCollectionMode.Embed)
{
replayEventSource.EmbeddedContentRead += args =>
eventArgsWriter.WriteBlob(args.ContentKind, args.ContentStream);
}
else if (CollectProjectImports == ProjectImportsCollectionMode.ZipFile)
{
replayEventSource.EmbeddedContentRead += args =>
ProjectImportsCollector.FlushBlobToFile(FilePath, args.ContentStream);
}
// If raw events are provided - let's try to use the advantage.
// But other subscribers can later on subscribe to structured events -
// for this reason we do only subscribe delayed.
replayEventSource.DeferredInitialize(
// For raw events we cannot write the initial info - as we cannot write
// at the same time as raw events are being written - this would break the deduplicated strings store.
// But we need to write the version info - but since we read/write raw - let's not change the version info.
() =>
{
binaryWriter.Write(replayEventSource.FileFormatVersion);
binaryWriter.Write(replayEventSource.MinimumReaderVersion);
replayEventSource.RawLogRecordReceived += RawEvents_LogDataSliceReceived;
// Replay separated strings here as well (and do not deduplicate! It would skew string indexes)
replayEventSource.StringReadDone += strArg => eventArgsWriter.WriteStringRecord(strArg.StringToBeUsed);
},
SubscribeToStructuredEvents);
}
else
{
SubscribeToStructuredEvents();
}
KnownTelemetry.LoggingConfigurationTelemetry.BinaryLogger = true;
void SubscribeToStructuredEvents()
{
// Write the version info - the latest version is written only for structured events replaying
// as raw events do not change structure - hence the version is the same as the one they were written with.
binaryWriter.Write(FileFormatVersion);
binaryWriter.Write(MinimumReaderVersion);
if (!omitInitialInfo)
{
LogInitialInfo();
}
eventSource.AnyEventRaised += EventSource_AnyEventRaised;
}
}
private void EventArgsWriter_EmbedFile(string filePath)
{
if (projectImportsCollector != null)
{
projectImportsCollector.AddFile(filePath);
}
}
private void LogInitialInfo()
{
LogMessage("BinLogFilePath=" + FilePath);
LogMessage("CurrentUICulture=" + System.Globalization.CultureInfo.CurrentUICulture.Name);
}
private void LogMessage(string text)
{
var args = new BuildMessageEventArgs(text, helpKeyword: null, senderName: "BinaryLogger", MessageImportance.Normal);
args.BuildEventContext = BuildEventContext.Invalid;
Write(args);
}
/// <summary>
/// Closes the underlying file stream.
/// </summary>
public void Shutdown()
{
Environment.SetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING", _initialTargetOutputLogging);
Environment.SetEnvironmentVariable("MSBUILDLOGIMPORTS", _initialLogImports ? "1" : null);
Environment.SetEnvironmentVariable("MSBUILDBINARYLOGGERENABLED", _initialIsBinaryLoggerEnabled);
Traits.Instance.EscapeHatches.LogProjectImports = _initialLogImports;
if (projectImportsCollector != null)
{
projectImportsCollector.Close();
if (CollectProjectImports == ProjectImportsCollectionMode.Embed)
{
projectImportsCollector.ProcessResult(
streamToEmbed => eventArgsWriter.WriteBlob(BinaryLogRecordKind.ProjectImportArchive, streamToEmbed),
LogMessage);
projectImportsCollector.DeleteArchive();
}
projectImportsCollector.FileIOExceptionEvent -= EventSource_AnyEventRaised;
projectImportsCollector = null;
}
if (stream != null)
{
// It's hard to determine whether we're at the end of decoding GZipStream
// so add an explicit 0 at the end to signify end of file
stream.WriteByte((byte)BinaryLogRecordKind.EndOfFile);
stream.Flush();
stream.Dispose();
stream = null;
}
}
private void RawEvents_LogDataSliceReceived(BinaryLogRecordKind recordKind, Stream stream)
{
eventArgsWriter.WriteBlob(recordKind, stream);
}
private void EventSource_AnyEventRaised(object sender, BuildEventArgs e)
{
Write(e);
}
private void Write(BuildEventArgs e)
{
if (stream != null)
{
if (projectImportsCollector != null)
{
CollectImports(e);
}
if (DoNotWriteToBinlog(e))
{
return;
}
// TODO: think about queuing to avoid contention
lock (eventArgsWriter)
{
eventArgsWriter.Write(e);
}
}
}
private static bool DoNotWriteToBinlog(BuildEventArgs e)
{
return e is GeneratedFileUsedEventArgs;
}
private void CollectImports(BuildEventArgs e)
{
if (e is ProjectImportedEventArgs importArgs && importArgs.ImportedProjectFile != null)
{
projectImportsCollector.AddFile(importArgs.ImportedProjectFile);
}
else if (e is ProjectStartedEventArgs projectArgs)
{
projectImportsCollector.AddFile(projectArgs.ProjectFile);
}
else if (e is MetaprojectGeneratedEventArgs { metaprojectXml: { } } metaprojectArgs)
{
projectImportsCollector.AddFileFromMemory(metaprojectArgs.ProjectFile, metaprojectArgs.metaprojectXml);
}
else if (e is ResponseFileUsedEventArgs responseFileArgs && responseFileArgs.ResponseFilePath != null)
{
projectImportsCollector.AddFile(responseFileArgs.ResponseFilePath);
}
else if (e is GeneratedFileUsedEventArgs generatedFileUsedEventArgs && generatedFileUsedEventArgs.FilePath != null)
{
string fullPath = Path.GetFullPath(generatedFileUsedEventArgs.FilePath);
projectImportsCollector.AddFileFromMemory(fullPath, generatedFileUsedEventArgs.Content);
}
}
/// <summary>
/// Processes the parameters given to the logger from MSBuild.
/// </summary>
/// <exception cref="LoggerException">
/// </exception>
private void ProcessParameters(out bool omitInitialInfo)
{
if (Parameters == null)
{
throw new LoggerException(ResourceUtilities.FormatResourceStringStripCodeAndKeyword("InvalidBinaryLoggerParameters", ""));
}
omitInitialInfo = false;
var parameters = Parameters.Split(MSBuildConstants.SemicolonChar, StringSplitOptions.RemoveEmptyEntries);
foreach (var parameter in parameters)
{
if (string.Equals(parameter, "ProjectImports=None", StringComparison.OrdinalIgnoreCase))
{
CollectProjectImports = ProjectImportsCollectionMode.None;
}
else if (string.Equals(parameter, "ProjectImports=Embed", StringComparison.OrdinalIgnoreCase))
{
CollectProjectImports = ProjectImportsCollectionMode.Embed;
}
else if (string.Equals(parameter, "ProjectImports=ZipFile", StringComparison.OrdinalIgnoreCase))
{
CollectProjectImports = ProjectImportsCollectionMode.ZipFile;
}
else if (string.Equals(parameter, "OmitInitialInfo", StringComparison.OrdinalIgnoreCase))
{
omitInitialInfo = true;
}
else if (TryInterpretPathParameter(parameter, out string filePath))
{
FilePath = filePath;
}
else
{
throw new LoggerException(ResourceUtilities.FormatResourceStringStripCodeAndKeyword("InvalidBinaryLoggerParameters", parameter));
}
}
if (FilePath == null)
{
FilePath = "msbuild.binlog";
}
KnownTelemetry.LoggingConfigurationTelemetry.BinaryLoggerUsedDefaultName = FilePath == "msbuild.binlog";
try
{
FilePath = Path.GetFullPath(FilePath);
}
catch (Exception e)
{
string errorCode;
string helpKeyword;
string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out errorCode, out helpKeyword, "InvalidFileLoggerFile", FilePath, e.Message);
throw new LoggerException(message, e, errorCode, helpKeyword);
}
}
private bool TryInterpretPathParameter(string parameter, out string filePath)
{
bool hasPathPrefix = parameter.StartsWith("LogFile=", StringComparison.OrdinalIgnoreCase);
if (hasPathPrefix)
{
parameter = parameter.Substring("LogFile=".Length);
}
parameter = parameter.Trim('"');
bool isWildcard = ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_12) && parameter.Contains("{}");
bool hasProperExtension = parameter.EndsWith(".binlog", StringComparison.OrdinalIgnoreCase);
filePath = parameter;
if (!isWildcard)
{
return hasProperExtension;
}
filePath = parameter.Replace("{}", GetUniqueStamp(), StringComparison.Ordinal);
if (!hasProperExtension)
{
filePath += ".binlog";
}
return true;
}
private string GetUniqueStamp()
=> (PathParameterExpander ?? ExpandPathParameter)(string.Empty);
private static string ExpandPathParameter(string parameters)
=> $"{DateTime.UtcNow.ToString("yyyyMMdd-HHmmss")}--{ProcessId}--{StringUtils.GenerateRandomString(6)}";
private static int ProcessId
#if NET
=> Environment.ProcessId;
#else
=> System.Diagnostics.Process.GetCurrentProcess().Id;
#endif
}
}