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);
            }
        }
Example #2
0
        public static IDisposable SetupEventListener(EventLevel level)
        {
            var eventListener = new ConsoleEventListener(Events.Log, DateTime.UtcNow, true, true, true, false, level: level);

            var primarySource = bxlScriptAnalyzer.ETWLogger.Log;

            if (primarySource.ConstructionException != null)
            {
                throw primarySource.ConstructionException;
            }

            eventListener.RegisterEventSource(primarySource);

            eventListener.EnableTaskDiagnostics(BuildXL.Tracing.ETWLogger.Tasks.CommonInfrastructure);

            var eventSources = new EventSource[]
            {
                bxlScriptAnalyzer.ETWLogger.Log,
                BuildXL.Engine.Cache.ETWLogger.Log,
                BuildXL.Engine.ETWLogger.Log,
                BuildXL.Scheduler.ETWLogger.Log,
                BuildXL.Tracing.ETWLogger.Log,
                BuildXL.Storage.ETWLogger.Log,
            }.Concat(FrontEndControllerFactory.GeneratedEventSources);

            using (var dummy = new TrackingEventListener(Events.Log))
            {
                foreach (var eventSource in eventSources)
                {
                    Events.Log.RegisterMergedEventSource(eventSource);
                }
            }

            return(eventListener);
        }
        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()
        {
            string warningMessage = "I'm a warning you want to ignore; it hurts.";

            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, warningMapper: warningManager.GetState))
                {
                    listener.RegisterEventSource(TestEvents.Log);

                    TestEvents.Log.WarningEvent(warningMessage);
                    console.ValidateNoCall();
                }
            }

            // allow the warning
            using (var console = new MockConsole())
            {
                using (var listener = new ConsoleEventListener(Events.Log, console, DateTime.UtcNow, false))
                {
                    listener.RegisterEventSource(TestEvents.Log);

                    TestEvents.Log.WarningEvent(warningMessage);
                    console.ValidateCall(MessageLevel.Warning, 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);
                }
            }
        }
        public void CustomPipDecsription()
        {
            string pipHash              = "PipB9ACFCBECDA09F1F";
            string somePipInformation   = "xunit.console.exe, Test.Sdk, StandardSdk.Testing.testingTest, debug-net451";
            string pipCustomDescription = "some custom description";

            string eventMessage = $"[{pipHash}, {somePipInformation}{FormattingEventListener.CustomPipDescriptionMarker}{pipCustomDescription}]";
            string eventMessageWithoutCustomDescription = $"[{pipHash}, {somePipInformation}]";
            string expectedShortenedMessage             = $"[{pipHash}, {pipCustomDescription}]";

            using (var console = new MockConsole())
            {
                using (var listener = new ConsoleEventListener(Events.Log, console, DateTime.UtcNow, true, CancellationToken.None))
                {
                    listener.RegisterEventSource(TestEvents.Log);

                    // check that the message is shortened
                    TestEvents.Log.ErrorEvent(eventMessage);
                    console.ValidateCall(MessageLevel.Error, expectedShortenedMessage);
                    TestEvents.Log.WarningEvent(eventMessage);
                    console.ValidateCall(MessageLevel.Warning, expectedShortenedMessage);
                    // event message should remain unchanged since there is no custom description in it
                    TestEvents.Log.ErrorEvent(eventMessageWithoutCustomDescription);
                    console.ValidateCall(MessageLevel.Error, eventMessageWithoutCustomDescription);
                }
            }

            // now all messages should be 'displayed' as-is
            using (var console = new MockConsole())
            {
                using (var listener = new ConsoleEventListener(Events.Log, console, DateTime.UtcNow, false, CancellationToken.None))
                {
                    listener.RegisterEventSource(TestEvents.Log);

                    TestEvents.Log.ErrorEvent(eventMessage);
                    console.ValidateCall(MessageLevel.Error, eventMessage);
                    TestEvents.Log.WarningEvent(eventMessage);
                    console.ValidateCall(MessageLevel.Warning, eventMessage);
                    TestEvents.Log.ErrorEvent(eventMessageWithoutCustomDescription);
                    console.ValidateCall(MessageLevel.Error, eventMessageWithoutCustomDescription);
                }
            }
        }
        public void ConsoleEventListenerBasicTest()
        {
            TestEvents log = TestEvents.Log;

            for (int i = 0; i < 4; i++)
            {
                DateTime baseTime = DateTime.UtcNow;
                if (i == 2)
                {
                    baseTime -= TimeSpan.FromHours(1);
                }
                else if (i == 3)
                {
                    baseTime -= TimeSpan.FromDays(1);
                }

                bool colorize = i == 0;

                using (var console = new MockConsole())
                {
                    using (var listener = new ConsoleEventListener(Events.Log, console, baseTime, false, CancellationToken.None))
                    {
                        listener.RegisterEventSource(TestEvents.Log);

                        log.VerboseEvent("1");
                        console.ValidateCall(MessageLevel.Info, "1");
                        log.InfoEvent("2");
                        console.ValidateCall(MessageLevel.Info, "2");
                        log.WarningEvent("3");
                        console.ValidateCall(MessageLevel.Warning, "3");
                        log.ErrorEvent("4");
                        console.ValidateCall(MessageLevel.Error, "4");
                        log.CriticalEvent("5");
                        console.ValidateCall(MessageLevel.Error, "5");
                        log.AlwaysEvent("6");
                        console.ValidateCall(MessageLevel.Info, "6");
                        log.VerboseEventWithProvenance("Test.cs", 1, 2, "Bad juju");
                        console.ValidateCall(MessageLevel.Info, "Test.cs(1,2): ", "Bad juju");
                    }
                }
            }
        }