/
MockLoggingService.cs
600 lines (534 loc) · 21.5 KB
/
MockLoggingService.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
// 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 Microsoft.Build.Framework;
using Microsoft.Build.Framework.Profiler;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Logging;
using Microsoft.Build.Shared;
using InvalidProjectFileException = Microsoft.Build.Exceptions.InvalidProjectFileException;
using TaskItem = Microsoft.Build.Execution.ProjectItemInstance.TaskItem;
namespace Microsoft.Build.UnitTests.BackEnd
{
/// <summary>
/// A class providing a mock implementation of ILoggingService.
/// </summary>
internal class MockLoggingService : ILoggingService
{
private Action<string> _writer;
public MockLoggingService(Action<string> writter = null)
{
_writer = writter ?? Console.WriteLine;
}
#region ILoggingService Members
/// <summary>
/// The event to raise when there is a logging exception
/// </summary>
public event LoggingExceptionDelegate OnLoggingThreadException;
/// <summary>
/// The event to raise when ProjectStarted is processed.
/// </summary>
public event ProjectStartedEventHandler OnProjectStarted;
/// <summary>
/// The event to raise when ProjectFinished is processed
/// </summary>
public event ProjectFinishedEventHandler OnProjectFinished;
/// <summary>
/// Enumerator over all registered loggers.
/// </summary>
public ICollection<ILogger> Loggers
{
get { throw new NotImplementedException(); }
}
/// <summary>
/// The logging service state
/// </summary>
public LoggingServiceState ServiceState
{
get
{
OnLoggingThreadException(null);
OnProjectStarted(null, null);
OnProjectFinished(null, null);
throw new NotImplementedException();
}
}
/// <summary>
/// The logging mode.
/// </summary>
public LoggerMode LoggingMode
{
get
{
return LoggerMode.Synchronous;
}
}
/// <summary>
/// Whether to log critical events
/// </summary>
public bool OnlyLogCriticalEvents
{
get
{
return false;
}
set
{
}
}
/// <summary>
/// Returns the number of initial nodes.
/// </summary>
public int MaxCPUCount
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
/// <summary>
/// Gets the logger descriptions
/// </summary>
public ICollection<LoggerDescription> LoggerDescriptions
{
get
{
throw new NotImplementedException();
}
}
/// <summary>
/// Gets the registered logger type names.
/// </summary>
public ICollection<string> RegisteredLoggerTypeNames
{
get
{
throw new NotImplementedException();
}
}
/// <summary>
/// Retrieves the registered sink names.
/// </summary>
public ICollection<string> RegisteredSinkNames
{
get
{
throw new NotImplementedException();
}
}
/// <summary>
/// Properties to serialize from the child node to the parent node
/// </summary>
public string[] PropertiesToSerialize
{
get;
set;
}
/// <summary>
/// List of warnings to treat as errors.
/// </summary>
public ISet<string> WarningsAsErrors
{
get;
set;
}
/// <summary>
/// List of warnings to treat as low importance messages.
/// </summary>
public ISet<string> WarningsAsMessages
{
get;
set;
}
/// <summary>
/// Is the logging service on a remote node, this is used to determine if properties need to be serialized
/// </summary>
public bool RunningOnRemoteNode
{
get;
set;
}
/// <summary>
/// Should all properties be serialized from the child to the parent process
/// </summary>
public bool SerializeAllProperties
{
get;
set;
}
/// <summary>
/// Should evaluation events include generated metaprojects?
/// </summary>
public bool IncludeEvaluationMetaprojects
{
get => false;
set { }
}
/// <summary>
/// Should evaluation events include profiling information?
/// </summary>
public bool IncludeEvaluationProfile
{
get => false;
set { }
}
/// <summary>
/// Log properties and items on ProjectEvaluationFinishedEventArgs
/// instead of ProjectStartedEventArgs.
/// </summary>
public bool IncludeEvaluationPropertiesAndItems
{
get => false;
set { }
}
/// <summary>
/// Should task events include task inputs?
/// </summary>
public bool IncludeTaskInputs
{
get => false;
set { }
}
public void AddWarningsAsMessages(BuildEventContext buildEventContext, ISet<string> codes)
{
throw new NotImplementedException();
}
public void AddWarningsAsErrors(BuildEventContext buildEventContext, ISet<string> codes)
{
throw new NotImplementedException();
}
/// <summary>
/// Registers a distributed logger.
/// </summary>
/// <param name="centralLogger">The central logger, which resides on the build manager.</param>
/// <param name="forwardingLogger">The forwarding logger, which resides on the node.</param>
/// <returns>True if successful.</returns>
public bool RegisterDistributedLogger(ILogger centralLogger, LoggerDescription forwardingLogger)
{
throw new NotImplementedException();
}
/// <summary>
/// Registers a logger
/// </summary>
/// <param name="logger">The logger</param>
/// <returns>True if successful.</returns>
public bool RegisterLogger(ILogger logger)
{
throw new NotImplementedException();
}
/// <summary>
/// Clear out all registered loggers so that none are registered.
/// </summary>
public void UnregisterAllLoggers()
{
throw new NotImplementedException();
}
/// <summary>
/// Initializes the loggers on a node
/// </summary>
/// <param name="loggerDescriptions">The descriptions received from the Build Manager</param>
/// <param name="forwardingLoggerSink">The sink used to transmit messages to the manager.</param>
/// <param name="nodeId">The id of the node.</param>
public void InitializeNodeLoggers(ICollection<LoggerDescription> loggerDescriptions, IBuildEventSink forwardingLoggerSink, int nodeId)
{
throw new NotImplementedException();
}
/// <summary>
/// Logs a comment based on a message resource
/// </summary>
/// <param name="buildEventContext">The context</param>
/// <param name="importance">The importance</param>
/// <param name="messageResourceName">The resource for the message</param>
/// <param name="messageArgs">The args for the message</param>
public void LogComment(BuildEventContext buildEventContext, MessageImportance importance, string messageResourceName, params object[] messageArgs)
{
_writer(messageResourceName);
foreach (object o in messageArgs)
{
_writer((string)o);
}
}
/// <summary>
/// Logs a text comment
/// </summary>
/// <param name="buildEventContext">The context</param>
/// <param name="importance">The importance</param>
/// <param name="message">The message</param>
public void LogCommentFromText(BuildEventContext buildEventContext, MessageImportance importance, string message)
{
_writer(message);
}
/// <inheritdoc />
public void LogCommentFromText(BuildEventContext buildEventContext, MessageImportance importance, string message, params object[] messageArgs)
{
if (messageArgs?.Length > 0)
{
message = string.Format(message, messageArgs);
}
_writer(message);
}
/// <summary>
/// Logs a pre-formed build event
/// </summary>
/// <param name="buildEvent">The event to log</param>
public void LogBuildEvent(BuildEventArgs buildEvent)
{
}
/// <summary>
/// Logs an error
/// </summary>
/// <param name="buildEventContext">The event context</param>
/// <param name="file">The file from which the error is logged</param>
/// <param name="messageResourceName">The message resource</param>
/// <param name="messageArgs">The message args</param>
public void LogError(BuildEventContext buildEventContext, BuildEventFileInfo file, string messageResourceName, params object[] messageArgs)
{
_writer(messageResourceName);
foreach (object o in messageArgs)
{
_writer((string)o);
}
}
/// <summary>
/// Logs an error with a subcategory
/// </summary>
/// <param name="buildEventContext">The build event context</param>
/// <param name="subcategoryResourceName">The subcategory resource</param>
/// <param name="file">The file</param>
/// <param name="messageResourceName">The message resource</param>
/// <param name="messageArgs">The message args</param>
public void LogError(BuildEventContext buildEventContext, string subcategoryResourceName, BuildEventFileInfo file, string messageResourceName, params object[] messageArgs)
{
_writer(messageResourceName);
foreach (object o in messageArgs)
{
_writer((string)o);
}
}
/// <summary>
/// Logs a text error
/// </summary>
/// <param name="buildEventContext">The event context</param>
/// <param name="subcategoryResourceName">The subcategory resource</param>
/// <param name="errorCode">The error code</param>
/// <param name="helpKeyword">A help keyword</param>
/// <param name="file">The file</param>
/// <param name="message">The message</param>
public void LogErrorFromText(BuildEventContext buildEventContext, string subcategoryResourceName, string errorCode, string helpKeyword, BuildEventFileInfo file, string message)
{
_writer(message);
}
/// <summary>
/// Logs an invalid project file error
/// </summary>
/// <param name="buildEventContext">The event context</param>
/// <param name="invalidProjectFileException">The exception</param>
public void LogInvalidProjectFileError(BuildEventContext buildEventContext, InvalidProjectFileException invalidProjectFileException)
{
}
/// <summary>
/// Logs a fatal build error
/// </summary>
/// <param name="buildEventContext">The event context</param>
/// <param name="exception">The exception</param>
/// <param name="file">The file</param>
public void LogFatalBuildError(BuildEventContext buildEventContext, Exception exception, BuildEventFileInfo file)
{
_writer(exception.Message);
}
/// <summary>
/// Logs a fatal task error
/// </summary>
/// <param name="buildEventContext">The event context</param>
/// <param name="exception">The exception</param>
/// <param name="file">The file</param>
/// <param name="taskName">The name of the task</param>
public void LogFatalTaskError(BuildEventContext buildEventContext, Exception exception, BuildEventFileInfo file, string taskName)
{
}
/// <summary>
/// Logs a generic fatal error
/// </summary>
/// <param name="buildEventContext">The build context</param>
/// <param name="exception">The exception</param>
/// <param name="file">The file</param>
/// <param name="messageResourceName">The message resource</param>
/// <param name="messageArgs">The message args</param>
public void LogFatalError(BuildEventContext buildEventContext, Exception exception, BuildEventFileInfo file, string messageResourceName, params object[] messageArgs)
{
}
/// <summary>
/// Logs a task warning
/// </summary>
/// <param name="buildEventContext">The build context</param>
/// <param name="exception">The exception</param>
/// <param name="file">The file</param>
/// <param name="taskName">The name of the task</param>
public void LogTaskWarningFromException(BuildEventContext buildEventContext, Exception exception, BuildEventFileInfo file, string taskName)
{
}
/// <summary>
/// Logs a warning
/// </summary>
/// <param name="buildEventContext">The event context</param>
/// <param name="subcategoryResourceName">The subcategory resource</param>
/// <param name="file">The file</param>
/// <param name="messageResourceName">The message resource</param>
/// <param name="messageArgs">The message args</param>
public void LogWarning(BuildEventContext buildEventContext, string subcategoryResourceName, BuildEventFileInfo file, string messageResourceName, params object[] messageArgs)
{
_writer(messageResourceName);
foreach (object o in messageArgs)
{
_writer((string)o);
}
}
/// <summary>
/// Logs a text warning
/// </summary>
/// <param name="buildEventContext">The build context</param>
/// <param name="subcategoryResourceName">The subcategory resource</param>
/// <param name="warningCode">The warning code</param>
/// <param name="helpKeyword">A help keyword</param>
/// <param name="file">The file</param>
/// <param name="message">The message</param>
public void LogWarningFromText(BuildEventContext buildEventContext, string subcategoryResourceName, string warningCode, string helpKeyword, BuildEventFileInfo file, string message)
{
_writer(message);
}
/// <summary>
/// Logs a build started event
/// </summary>
public void LogBuildStarted()
{
}
/// <summary>
/// Logs a build finished event
/// </summary>
/// <param name="success">Set to true if the build was successful</param>
public void LogBuildFinished(bool success)
{
}
/// <inheritdoc />
public BuildEventContext CreateEvaluationBuildEventContext(int nodeId, int submissionId)
{
return new BuildEventContext(0, 0, 0, 0, 0, 0, 0);
}
/// <inheritdoc />
public void LogProjectEvaluationStarted(BuildEventContext eventContext, string projectFile)
{
}
/// <summary>
/// Logs a project evaluation finished event
/// </summary>
public void LogProjectEvaluationFinished(
BuildEventContext projectEvaluationEventContext,
string projectFile,
IEnumerable globalProperties,
IEnumerable properties,
IEnumerable items,
ProfilerResult? profilerResult)
{
}
/// <summary>
/// Logs a project started event
/// </summary>
public BuildEventContext LogProjectStarted(BuildEventContext nodeBuildEventContext, int submissionId, int projectId, BuildEventContext parentBuildEventContext, string projectFile, string targetNames, IEnumerable<DictionaryEntry> properties, IEnumerable<DictionaryEntry> items, int evaluationId = BuildEventContext.InvalidEvaluationId)
{
return new BuildEventContext(0, 0, 0, 0);
}
/// <summary>
/// Logs a project finished event
/// </summary>
/// <param name="projectBuildEventContext">The project build event context</param>
/// <param name="projectFile">The project filename</param>
/// <param name="success">Whether it was successful or not.</param>
public void LogProjectFinished(BuildEventContext projectBuildEventContext, string projectFile, bool success)
{
}
/// <summary>
/// Logs a target started event
/// </summary>
/// <param name="projectBuildEventContext">The build event context of the project</param>
/// <param name="targetName">The name of the target</param>
/// <param name="projectFile">The project file</param>
/// <param name="projectFileOfTargetElement">The project file containing the target element</param>
/// <returns>The build event context for the target</returns>
public BuildEventContext LogTargetStarted(BuildEventContext projectBuildEventContext, string targetName, string projectFile, string projectFileOfTargetElement, string parentTargetName, TargetBuiltReason buildReason)
{
return new BuildEventContext(0, 0, 0, 0);
}
/// <summary>
/// Logs a target finished event
/// </summary>
/// <param name="targetBuildEventContext">The target's build event context</param>
/// <param name="targetName">The name of the target</param>
/// <param name="projectFile">The project file</param>
/// <param name="projectFileOfTargetElement">The project file containing the target element</param>
/// <param name="success">Whether it was successful or not.</param>
public void LogTargetFinished(BuildEventContext targetBuildEventContext, string targetName, string projectFile, string projectFileOfTargetElement, bool success, IEnumerable<TaskItem> targetOutputs)
{
}
/// <summary>
/// Logs a task started event
/// </summary>
/// <param name="targetBuildEventContext">The target's build event context</param>
/// <param name="taskName">The name of the task</param>
/// <param name="projectFile">The project file</param>
/// <param name="projectFileOfTaskNode">The project file containing the task node.</param>
public void LogTaskStarted(BuildEventContext targetBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode)
{
}
/// <summary>
/// Logs a task started event
/// </summary>
/// <param name="targetBuildEventContext">The target's build event context</param>
/// <param name="taskName">The name of the task</param>
/// <param name="projectFile">The project file</param>
/// <param name="projectFileOfTaskNode">The project file containing the task node.</param>
/// <returns>The task logging context</returns>
public BuildEventContext LogTaskStarted2(BuildEventContext targetBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode)
{
return new BuildEventContext(0, 0, 0, 0);
}
/// <summary>
/// Logs a task finished event
/// </summary>
/// <param name="taskBuildEventContext">The task's build event context</param>
/// <param name="taskName">The name of the task</param>
/// <param name="projectFile">The project file</param>
/// <param name="projectFileOfTaskNode">The project file of the task node</param>
/// <param name="success">Whether the task was successful or not.</param>
public void LogTaskFinished(BuildEventContext taskBuildEventContext, string taskName, string projectFile, string projectFileOfTaskNode, bool success)
{
}
/// <summary>
/// Logs a telemetry event.
/// </summary>
/// <param name="buildEventContext">The task's build event context</param>
/// <param name="eventName">The event name.</param>
/// <param name="properties">The list of properties associated with the event.</param>
public void LogTelemetry(BuildEventContext buildEventContext, string eventName, IDictionary<string, string> properties)
{
}
public bool HasBuildSubmissionLoggedErrors(int submissionId)
{
return false;
}
public ICollection<string> GetWarningsAsErrors(BuildEventContext context)
{
throw new NotImplementedException();
}
public ICollection<string> GetWarningsAsMessages(BuildEventContext context)
{
throw new NotImplementedException();
}
#endregion
}
}