-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
LoggingServiceLogMethods.cs
897 lines (803 loc) · 45.9 KB
/
LoggingServiceLogMethods.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Framework.Profiler;
using Microsoft.Build.Shared;
using InvalidProjectFileException = Microsoft.Build.Exceptions.InvalidProjectFileException;
using TaskItem = Microsoft.Build.Execution.ProjectItemInstance.TaskItem;
namespace Microsoft.Build.BackEnd.Logging
{
/// <summary>
/// Partial class half of LoggingService which contains the Logging methods.
/// </summary>
internal partial class LoggingService : ILoggingService, INodePacketHandler, IBuildComponent
{
#region Log comments
/// <summary>
/// Logs a comment (BuildMessageEventArgs) with a certain MessageImportance level
/// </summary>
/// <param name="buildEventContext">Event context information which describes who is logging the event</param>
/// <param name="importance">How important is the message, this will determine which verbosities the message will show up on.
/// The higher the importance the lower the verbosity needs to be for the message to be seen</param>
/// <param name="messageResourceName">String which identifies the message in the string resx</param>
/// <param name="messageArgs">Arguments for the format string indexed by messageResourceName</param>
/// <exception cref="InternalErrorException">MessageResourceName is null</exception>
public void LogComment(BuildEventContext buildEventContext, MessageImportance importance, string messageResourceName, params object[] messageArgs)
{
lock (_lockObject)
{
if (!OnlyLogCriticalEvents)
{
ErrorUtilities.VerifyThrow(!string.IsNullOrEmpty(messageResourceName), "Need resource string for comment message.");
LogCommentFromText(buildEventContext, importance, ResourceUtilities.GetResourceString(messageResourceName), messageArgs);
}
}
}
/// <summary>
/// Log a comment
/// </summary>
/// <param name="buildEventContext">Event context information which describes who is logging the event</param>
/// <param name="importance">How important is the message, this will determine which verbosities the message will show up on.
/// The higher the importance the lower the verbosity needs to be for the message to be seen</param>
/// <param name="message">Message to log</param>
/// <exception cref="InternalErrorException">BuildEventContext is null</exception>
/// <exception cref="InternalErrorException">Message is null</exception>
public void LogCommentFromText(BuildEventContext buildEventContext, MessageImportance importance, string message)
{
lock (_lockObject)
{
this.LogCommentFromText(buildEventContext, importance, message, null);
}
}
/// <summary>
/// Log a comment
/// </summary>
/// <param name="buildEventContext">Event context information which describes who is logging the event</param>
/// <param name="importance">How important is the message, this will determine which verbosities the message will show up on.
/// The higher the importance the lower the verbosity needs to be for the message to be seen</param>
/// <param name="message">Message to log</param>
/// <param name="messageArgs">Message formatting arguments</param>
/// <exception cref="InternalErrorException">BuildEventContext is null</exception>
/// <exception cref="InternalErrorException">Message is null</exception>
public void LogCommentFromText(BuildEventContext buildEventContext, MessageImportance importance, string message, params object[] messageArgs)
{
lock (_lockObject)
{
if (!OnlyLogCriticalEvents)
{
ErrorUtilities.VerifyThrow(buildEventContext != null, "buildEventContext was null");
ErrorUtilities.VerifyThrow(message != null, "message was null");
BuildMessageEventArgs buildEvent = new BuildMessageEventArgs
(
message,
null,
"MSBuild",
importance,
DateTime.UtcNow,
messageArgs
);
buildEvent.BuildEventContext = buildEventContext;
ProcessLoggingEvent(buildEvent);
}
}
}
#endregion
#region Log errors
/**************************************************************************************************************************
* WARNING: Do not add overloads that allow raising events without specifying a file. In general ALL events should have a
* file associated with them. We've received a LOT of feedback from dogfooders about the lack of information in our
* events. If an event TRULY does not have an associated file, then String.Empty can be passed in for the file. However,
* that burden should lie on the caller -- these wrapper methods should NOT make it easy to skip the filename.
*************************************************************************************************************************/
/// <summary>
/// Logs an error with all registered loggers using the specified resource string.
/// </summary>
/// <param name="location">Event context information which describes who is logging the event</param>
/// <param name="file">File information where the error happened</param>
/// <param name="messageResourceName">String key to find the correct string resource</param>
/// <param name="messageArgs">Arguments for the string resource</param>
public void LogError(BuildEventContext location, BuildEventFileInfo file, string messageResourceName, params object[] messageArgs)
{
lock (_lockObject)
{
LogError(location, null, file, messageResourceName, messageArgs);
}
}
/// <summary>
/// Logs an error
/// </summary>
/// <param name="buildEventContext">Event context information which describes who is logging the event</param>
/// <param name="subcategoryResourceName">Can be null.</param>
/// <param name="file">File information about where the error happened</param>
/// <param name="messageResourceName">String index into the string.resx file</param>
/// <param name="messageArgs">Arguments for the format string in the resource file</param>
/// <exception cref="InternalErrorException">MessageResourceName is null</exception>
public void LogError(BuildEventContext buildEventContext, string subcategoryResourceName, BuildEventFileInfo file, string messageResourceName, params object[] messageArgs)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(!string.IsNullOrEmpty(messageResourceName), "Need resource string for error message.");
string errorCode;
string helpKeyword;
string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out errorCode, out helpKeyword, messageResourceName, messageArgs);
LogErrorFromText(buildEventContext, subcategoryResourceName, errorCode, helpKeyword, file, message);
}
}
/// <summary>
/// Logs an error with a given message
/// </summary>
/// <param name="buildEventContext">Event context information which describes who is logging the event</param>
/// <param name="subcategoryResourceName">Can be null.</param>
/// <param name="errorCode">Can be null.</param>
/// <param name="helpKeyword">Can be null.</param>
/// <param name="file">File information about where the error happened</param>
/// <param name="message">Error message which will be displayed</param>
/// <exception cref="InternalErrorException">File is null</exception>
/// <exception cref="InternalErrorException">Message is null</exception>
public void LogErrorFromText(BuildEventContext buildEventContext, string subcategoryResourceName, string errorCode, string helpKeyword, BuildEventFileInfo file, string message)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(buildEventContext != null, "Must specify the buildEventContext");
ErrorUtilities.VerifyThrow(file != null, "Must specify the associated file.");
ErrorUtilities.VerifyThrow(message != null, "Need error message.");
string subcategory = null;
if (subcategoryResourceName != null)
{
subcategory = AssemblyResources.GetString(subcategoryResourceName);
}
BuildErrorEventArgs buildEvent =
new BuildErrorEventArgs
(
subcategory,
errorCode,
file.File,
file.Line,
file.Column,
file.EndLine,
file.EndColumn,
message,
helpKeyword,
"MSBuild"
);
buildEvent.BuildEventContext = buildEventContext;
if (buildEvent.ProjectFile == null && buildEventContext.ProjectContextId != BuildEventContext.InvalidProjectContextId)
{
string projectFile;
_projectFileMap.TryGetValue(buildEventContext.ProjectContextId, out projectFile);
ErrorUtilities.VerifyThrow(projectFile != null, "ContextID {0} should have been in the ID-to-project file mapping but wasn't!", buildEventContext.ProjectContextId);
buildEvent.ProjectFile = projectFile;
}
ProcessLoggingEvent(buildEvent);
}
}
/// <summary>
/// Logs an error regarding an invalid project file . Since this method may be multiple times for the same InvalidProjectException
/// we do not want to log the error multiple times. Once the exception has been logged we set a flag on the exception to note that
/// it has already been logged.
/// </summary>
/// <param name="buildEventContext">Event context information which describes who is logging the event</param>
/// <param name="invalidProjectFileException">Exception which is causing the error</param>
/// <exception cref="InternalErrorException">InvalidProjectFileException is null</exception>
/// <exception cref="InternalErrorException">BuildEventContext is null</exception>
public void LogInvalidProjectFileError(BuildEventContext buildEventContext, InvalidProjectFileException invalidProjectFileException)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(invalidProjectFileException != null, "Need exception context.");
ErrorUtilities.VerifyThrow(buildEventContext != null, "buildEventContext is null");
// Don't log the exception more than once.
if (!invalidProjectFileException.HasBeenLogged)
{
BuildErrorEventArgs buildEvent =
new BuildErrorEventArgs
(
invalidProjectFileException.ErrorSubcategory,
invalidProjectFileException.ErrorCode,
invalidProjectFileException.ProjectFile,
invalidProjectFileException.LineNumber,
invalidProjectFileException.ColumnNumber,
invalidProjectFileException.EndLineNumber,
invalidProjectFileException.EndColumnNumber,
invalidProjectFileException.BaseMessage,
invalidProjectFileException.HelpKeyword,
"MSBuild"
);
buildEvent.BuildEventContext = buildEventContext;
if (buildEvent.ProjectFile == null && buildEventContext.ProjectContextId != BuildEventContext.InvalidProjectContextId)
{
string projectFile;
_projectFileMap.TryGetValue(buildEventContext.ProjectContextId, out projectFile);
ErrorUtilities.VerifyThrow(projectFile != null, "ContextID {0} should have been in the ID-to-project file mapping but wasn't!", buildEventContext.ProjectContextId);
buildEvent.ProjectFile = projectFile;
}
ProcessLoggingEvent(buildEvent);
invalidProjectFileException.HasBeenLogged = true;
}
}
}
/// <summary>
/// Logs an error regarding an unexpected build failure
/// This will include a stack dump.
/// </summary>
/// <param name="buildEventContext">BuildEventContext of the error</param>
/// <param name="exception">Exception wihch caused the build error</param>
/// <param name="file">Provides file information about where the build error happened</param>
public void LogFatalBuildError(BuildEventContext buildEventContext, Exception exception, BuildEventFileInfo file)
{
lock (_lockObject)
{
LogFatalError(buildEventContext, exception, file, "FatalBuildError");
}
}
/// <summary>
/// Logs an error regarding an unexpected task failure.
/// This will include a stack dump.
/// </summary>
/// <param name="buildEventContext">BuildEventContext of the error</param>
/// <param name="exception">Exceptionm which caused the error</param>
/// <param name="file">File information which indicates which file the error is happening in</param>
/// <param name="taskName">Task which the error is happening in</param>
/// <exception cref="InternalErrorException">TaskName is null</exception>
public void LogFatalTaskError(BuildEventContext buildEventContext, Exception exception, BuildEventFileInfo file, string taskName)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(taskName != null, "Must specify the name of the task that failed.");
LogFatalError(buildEventContext, exception, file, "FatalTaskError", taskName);
}
}
/// <summary>
/// Logs an error regarding an unexpected failure using the specified resource string.
/// This will include a stack dump.
/// </summary>
/// <param name="buildEventContext">BuildEventContext of the error</param>
/// <param name="exception">Exception which will be used to generate the error message</param>
/// <param name="file">File information which describes where the error happened</param>
/// <param name="messageResourceName">String name for the resource string to be used</param>
/// <param name="messageArgs">Arguments for messageResourceName</param>
/// <exception cref="InternalErrorException">MessageResourceName is null</exception>
public void LogFatalError(BuildEventContext buildEventContext, Exception exception, BuildEventFileInfo file, string messageResourceName, params object[] messageArgs)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(!string.IsNullOrEmpty(messageResourceName), "Need resource string for error message.");
string errorCode;
string helpKeyword;
string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out errorCode, out helpKeyword, messageResourceName, messageArgs);
#if DEBUG
message += Environment.NewLine + "This is an unhandled exception from a task -- PLEASE OPEN A BUG AGAINST THE TASK OWNER.";
#endif
if (exception != null)
{
message += Environment.NewLine + exception.ToString();
}
LogErrorFromText(buildEventContext, null, errorCode, helpKeyword, file, message);
}
}
#endregion
#region Log warnings
/**************************************************************************************************************************
* WARNING: Do not add overloads that allow raising events without specifying a file. In general ALL events should have a
* file associated with them. We've received a LOT of feedback from dogfooders about the lack of information in our
* events. If an event TRULY does not have an associated file, then String.Empty can be passed in for the file. However,
* that burden should lie on the caller -- these wrapper methods should NOT make it easy to skip the filename.
*************************************************************************************************************************/
/// <summary>
/// Logs an warning regarding an unexpected task failure
/// This will include a stack dump.
/// </summary>
/// <param name="buildEventContext">Event context information which describes who is logging the event</param>
/// <param name="exception">The exception to be used to create the warning text</param>
/// <param name="file">The file information which indicates where the warning happened</param>
/// <param name="taskName">Name of the task which the warning is being raised from</param>
public void LogTaskWarningFromException(BuildEventContext buildEventContext, Exception exception, BuildEventFileInfo file, string taskName)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(!String.IsNullOrEmpty(taskName), "Must specify the name of the task that failed.");
string warningCode;
string helpKeyword;
string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out warningCode, out helpKeyword, "FatalTaskError", taskName);
#if DEBUG
message += Environment.NewLine + "This is an unhandled exception from a task -- PLEASE OPEN A BUG AGAINST THE TASK OWNER.";
#endif
if (exception != null)
{
message += Environment.NewLine + exception.ToString();
}
LogWarningFromText(buildEventContext, null, warningCode, helpKeyword, file, message);
}
}
/// <summary>
/// Logs a warning using the specified resource string.
/// </summary>
/// <param name="buildEventContext">Event context information which describes who is logging the event</param>
/// <param name="subcategoryResourceName">Can be null.</param>
/// <param name="file">File information which describes where the warning happened</param>
/// <param name="messageResourceName">String name for the resource string to be used</param>
/// <param name="messageArgs">Arguments for messageResourceName</param>
public void LogWarning(BuildEventContext buildEventContext, string subcategoryResourceName, BuildEventFileInfo file, string messageResourceName, params object[] messageArgs)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(!string.IsNullOrEmpty(messageResourceName), "Need resource string for warning message.");
string warningCode;
string helpKeyword;
string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out warningCode, out helpKeyword, messageResourceName, messageArgs);
LogWarningFromText(buildEventContext, subcategoryResourceName, warningCode, helpKeyword, file, message);
}
}
/// <summary>
/// Logs a warning
/// </summary>
/// <param name="buildEventContext">Event context information which describes who is logging the event</param>
/// <param name="subcategoryResourceName">Subcategory resource Name. Can be null.</param>
/// <param name="warningCode">The warning code of the message. Can be null.</param>
/// <param name="helpKeyword">Help keyword for the message. Can be null.</param>
/// <param name="file">The file information which will describe where the warning happened</param>
/// <param name="message">Warning message to log</param>
public void LogWarningFromText(BuildEventContext buildEventContext, string subcategoryResourceName, string warningCode, string helpKeyword, BuildEventFileInfo file, string message)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(file != null, "Must specify the associated file.");
ErrorUtilities.VerifyThrow(message != null, "Need warning message.");
ErrorUtilities.VerifyThrow(buildEventContext != null, "Need a BuildEventContext");
string subcategory = null;
if (!string.IsNullOrWhiteSpace(subcategoryResourceName))
{
subcategory = AssemblyResources.GetString(subcategoryResourceName);
}
BuildWarningEventArgs buildEvent = new BuildWarningEventArgs
(
subcategory,
warningCode,
file.File,
file.Line,
file.Column,
file.EndLine,
file.EndColumn,
message,
helpKeyword,
"MSBuild"
);
buildEvent.BuildEventContext = buildEventContext;
if (buildEvent.ProjectFile == null && buildEventContext.ProjectContextId != BuildEventContext.InvalidProjectContextId)
{
string projectFile;
_projectFileMap.TryGetValue(buildEventContext.ProjectContextId, out projectFile);
ErrorUtilities.VerifyThrow(projectFile != null, "ContextID {0} should have been in the ID-to-project file mapping but wasn't!", buildEventContext.ProjectContextId);
buildEvent.ProjectFile = projectFile;
}
ProcessLoggingEvent(buildEvent);
}
}
#endregion
#region Log status
/// <summary>
/// Logs that the build has started
/// </summary>
public void LogBuildStarted()
{
lock (_lockObject)
{
// If we're only logging critical events, don't risk causing all the resources to load by formatting
// a string that won't get emitted anyway.
string message = String.Empty;
if (!OnlyLogCriticalEvents)
{
message = ResourceUtilities.GetResourceString("BuildStarted");
}
IDictionary<string, string> environmentProperties = null;
if (_componentHost?.BuildParameters != null)
{
environmentProperties = _componentHost.BuildParameters.BuildProcessEnvironment;
}
BuildStartedEventArgs buildEvent = new BuildStartedEventArgs(message, null /* no help keyword */, environmentProperties);
// Raise the event with the filters
ProcessLoggingEvent(buildEvent);
// Make sure we process this event before going any further
if (_logMode == LoggerMode.Asynchronous)
{
WaitForThreadToProcessEvents();
}
}
}
/// <summary>
/// Logs that the build has finished
/// </summary>
/// <param name="success">Did the build pass or fail</param>
public void LogBuildFinished(bool success)
{
lock (_lockObject)
{
// If we're only logging critical events, don't risk causing all the resources to load by formatting
// a string that won't get emitted anyway.
string message = String.Empty;
if (!OnlyLogCriticalEvents)
{
message = ResourceUtilities.GetResourceString(success ? "BuildFinishedSuccess" : "BuildFinishedFailure");
}
BuildFinishedEventArgs buildEvent = new BuildFinishedEventArgs(message, null /* no help keyword */, success);
ProcessLoggingEvent(buildEvent);
if (_logMode == LoggerMode.Asynchronous)
{
WaitForThreadToProcessEvents();
}
}
}
/// <inheritdoc />
public BuildEventContext CreateEvaluationBuildEventContext(int nodeId, int submissionId)
{
return new BuildEventContext(submissionId, nodeId, NextEvaluationId, BuildEventContext.InvalidProjectInstanceId, BuildEventContext.InvalidProjectContextId, BuildEventContext.InvalidTargetId, BuildEventContext.InvalidTaskId);
}
/// <inheritdoc />
public void LogProjectEvaluationStarted(BuildEventContext projectEvaluationEventContext, string projectFile)
{
lock (_lockObject)
{
ProjectEvaluationStartedEventArgs evaluationEvent =
new ProjectEvaluationStartedEventArgs(ResourceUtilities.GetResourceString("EvaluationStarted"),
projectFile)
{
BuildEventContext = projectEvaluationEventContext,
ProjectFile = projectFile
};
ProcessLoggingEvent(evaluationEvent);
}
}
/// <summary>
/// Logs that a project evaluation has finished
/// </summary>
/// <param name="projectEvaluationEventContext">Event context for the project.</param>
/// <param name="projectFile">Project file being built</param>
/// <param name="globalProperties">Global properties used for the evaluation.</param>
/// <param name="properties">Properties produced by the evaluation.</param>
/// <param name="items">Items produced by the evaluation.</param>
/// <param name="profilerResult">Profiler results if evaluation profiling was enabled.</param>
/// <exception cref="InternalErrorException">BuildEventContext is null</exception>
public void LogProjectEvaluationFinished(
BuildEventContext projectEvaluationEventContext,
string projectFile,
IEnumerable globalProperties,
IEnumerable properties,
IEnumerable items,
ProfilerResult? profilerResult)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(projectEvaluationEventContext != null, "projectBuildEventContext");
ProjectEvaluationFinishedEventArgs buildEvent =
new ProjectEvaluationFinishedEventArgs(ResourceUtilities.GetResourceString("EvaluationFinished"), projectFile)
{
BuildEventContext = projectEvaluationEventContext,
ProjectFile = projectFile,
ProfilerResult = profilerResult,
GlobalProperties = globalProperties,
Properties = properties,
Items = items
};
ProcessLoggingEvent(buildEvent);
}
}
/// <summary>
/// Logs that a project build has started
/// </summary>
/// <param name="nodeBuildEventContext">The event context of the node which is spawning this project.</param>
/// <param name="submissionId">The id of the submission.</param>
/// <param name="projectInstanceId">Id of the project instance which is being started</param>
/// <param name="parentBuildEventContext">BuildEventContext of the project who is requesting "projectFile" to build</param>
/// <param name="projectFile">Project file to build</param>
/// <param name="targetNames">Target names to build</param>
/// <param name="properties">Initial property list</param>
/// <param name="items">Initial items list</param>
/// <param name="evaluationId">EvaluationId of the project instance</param>
/// <returns>The build event context for the project.</returns>
/// <exception cref="InternalErrorException">parentBuildEventContext is null</exception>
/// <exception cref="InternalErrorException">projectBuildEventContext is null</exception>
public BuildEventContext LogProjectStarted(BuildEventContext nodeBuildEventContext, int submissionId, int projectInstanceId, BuildEventContext parentBuildEventContext, string projectFile, string targetNames, IEnumerable<DictionaryEntry> properties, IEnumerable<DictionaryEntry> items, int evaluationId = BuildEventContext.InvalidEvaluationId)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(nodeBuildEventContext != null, "Need a nodeBuildEventContext");
BuildEventContext projectBuildEventContext = new BuildEventContext(submissionId, nodeBuildEventContext.NodeId, evaluationId, projectInstanceId, NextProjectId, BuildEventContext.InvalidTargetId, BuildEventContext.InvalidTaskId);
// PERF: Not using VerifyThrow to avoid boxing of projectBuildEventContext.ProjectContextId in the non-error case.
if (_projectFileMap.ContainsKey(projectBuildEventContext.ProjectContextId))
{
ErrorUtilities.ThrowInternalError("ContextID {0} for project {1} should not already be in the ID-to-file mapping!", projectBuildEventContext.ProjectContextId, projectFile);
}
_projectFileMap[projectBuildEventContext.ProjectContextId] = projectFile;
ErrorUtilities.VerifyThrow(parentBuildEventContext != null, "Need a parentBuildEventContext");
string message = string.Empty;
string projectFilePath = Path.GetFileName(projectFile);
// Check to see if the there are any specific target names to be built.
// If targetNames is null or empty then we will be building with the
// default targets.
if (!String.IsNullOrEmpty(targetNames))
{
message = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("ProjectStartedPrefixForTopLevelProjectWithTargetNames", projectFilePath, targetNames);
}
else
{
message = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("ProjectStartedPrefixForTopLevelProjectWithDefaultTargets", projectFilePath);
}
ErrorUtilities.VerifyThrow(_configCache.Value.HasConfiguration(projectInstanceId), "Cannot find the project configuration while injecting non-serialized data from out-of-proc node.");
var buildRequestConfiguration = _configCache.Value[projectInstanceId];
// Always log GlobalProperties on ProjectStarted
// See https://github.com/dotnet/msbuild/issues/6341 for details
IDictionary<string, string> globalProperties = buildRequestConfiguration.GlobalProperties.ToDictionary();
var buildEvent = new ProjectStartedEventArgs
(
projectInstanceId,
message,
helpKeyword: null,
projectFile,
targetNames,
properties,
items,
parentBuildEventContext,
globalProperties,
buildRequestConfiguration.ToolsVersion
);
buildEvent.BuildEventContext = projectBuildEventContext;
ProcessLoggingEvent(buildEvent);
return projectBuildEventContext;
}
}
/// <summary>
/// Logs that a project has finished
/// </summary>
/// <param name="projectBuildEventContext">Event context for the project.</param>
/// <param name="projectFile">Project file being built</param>
/// <param name="success">Did the project pass or fail</param>
/// <exception cref="InternalErrorException">BuildEventContext is null</exception>
public void LogProjectFinished(BuildEventContext projectBuildEventContext, string projectFile, bool success)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(projectBuildEventContext != null, "projectBuildEventContext");
string message = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword(success ? "ProjectFinishedSuccess" : "ProjectFinishedFailure", Path.GetFileName(projectFile));
ProjectFinishedEventArgs buildEvent = new ProjectFinishedEventArgs
(
message,
null, // no help keyword
projectFile,
success
);
buildEvent.BuildEventContext = projectBuildEventContext;
ProcessLoggingEvent(buildEvent);
// PERF: Not using VerifyThrow to avoid boxing of projectBuildEventContext.ProjectContextId in the non-error case.
if (!_projectFileMap.Remove(projectBuildEventContext.ProjectContextId))
{
ErrorUtilities.ThrowInternalError("ContextID {0} for project {1} should be in the ID-to-file mapping!", projectBuildEventContext.ProjectContextId, projectFile);
}
}
}
/// <summary>
/// Logs that a target started
/// </summary>
/// <param name="projectBuildEventContext">Event context for the project spawning this target</param>
/// <param name="targetName">Name of target</param>
/// <param name="projectFile">Project file being built</param>
/// <param name="projectFileOfTargetElement">Project file which contains the target</param>
/// <param name="parentTargetName">The name of the parent target.</param>
/// <param name="buildReason">The reason the parent target built the target.</param>
/// <returns>The build event context for the target.</returns>
/// <exception cref="InternalErrorException">BuildEventContext is null</exception>
public BuildEventContext LogTargetStarted(BuildEventContext projectBuildEventContext, string targetName, string projectFile, string projectFileOfTargetElement, string parentTargetName, TargetBuiltReason buildReason)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(projectBuildEventContext != null, "projectBuildEventContext is null");
BuildEventContext targetBuildEventContext = new BuildEventContext
(
projectBuildEventContext.SubmissionId,
projectBuildEventContext.NodeId,
projectBuildEventContext.ProjectInstanceId,
projectBuildEventContext.ProjectContextId,
NextTargetId,
BuildEventContext.InvalidTaskId
);
string message = String.Empty;
if (!OnlyLogCriticalEvents)
{
if (String.Equals(projectFile, projectFileOfTargetElement, StringComparison.OrdinalIgnoreCase))
{
if (!String.IsNullOrEmpty(parentTargetName))
{
message = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("TargetStartedProjectDepends", targetName, projectFile, parentTargetName);
}
else
{
message = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("TargetStartedProjectEntry", targetName, projectFile);
}
}
else
{
if (!String.IsNullOrEmpty(parentTargetName))
{
message = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("TargetStartedFileProjectDepends", targetName, projectFileOfTargetElement, projectFile, parentTargetName);
}
else
{
message = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("TargetStartedFileProjectEntry", targetName, projectFileOfTargetElement, projectFile);
}
}
TargetStartedEventArgs buildEvent = new TargetStartedEventArgs
(
message,
null, // no help keyword
targetName,
projectFile,
projectFileOfTargetElement,
parentTargetName,
buildReason,
DateTime.UtcNow
);
buildEvent.BuildEventContext = targetBuildEventContext;
ProcessLoggingEvent(buildEvent);
}
return targetBuildEventContext;
}
}
/// <summary>
/// Logs that a target has finished.
/// </summary>
/// <param name="targetBuildEventContext">Event context for the target</param>
/// <param name="targetName">Target which has just finished</param>
/// <param name="projectFile">Project file being built</param>
/// <param name="projectFileOfTargetElement">Project file which contains the target</param>
/// <param name="success">Did the target pass or fail</param>
/// <param name="targetOutputs">Target outputs for the target.</param>
/// <exception cref="InternalErrorException">BuildEventContext is null</exception>
public void LogTargetFinished(BuildEventContext targetBuildEventContext, string targetName, string projectFile, string projectFileOfTargetElement, bool success, IEnumerable<TaskItem> targetOutputs)
{
lock (_lockObject)
{
if (!OnlyLogCriticalEvents)
{
ErrorUtilities.VerifyThrow(targetBuildEventContext != null, "targetBuildEventContext is null");
string message = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword(success ? "TargetFinishedSuccess" : "TargetFinishedFailure", targetName, Path.GetFileName(projectFile));
TargetFinishedEventArgs buildEvent = new TargetFinishedEventArgs
(
message,
null, // no help keyword
targetName,
projectFile,
projectFileOfTargetElement,
success,
targetOutputs
);
buildEvent.BuildEventContext = targetBuildEventContext;
ProcessLoggingEvent(buildEvent);
}
}
}
/// <summary>
/// Logs that task execution has started.
/// </summary>
/// <param name="taskBuildEventContext">Event context for the task</param>
/// <param name="taskName">Task Name</param>
/// <param name="projectFile">Project file being built</param>
/// <param name="projectFileOfTaskNode">Project file which contains the task</param>
/// <exception cref="InternalErrorException">BuildEventContext is null</exception>
public void LogTaskStarted(BuildEventContext taskBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(taskBuildEventContext != null, "targetBuildEventContext is null");
if (!OnlyLogCriticalEvents)
{
TaskStartedEventArgs buildEvent = new TaskStartedEventArgs
(
ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("TaskStarted", taskName),
null, // no help keyword
projectFile,
projectFileOfTaskNode,
taskName
);
buildEvent.BuildEventContext = taskBuildEventContext;
ProcessLoggingEvent(buildEvent);
}
}
}
/// <summary>
/// Logs that task execution has started.
/// </summary>
/// <param name="targetBuildEventContext">Event context for the target spawning this task.</param>
/// <param name="taskName">Task Name</param>
/// <param name="projectFile">Project file being built</param>
/// <param name="projectFileOfTaskNode">Project file which contains the task</param>
/// <returns>The build event context for the task.</returns>
/// <exception cref="InternalErrorException">BuildEventContext is null</exception>
public BuildEventContext LogTaskStarted2(BuildEventContext targetBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(targetBuildEventContext != null, "targetBuildEventContext is null");
BuildEventContext taskBuildEventContext = new BuildEventContext
(
targetBuildEventContext.SubmissionId,
targetBuildEventContext.NodeId,
targetBuildEventContext.ProjectInstanceId,
targetBuildEventContext.ProjectContextId,
targetBuildEventContext.TargetId,
NextTaskId
);
if (!OnlyLogCriticalEvents)
{
TaskStartedEventArgs buildEvent = new TaskStartedEventArgs
(
ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("TaskStarted", taskName),
null, // no help keyword
projectFile,
projectFileOfTaskNode,
taskName
);
buildEvent.BuildEventContext = taskBuildEventContext;
ProcessLoggingEvent(buildEvent);
}
return taskBuildEventContext;
}
}
/// <summary>
/// Logs that a task has finished executing.
/// </summary>
/// <param name="taskBuildEventContext">Event context for the task</param>
/// <param name="taskName">Name of the task</param>
/// <param name="projectFile">Project which is being processed</param>
/// <param name="projectFileOfTaskNode">Project file which contains the task</param>
/// <param name="success">Did the task pass or fail</param>
/// <exception cref="InternalErrorException">BuildEventContext is null</exception>
public void LogTaskFinished(BuildEventContext taskBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, bool success)
{
lock (_lockObject)
{
if (!OnlyLogCriticalEvents)
{
ErrorUtilities.VerifyThrow(taskBuildEventContext != null, "taskBuildEventContext is null");
string message = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword(success ? "TaskFinishedSuccess" : "TaskFinishedFailure", taskName);
TaskFinishedEventArgs buildEvent = new TaskFinishedEventArgs
(
message,
null, // no help keyword
projectFile,
projectFileOfTaskNode,
taskName,
success
);
buildEvent.BuildEventContext = taskBuildEventContext;
ProcessLoggingEvent(buildEvent);
}
}
}
#endregion
#region Log telemetry
/// <summary>
/// Logs a telemetry event.
/// </summary>
/// <param name="buildEventContext">Event context information which describes who is logging the event</param>
/// <param name="eventName">The event name.</param>
/// <param name="properties">The list of properties assocated with the event.</param>
public void LogTelemetry(BuildEventContext buildEventContext, string eventName, IDictionary<string, string> properties)
{
lock (_lockObject)
{
ErrorUtilities.VerifyThrow(eventName != null, "eventName is null");
TelemetryEventArgs telemetryEvent = new TelemetryEventArgs
{
BuildEventContext = buildEventContext,
EventName = eventName,
Properties = properties == null ? new Dictionary<string, string>() : new Dictionary<string, string>(properties)
};
ProcessLoggingEvent(telemetryEvent);
}
}
#endregion
}
}