public void NoWarningsToConsole()
        {
            string warningMessage = "I'm a warning you want to ignore; it hurts.";

            var warningManager = new WarningManager();

            warningManager.SetState((int)EventId.WarningEvent, WarningState.Suppressed);


            // suppress the warning and check that it is not printed
            using (var console = new MockConsole())
            {
                using (var listener = new ConsoleEventListener(Events.Log, console, DateTime.UtcNow, false, warningMapper: warningManager.GetState))
                {
                    Events.Log.WarningEvent(warningMessage);
                    console.ValidateNoCall();
                }
            }

            // allow the warning
            using (var console = new MockConsole())
            {
                using (var listener = new ConsoleEventListener(Events.Log, console, DateTime.UtcNow, false))
                {
                    Events.Log.WarningEvent(warningMessage);
                    console.ValidateCall(MessageLevel.Warning, warningMessage);
                }
            }
        }
        public void NoErrorLoggingForActiveCancellationToken(bool doNotSuppressErrorLogging)
        {
            string errorMessage = "I'm an error you want to ignore; it hurts.";
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationToken       cancellationToken       = cancellationTokenSource.Token;

            // suppressError, else log the error
            if (doNotSuppressErrorLogging)
            {
                // cancel the token source
                cancellationTokenSource.Cancel();
            }

            using (var console = new MockConsole())
                using (var listener = new ConsoleEventListener(Events.Log, console, DateTime.UtcNow, false, cancellationToken))
                {
                    logError(listener);
                    if (doNotSuppressErrorLogging)
                    {
                        console.ValidateNoCall();
                    }
                    else
                    {
                        console.ValidateCall(MessageLevel.Error, errorMessage);
                    }
                }

            void logError(ConsoleEventListener listener)
            {
                listener.RegisterEventSource(TestEvents.Log);
                TestEvents.Log.ErrorEvent(errorMessage);
            }
        }
        public void NoWarningsToConsoleForActiveCancellationToken(bool suppressWarning)
        {
            string warningMessage = "I'm a warning you want to ignore; it hurts.";
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationToken       cancellationToken       = cancellationTokenSource.Token;

            // suppressWarning, else log the warnings
            if (suppressWarning)
            {
                // cancel the token source
                cancellationTokenSource.Cancel();
            }

            using (var console = new MockConsole())
                using (var listener = new ConsoleEventListener(Events.Log, console, DateTime.UtcNow, false, cancellationToken))
                {
                    logWarning(listener);
                    if (suppressWarning)
                    {
                        console.ValidateNoCall();
                    }
                    else
                    {
                        console.ValidateCall(MessageLevel.Warning, warningMessage);
                    }
                }

            void logWarning(ConsoleEventListener listener)
            {
                listener.RegisterEventSource(TestEvents.Log);
                TestEvents.Log.WarningEvent(warningMessage);
            }
        }
        public void NoWarningsToConsole(bool isFromWorker)
        {
            string warningMessage = "I'm a warning you want to ignore; it hurts.";
            string warningName    = "IgnoreWarning";
            var    warningManager = new WarningManager();

            warningManager.SetState((int)TestEvents.EventId.WarningEvent, WarningState.Suppressed);

            // suppress the warning and check that it is not printed
            using (var console = new MockConsole())
                using (var listener = new ConsoleEventListener(Events.Log, console, DateTime.UtcNow, false, CancellationToken.None, warningMapper: warningManager.GetState))
                {
                    logWarning(console, listener);
                    console.ValidateNoCall();
                }

            // allow the warning
            using (var console = new MockConsole())
                using (var listener = new ConsoleEventListener(Events.Log, console, DateTime.UtcNow, false, CancellationToken.None))
                {
                    logWarning(console, listener);
                    console.ValidateCall(MessageLevel.Warning, warningMessage);
                }

            void logWarning(MockConsole console, ConsoleEventListener listener)
            {
                listener.RegisterEventSource(TestEvents.Log);
                listener.RegisterEventSource(global::BuildXL.Engine.ETWLogger.Log);
                if (isFromWorker)
                {
                    global::BuildXL.Engine.Tracing.Logger.Log.DistributionWorkerForwardedWarning(BuildXLTestBase.CreateLoggingContextForTest(), new WorkerForwardedEvent()
                    {
                        EventId       = (int)TestEvents.EventId.WarningEvent,
                        EventName     = warningName,
                        EventKeywords = 0,
                        Text          = warningMessage,
                    });
                }
                else
                {
                    TestEvents.Log.WarningEvent(warningMessage);
                }
            }
        }