DiagnosticsEventSink.cs (2334B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="DiagnosticsEventSink.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.Management.Configuration.UnitTests.Helpers 8 { 9 using System.Collections.Generic; 10 using Microsoft.Management.Configuration.UnitTests.Fixtures; 11 using Xunit.Abstractions; 12 using Xunit.Sdk; 13 14 /// <summary> 15 /// This class aids in getting diagnostics data from the <see cref="ConfigurationProcessor"/> out to the xUnit infrastructure. 16 /// </summary> 17 public class DiagnosticsEventSink 18 { 19 private readonly UnitTestFixture fixture; 20 private readonly ITestOutputHelper log; 21 22 /// <summary> 23 /// Initializes a new instance of the <see cref="DiagnosticsEventSink"/> class. 24 /// </summary> 25 /// <param name="fixture">Unit test fixture.</param> 26 /// <param name="log">Log helper.</param> 27 public DiagnosticsEventSink(UnitTestFixture fixture, ITestOutputHelper log) 28 { 29 this.fixture = fixture; 30 this.log = log; 31 } 32 33 /// <summary> 34 /// Gets the telemetry events that have been seen. 35 /// </summary> 36 public List<TelemetryEvent> Events { get; private set; } = new List<TelemetryEvent>(); 37 38 /// <summary> 39 /// Handles diagnostic information from a <see cref="ConfigurationProcessor"/>. 40 /// </summary> 41 /// <param name="sender">The object sending the information.</param> 42 /// <param name="e">The diagnostic information.</param> 43 internal void DiagnosticsHandler(object? sender, IDiagnosticInformation e) 44 { 45 if (e.Message.Contains(TelemetryEvent.Preamble)) 46 { 47 this.Events.Add(new TelemetryEvent(e.Message)); 48 } 49 50 if (e.Level == DiagnosticLevel.Verbose) 51 { 52 this.fixture.MessageSink.OnMessage(new DiagnosticMessage(e.Message)); 53 } 54 else 55 { 56 this.log.WriteLine(e.Message); 57 } 58 } 59 } 60 }