public void PropertyTests()
 {
     EventSourceSink sink = new EventSourceSink();
     Assert.IsNull(sink.Name);
     string name = "Test Name";
     sink.Name = name;
     Assert.IsTrue(string.Compare(sink.Name, name, StringComparison.OrdinalIgnoreCase) == 0);
 }
 public void TestConstructorNegativeLoggerId()
 {
     Assert.Throws<InternalErrorException>(() =>
     {
         EventSourceSink testSink = new EventSourceSink();
         EventRedirectorToSink eventRedirector = new EventRedirectorToSink(-10, testSink);
     }
    );
 }
 public void ConsumeEventsGoodEventsNoHandlers()
 {
     EventSourceSink sink = new EventSourceSink();
     RaiseEventHelper eventHelper = new RaiseEventHelper(sink);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.BuildStarted);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.BuildFinished);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.NormalMessage);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.TaskFinished);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.CommandLine);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.Warning);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.Error);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.TargetStarted);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.TargetFinished);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.ProjectStarted);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.ProjectFinished);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.ExternalStartedEvent);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.ExternalStartedEvent);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.GenericStatusEvent);
 }
 public void ConsumeEventsGoodEvents()
 {
     EventSourceSink sink = new EventSourceSink();
     RaiseEventHelper eventHelper = new RaiseEventHelper(sink);
     EventHandlerHelper testHandlers = new EventHandlerHelper(sink, null);
     VerifyRegisteredHandlers(RaiseEventHelper.BuildStarted, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.BuildFinished, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.NormalMessage, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.TaskFinished, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.CommandLine, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.Warning, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.Error, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.TargetStarted, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.TargetFinished, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.ProjectStarted, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.ProjectFinished, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.ExternalStartedEvent, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.BuildStarted, eventHelper, testHandlers);
     VerifyRegisteredHandlers(RaiseEventHelper.GenericStatusEvent, eventHelper, testHandlers);
 }
        public void TestForwardingNotNullEvent()
        {
            EventSourceSink testSink = new EventSourceSink();
            EventRedirectorToSink eventRedirector = new EventRedirectorToSink(5, testSink);
            BuildMessageEventArgs messageEvent = new BuildMessageEventArgs("My message", "Help me keyword", "Sender", MessageImportance.High);
            bool wentInHandler = false;
            testSink.AnyEventRaised += new AnyEventHandler
                (
                  delegate
                  (
                    object sender,
                    BuildEventArgs buildEvent
                  )
                  {
                      wentInHandler = true;
                      BuildMessageEventArgs messageEventFromPacket = buildEvent as BuildMessageEventArgs;
                      Assert.IsTrue(messageEvent == messageEventFromPacket, "Expected messageEvent to be forwarded to match actually forwarded event");
                  }

                );

            ((IEventRedirector)eventRedirector).ForwardEvent(messageEvent);
            Assert.IsTrue(wentInHandler, "Expected to go into event handler");
        }
 /// <summary>
 /// Take an event and an exception to raise, create a new sink and raise the event on it. 
 /// In the event handler registered on the sink, the exception will be thrown.
 /// </summary>
 /// <param name="buildEventToRaise">BuildEvent to raise on the </param>
 /// <param name="exceptionToRaise">Exception to throw in the event handler </param>
 private static void RaiseExceptionInEventHandler(BuildEventArgs buildEventToRaise, Exception exceptionToRaise)
 {
     EventSourceSink sink = new EventSourceSink();
     RaiseEventHelper eventHelper = new RaiseEventHelper(sink);
     EventHandlerHelper testHandlers = new EventHandlerHelper(sink, exceptionToRaise);
     try
     {
         eventHelper.RaiseBuildEvent(buildEventToRaise);
     }
     catch (Exception e)
     {
         // Logger exceptions should be rethrown as is with no wrapping
         if (exceptionToRaise is LoggerException)
         {
             Assert.IsTrue(e == exceptionToRaise, "Expected Logger exception to be raised in event handler and re-thrown by event source");
         }
         else
         {
             if (ExceptionHandling.IsCriticalException(e))
             {
                 Assert.IsTrue(e == exceptionToRaise, "Expected Logger exception to be raised in event handler and re-thrown by event source");
             }
             else
             {
                 // All other exceptions should be wrapped in an InternalLoggerException, with the original exception as the inner exception
                 Assert.IsTrue(e is InternalLoggerException, "Expected general exception to be raised in event handler and re-thrown by event source as a InternalLoggerException");
             }
         }
     }
 }
        public void LowMessageColorTest()
        {
            EventSourceSink es = new EventSourceSink();
            SimulatedConsole sc = new SimulatedConsole();
            ConsoleLogger L = new ConsoleLogger(LoggerVerbosity.Diagnostic,
                                                sc.Write, sc.SetColor,
                                                sc.ResetColor);
            L.Initialize(es);

            BuildMessageEventArgs msg =
                new BuildMessageEventArgs("text", "help", "sender",
                                          MessageImportance.Low);

            es.Consume(msg);

            Assert.AreEqual(
               "<darkgray>text" +
               Environment.NewLine + "<reset color>",
               sc.ToString());
        }
 public void RaiseGenericBuildEventArgs()
 {
     EventSourceSink sink = new EventSourceSink();
     RaiseEventHelper eventHelper = new RaiseEventHelper(sink);
     eventHelper.RaiseBuildEvent(RaiseEventHelper.GenericBuildEvent);
 }
        public void VerifyShutdown()
        {
            EventSourceSink sink = new EventSourceSink();

            // Registers event handlers onto the event source 
            EventHandlerHelper handlerHelper = new EventHandlerHelper(sink, null);
            RaiseEventHelper raiseEventHelper = new RaiseEventHelper(sink);

            raiseEventHelper.RaiseBuildEvent(RaiseEventHelper.ProjectStarted);
            Assert.IsTrue(handlerHelper.EnteredEventHandler);
            Assert.IsTrue(handlerHelper.EnteredAnyEventHandler);
            Assert.IsTrue(handlerHelper.EnteredStatusEventHandler);
            Assert.IsTrue(handlerHelper.RaisedEvent == RaiseEventHelper.ProjectStarted);
            Assert.IsTrue(handlerHelper.RaisedAnyEvent == RaiseEventHelper.ProjectStarted);
            Assert.IsTrue(handlerHelper.RaisedStatusEvent == RaiseEventHelper.ProjectStarted);

            sink.ShutDown();

            handlerHelper.ResetRaisedEvent();
            raiseEventHelper.RaiseBuildEvent(RaiseEventHelper.ProjectStarted);
            Assert.IsFalse(handlerHelper.EnteredEventHandler);
            Assert.IsFalse(handlerHelper.EnteredAnyEventHandler);
            Assert.IsFalse(handlerHelper.EnteredStatusEventHandler);
            Assert.IsNull(handlerHelper.RaisedEvent);
            Assert.IsNull(handlerHelper.RaisedAnyEvent);
            Assert.IsNull(handlerHelper.RaisedStatusEvent);
        }
 public void TestConstructorValidInputs()
 {
     EventSourceSink testSink = new EventSourceSink();
     EventRedirectorToSink eventRedirector = new EventRedirectorToSink(5, testSink);
     Assert.IsNotNull(eventRedirector, "eventRedirector was not supposed to be null");
 }
        public void TestMinimalWithNormalMessage()
        {
            for (int i = 1; i <= 2; i++)
            {
                EventSourceSink es = new EventSourceSink();
                SimulatedConsole sc = new SimulatedConsole();
                ConsoleLogger L = new ConsoleLogger(LoggerVerbosity.Minimal,
                                                    sc.Write, sc.SetColor,
                                                    sc.ResetColor);
                L.Initialize(es, i);

                BuildEventContext buildEventContext = new BuildEventContext(1, 2, 3, 4);

                BuildStartedEventArgs bse = new BuildStartedEventArgs("bs", null);
                bse.BuildEventContext = buildEventContext;
                es.Consume(bse);

                ProjectStartedEventArgs pse = new ProjectStartedEventArgs(1, "ps", null, "fname", "", null, null, new BuildEventContext(1, 1, 1, 1));
                pse.BuildEventContext = buildEventContext;
                es.Consume(pse);

                TargetStartedEventArgs trse = new TargetStartedEventArgs("ts", null, "trname", "pfile", "tfile");
                trse.BuildEventContext = buildEventContext;
                es.Consume(trse);

                TaskStartedEventArgs tase = new TaskStartedEventArgs("tks", null, "tname", "tfname", "tsname");
                tase.BuildEventContext = buildEventContext;
                es.Consume(tase);

                BuildMessageEventArgs bmea = new BuildMessageEventArgs("foo!", null, "sender", MessageImportance.Normal);
                bmea.BuildEventContext = buildEventContext;
                es.Consume(bmea);

                TaskFinishedEventArgs tafea = new TaskFinishedEventArgs("tkf", null, "fname", "tsname", "tfname", true);
                tafea.BuildEventContext = buildEventContext;
                es.Consume(tafea);

                TargetFinishedEventArgs trfea = new TargetFinishedEventArgs("tf", null, "trname", "fname", "tfile", true);
                trfea.BuildEventContext = buildEventContext;
                es.Consume(trfea);

                ProjectFinishedEventArgs pfea = new ProjectFinishedEventArgs("pf", null, "fname", true);
                pfea.BuildEventContext = buildEventContext;
                es.Consume(pfea);

                BuildFinishedEventArgs bfea = new BuildFinishedEventArgs("bf", null, true);
                bfea.BuildEventContext = buildEventContext;
                es.Consume(bfea);

                Assert.AreEqual(String.Empty, sc.ToString());
            }
        }
 private void RaiseEvents(EventSourceSink source)
 {
     source.Consume(s_buildStarted);
     source.Consume(s_projectStarted);
     source.Consume(s_targetStarted);
     source.Consume(s_taskStarted);
     source.Consume(s_lowMessage);
     source.Consume(s_normalMessage);
     source.Consume(s_highMessage);
     source.Consume(s_commandLine);
     source.Consume(s_externalStartedEvent);
     source.Consume(s_warning);
     source.Consume(s_error);
     source.Consume(s_taskFinished);
     source.Consume(s_targetFinished);
     source.Consume(s_projectFinished);
     source.Consume(s_buildFinished);
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="eventSource">Event source on which the events will be raised</param>
 internal RaiseEventHelper(EventSourceSink eventSource)
 {
     _sourceForEvents = eventSource;
 }
 public void ForwardingLoggingPerformanceSummary()
 {
     EventSourceSink source = new EventSourceSink();
     TestForwardingLogger logger = new TestForwardingLogger();
     logger.BuildEventRedirector = null;
     logger.Parameters = "PERFORMANCESUMMARY";
     logger.Verbosity = LoggerVerbosity.Quiet;
     logger.Initialize(source, 4);
     RaiseEvents(source);
     Assert.IsTrue(logger.forwardedEvents.Count == 10);
     Assert.IsTrue(logger.forwardedEvents.Contains(s_buildStarted));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_buildFinished));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_error));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_warning));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_projectStarted));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_projectFinished));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_targetStarted));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_targetFinished));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_taskStarted));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_taskFinished));
 }
 public void ForwardingLoggingShowCommandLine()
 {
     EventSourceSink source = new EventSourceSink();
     TestForwardingLogger logger = new TestForwardingLogger();
     logger.BuildEventRedirector = null;
     logger.Verbosity = LoggerVerbosity.Normal;
     logger.Parameters = "SHOWCOMMANDLINE";
     logger.Initialize(source, 4);
     RaiseEvents(source);
     Assert.IsTrue(logger.forwardedEvents.Count == 11);
     Assert.IsTrue(logger.forwardedEvents.Contains(s_buildStarted));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_buildFinished));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_error));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_warning));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_highMessage));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_normalMessage));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_projectStarted));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_projectFinished));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_targetStarted));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_targetFinished));
     Assert.IsTrue(logger.forwardedEvents.Contains(s_commandLine));
 }
Exemple #16
0
        /// <summary>
        /// Register a distributed logger. This involves creating a new eventsource sink 
        /// and associating this with the central logger. In addition the sinkId needs 
        /// to be put in the loggerDescription so that nodes know what they need to 
        /// tag onto the event so that the message goes to the correct logger.
        /// 
        /// The central logger is initialized before the distributed logger
        /// </summary>
        /// <param name="centralLogger">Central logger to receive messages from the forwarding logger, This logger cannot have been registered before</param>
        /// <param name="forwardingLogger">Logger description which describes how to create the forwarding logger, the logger description cannot have been used before</param>
        /// <returns value="bool">True if the distributed and central logger were registered, false if they either were already registered</returns>
        /// <exception cref="InternalErrorException">If forwardingLogger is null</exception>
        /// <exception cref="LoggerException">If a logger exception is thrown while creating or initializing the distributed or central logger</exception>
        /// <exception cref="InternalLoggerException">If any exception (other than a loggerException)is thrown while creating or initializing the distributed or central logger, we will wrap these exceptions in an InternalLoggerException</exception>
        public bool RegisterDistributedLogger(ILogger centralLogger, LoggerDescription forwardingLogger)
        {
            lock (_lockObject)
            {
                ErrorUtilities.VerifyThrow(_serviceState != LoggingServiceState.Shutdown, " The object is shutdown, should not do any operations on a shutdown component");
                ErrorUtilities.VerifyThrow(forwardingLogger != null, "forwardingLogger was null");
                if (centralLogger == null)
                {
                    centralLogger = new NullCentralLogger();
                }

                IForwardingLogger localForwardingLogger = null;

                // create an eventSourceSink which the central logger will register with to receive the events from the forwarding logger
                EventSourceSink eventSourceSink = new EventSourceSink();

                // If the logger is already in the list it should not be registered again.
                if (_iloggerList.Contains(centralLogger))
                {
                    return false;
                }

                // Assign a unique logger Id to this distributed logger
                int sinkId = _nextSinkId++;
                forwardingLogger.LoggerId = sinkId;
                eventSourceSink.Name = "Sink for forwarding logger \"" + sinkId + "\".";

                // Initialize and register the central logger
                InitializeLogger(centralLogger, eventSourceSink);

                localForwardingLogger = forwardingLogger.CreateForwardingLogger();
                EventRedirectorToSink newRedirector = new EventRedirectorToSink(sinkId, eventSourceSink);
                localForwardingLogger.BuildEventRedirector = newRedirector;
                localForwardingLogger.Parameters = forwardingLogger.LoggerSwitchParameters;
                localForwardingLogger.Verbosity = forwardingLogger.Verbosity;

                // Give the forwarding logger registered on the inproc node the correct ID.
                localForwardingLogger.NodeId = 1;

                // Convert the path to the logger DLL to full path before passing it to the node provider
                forwardingLogger.ConvertPathsToFullPaths();

                CreateFilterEventSource();

                // Initialize and register the forwarding logger
                InitializeLogger(localForwardingLogger, _filterEventSource);

                _loggerDescriptions.Add(forwardingLogger);

                _eventSinkDictionary.Add(sinkId, eventSourceSink);

                return true;
            }
        }
Exemple #17
0
        /// <summary>
        /// NotThreadSafe, this method should only be called from the component host thread
        /// Called by the build component host when the component host is about to shutdown.
        /// 1. Shutdown forwarding loggers so that any events they have left to forward can get into the queue
        /// 2. Terminate the logging thread
        /// 3. Null out sinks and the filter event source so that no more events can get to the central loggers
        /// 4. Shutdown the central loggers
        /// </summary>
        /// <exception cref="InternalErrorException">Service has already shutdown</exception>
        /// <exception cref="LoggerException"> A logger may throw a logger exception when shutting down</exception>
        /// <exception cref="InternalLoggerException">A logger will wrap other exceptions (except ExceptionHandling.IsCriticalException exceptions) in a InternalLoggerException if it crashes during shutdown</exception>
        public void ShutdownComponent()
        {
            lock (_lockObject)
            {
                ErrorUtilities.VerifyThrow(_serviceState != LoggingServiceState.Shutdown, " The object is shutdown, should not do any operations on a shutdown component");

                // Set the state to indicate we are starting the shutdown process.
                _serviceState = LoggingServiceState.ShuttingDown;

                try
                {
                    try
                    {
                        // 1. Shutdown forwarding loggers so that any events they have left to forward can get into the queue
                        foreach (ILogger logger in _iloggerList)
                        {
                            if (logger is IForwardingLogger)
                            {
                                ShutdownLogger(logger);
                            }
                        }
                    }
                    finally
                    {
                        // 2. Terminate the logging event queue
                        if (_logMode == LoggerMode.Asynchronous)
                        {
                            TerminateLoggingEventQueue();
                        }
                    }

                    // 3. Null out sinks and the filter event source so that no more events can get to the central loggers
                    if (_filterEventSource != null)
                    {
                        _filterEventSource.ShutDown();
                    }

                    foreach (IBuildEventSink sink in _eventSinkDictionary.Values)
                    {
                        sink.ShutDown();
                    }

                    // 4. Shutdown the central loggers
                    foreach (ILogger logger in _iloggerList)
                    {
                        ShutdownLogger(logger);
                    }
                }
                finally
                {
                    // Revert the centralLogger sinId back to -1 so that when another central logger is registered, it will generate a new
                    // sink for the central loggers.
                    _centralLoggerSinkId = -1;

                    // Clean up anything related to the asynchronous logging
                    if (_logMode == LoggerMode.Asynchronous)
                    {
                        _loggingQueue = null;
                        _loggingQueueProcessor = null;
                    }

                    _iloggerList = new List<ILogger>();
                    _loggerDescriptions = null;
                    _eventSinkDictionary = null;
                    _filterEventSource = null;
                    _serviceState = LoggingServiceState.Shutdown;
                }
            }
        }
Exemple #18
0
 /// <summary>
 /// Create an event source to which the distributed (filter loggers) can attach to and listen
 /// for logging events. This event source will consume events which are logged against
 /// the logging service and raise them on itself.
 /// </summary>
 private void CreateFilterEventSource()
 {
     if (_filterEventSource == null)
     {
         _filterEventSource = new EventSourceSink();
         _filterEventSource.Name = "Sink for Distributed/Filter loggers";
     }
 }
        public void WarningColorTestParallel()
        {
            EventSourceSink es = new EventSourceSink();
            SimulatedConsole sc = new SimulatedConsole();
            ConsoleLogger L = new ConsoleLogger(LoggerVerbosity.Quiet,
                                                sc.Write, sc.SetColor,
                                                sc.ResetColor);
            L.Initialize(es, 2);

            BuildWarningEventArgs bwea = new BuildWarningEventArgs("VBC",
                        "31415", "file.vb", 42, 0, 0, 0,
                        "Some long message", "help", "sender");

            bwea.BuildEventContext = new BuildEventContext(1, 2, 3, 4);
            es.Consume(bwea);

            Assert.AreEqual(
               "<yellow>file.vb(42): VBC warning 31415: Some long message" +
               Environment.NewLine + "<reset color>",
               sc.ToString());
        }
 public void TestForwardingNullEvent()
 {
     EventSourceSink testSink = new EventSourceSink();
     EventRedirectorToSink eventRedirector = new EventRedirectorToSink(5, testSink);
     ((IEventRedirector)eventRedirector).ForwardEvent(null);
 }
        public void ErrorColorTest()
        {
            EventSourceSink es = new EventSourceSink();
            SimulatedConsole sc = new SimulatedConsole();

            ConsoleLogger L = new ConsoleLogger(LoggerVerbosity.Quiet, sc.Write, sc.SetColor, sc.ResetColor);
            L.Initialize(es);

            BuildErrorEventArgs beea = new BuildErrorEventArgs("VBC", "31415", "file.vb", 42, 0, 0, 0, "Some long message", "help", "sender");
            es.Consume(beea);
            Assert.AreEqual("<red>file.vb(42): VBC error 31415: Some long message" + Environment.NewLine + "<reset color>", sc.ToString());
        }
 /// <summary>
 /// Create a new forwarding logger, event redirector, and event source.
 /// The returned event source can then have and event raised on it and it can 
 /// check to see if the event raised matches the one we were expecting.
 /// </summary>
 /// <param name="buildEventToCheck">A build event we are expecting to be forwarded by the forwarding logger</param>
 /// <returns>An event source on which one can raise an event.</returns>
 private static EventSourceSink AttachForwardingLoggerAndRedirector(BuildEventArgs buildEventToCheck)
 {
     EventSourceSink loggerEventSource = new EventSourceSink();
     CentralForwardingLogger forwardingLogger = new CentralForwardingLogger();
     TestEventRedirector eventRedirector = new TestEventRedirector(buildEventToCheck);
     forwardingLogger.BuildEventRedirector = eventRedirector;
     forwardingLogger.Initialize(loggerEventSource);
     return loggerEventSource;
 }
 public void TestConstructorNegativeLoggerId()
 {
     EventSourceSink testSink = new EventSourceSink();
     EventRedirectorToSink eventRedirector = new EventRedirectorToSink(-10, testSink);
 }
        public void NullEventFieldsParallel()
        {
            EventSourceSink es = new EventSourceSink();
            SimulatedConsole sc = new SimulatedConsole();
            ConsoleLogger L = new ConsoleLogger(LoggerVerbosity.Diagnostic,
                                                sc.Write, sc.SetColor,
                                                sc.ResetColor);
            L.Initialize(es, 2);
            BuildEventContext buildEventContext = new BuildEventContext(1, 2, 3, 4);

            BuildStartedEventArgs bse = new BuildStartedEventArgs(null, null);
            bse.BuildEventContext = buildEventContext;
            ProjectStartedEventArgs pse = new ProjectStartedEventArgs(-1, null, null, "p", null, null, null, buildEventContext);
            pse.BuildEventContext = buildEventContext;
            TargetStartedEventArgs trse = new TargetStartedEventArgs(null, null, "t", null, null);
            trse.BuildEventContext = buildEventContext;
            TaskStartedEventArgs tase = new TaskStartedEventArgs(null, null, null, null, "task");
            tase.BuildEventContext = buildEventContext;
            BuildMessageEventArgs bmea = new BuildMessageEventArgs(null, null, null, MessageImportance.High);
            bmea.BuildEventContext = buildEventContext;
            BuildWarningEventArgs bwea = new BuildWarningEventArgs(null, null, null, 0, 0, 0, 0, null, null, null);
            bwea.BuildEventContext = buildEventContext;
            BuildErrorEventArgs beea = new BuildErrorEventArgs(null, null, null, 0, 0, 0, 0, null, null, null);
            beea.BuildEventContext = buildEventContext;
            TaskFinishedEventArgs trfea = new TaskFinishedEventArgs(null, null, null, null, "task", true);
            trfea.BuildEventContext = buildEventContext;
            TargetFinishedEventArgs tafea = new TargetFinishedEventArgs(null, null, "t", null, null, true);
            tafea.BuildEventContext = buildEventContext;
            ProjectFinishedEventArgs pfea = new ProjectFinishedEventArgs(null, null, "p", true);
            pfea.BuildEventContext = buildEventContext;
            BuildFinishedEventArgs bfea = new BuildFinishedEventArgs(null, null, true);
            bfea.BuildEventContext = buildEventContext;
            MyCustomBuildEventArgs2 mcea = new MyCustomBuildEventArgs2();
            mcea.BuildEventContext = buildEventContext;


            // Not all parameters are null here, but that's fine, we assume the engine will never
            // fire a ProjectStarted without a project name, etc.
            es.Consume(bse);
            es.Consume(pse);
            es.Consume(trse);
            es.Consume(tase);
            es.Consume(bmea);
            es.Consume(bwea);
            es.Consume(beea);
            es.Consume(trfea);
            es.Consume(tafea);
            es.Consume(pfea);
            es.Consume(bfea);
            es.Consume(bfea);
            es.Consume(bfea);
            es.Consume(mcea);
            // No exception raised
        }
        public void ForwardingLoggingEventsBasedOnVerbosity()
        {
            EventSourceSink source = new EventSourceSink();
            TestForwardingLogger logger = new TestForwardingLogger();
            logger.BuildEventRedirector = null;
            logger.Parameters = "BUILDSTARTEDEVENT";
            logger.Initialize(source, 4);
            RaiseEvents(source);
            Assert.IsTrue(logger.forwardedEvents.Count == 1);

            logger = new TestForwardingLogger();
            logger.BuildEventRedirector = null;
            logger.Verbosity = LoggerVerbosity.Quiet;
            logger.Initialize(source, 4);
            RaiseEvents(source);
            Assert.IsTrue(logger.forwardedEvents.Count == 4);
            Assert.IsTrue(logger.forwardedEvents.Contains(s_buildStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_buildFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_error));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_warning));

            logger = new TestForwardingLogger();
            logger.BuildEventRedirector = null;
            logger.Verbosity = LoggerVerbosity.Minimal;
            logger.Initialize(source, 4);
            RaiseEvents(source);
            Assert.IsTrue(logger.forwardedEvents.Count == 5);
            Assert.IsTrue(logger.forwardedEvents.Contains(s_buildStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_buildFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_error));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_warning));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_highMessage));

            logger = new TestForwardingLogger();
            logger.BuildEventRedirector = null;
            logger.Verbosity = LoggerVerbosity.Normal;
            logger.Initialize(source, 4);
            RaiseEvents(source);
            Assert.IsTrue(logger.forwardedEvents.Count == 11);
            Assert.IsTrue(logger.forwardedEvents.Contains(s_buildStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_buildFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_error));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_warning));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_highMessage));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_normalMessage));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_projectStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_projectFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_targetStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_targetFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_commandLine));

            logger = new TestForwardingLogger();
            logger.BuildEventRedirector = null;
            logger.Verbosity = LoggerVerbosity.Detailed;
            logger.Initialize(source, 4);
            RaiseEvents(source);
            Assert.IsTrue(logger.forwardedEvents.Count == 14);
            Assert.IsTrue(logger.forwardedEvents.Contains(s_buildStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_buildFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_error));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_warning));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_highMessage));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_lowMessage));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_normalMessage));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_projectStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_projectFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_targetStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_targetFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_taskStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_taskFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_commandLine));

            logger = new TestForwardingLogger();
            logger.BuildEventRedirector = null;
            logger.Verbosity = LoggerVerbosity.Diagnostic;
            logger.Initialize(source, 4);
            RaiseEvents(source);
            Assert.IsTrue(logger.forwardedEvents.Count == 15);
            Assert.IsTrue(logger.forwardedEvents.Contains(s_buildStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_buildFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_error));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_warning));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_highMessage));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_lowMessage));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_normalMessage));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_projectStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_projectFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_targetStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_targetFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_taskStarted));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_taskFinished));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_externalStartedEvent));
            Assert.IsTrue(logger.forwardedEvents.Contains(s_commandLine));
        }
        public void TestQuietWithWarning()
        {
            for (int i = 1; i <= 2; i++)
            {
                EventSourceSink es = new EventSourceSink();
                SimulatedConsole sc = new SimulatedConsole();
                ConsoleLogger L = new ConsoleLogger(LoggerVerbosity.Quiet,
                                                    sc.Write, sc.SetColor, sc.ResetColor);
                L.Initialize(es, i);

                BuildEventContext buildEventContext = new BuildEventContext(1, 2, 3, 4);

                BuildStartedEventArgs bse = new BuildStartedEventArgs("bs", null);
                bse.BuildEventContext = buildEventContext;
                es.Consume(bse);

                ProjectStartedEventArgs pse = new ProjectStartedEventArgs(-1, "ps", null, "fname", "", null, null, new BuildEventContext(1, 2, 3, 4));
                pse.BuildEventContext = buildEventContext;
                es.Consume(pse);

                TargetStartedEventArgs trse = new TargetStartedEventArgs("ts", null, "trname", "pfile", "tfile");
                trse.BuildEventContext = buildEventContext;
                es.Consume(trse);

                TaskStartedEventArgs tase = new TaskStartedEventArgs("tks", null, "tname", "tfname", "tsname");
                tase.BuildEventContext = buildEventContext;
                es.Consume(tase);

                BuildWarningEventArgs beea = new BuildWarningEventArgs("VBC",
                                "31415", "file.vb", 42, 0, 0, 0,
                                "Some long message", "help", "sender");


                beea.BuildEventContext = buildEventContext;
                es.Consume(beea);

                TaskFinishedEventArgs tafea = new TaskFinishedEventArgs("tkf", null, "fname", "tsname", "tfname", true);
                tafea.BuildEventContext = buildEventContext;
                es.Consume(tafea);

                TargetFinishedEventArgs trfea = new TargetFinishedEventArgs("tf", null, "trname", "fname", "tfile", true);
                trfea.BuildEventContext = buildEventContext;
                es.Consume(trfea);

                ProjectFinishedEventArgs pfea = new ProjectFinishedEventArgs("pf", null, "fname", true);
                pfea.BuildEventContext = buildEventContext;
                es.Consume(pfea);

                BuildFinishedEventArgs bfea = new BuildFinishedEventArgs("bf", null, true);
                bfea.BuildEventContext = buildEventContext;
                es.Consume(bfea);

                Console.WriteLine("==");
                Console.WriteLine(sc.ToString());
                Console.WriteLine("==");

                if (i == 1)
                {
                    Assert.AreEqual(
                            "<cyan>" + BaseConsoleLogger.projectSeparatorLine + Environment.NewLine +
                            ResourceUtilities.FormatResourceString("ProjectStartedPrefixForTopLevelProjectWithDefaultTargets", "fname") + Environment.NewLine + Environment.NewLine +
                            "<reset color><yellow>file.vb(42): VBC warning 31415: Some long message" + Environment.NewLine +
                            "<reset color><cyan>pf" + Environment.NewLine +
                            "<reset color>",
                            sc.ToString());
                }
                else
                {
                    Assert.AreEqual(
                            "<yellow>file.vb(42): VBC warning 31415: Some long message" + Environment.NewLine + "<reset color>",
                            sc.ToString());
                }
            }
        }
 public void RaiseEventWithNoBuildEventRedirector()
 {
     BuildMessageEventArgs normalMessage = new BuildMessageEventArgs("Message2", "help", "sender", MessageImportance.Normal);
     EventSourceSink loggerSource = new EventSourceSink();
     CentralForwardingLogger forwardingLogger = new CentralForwardingLogger();
     forwardingLogger.Initialize(loggerSource);
     loggerSource.Consume(normalMessage);
 }
 public void TestForwardingNullEvent()
 {
     Assert.Throws<InternalErrorException>(() =>
     {
         EventSourceSink testSink = new EventSourceSink();
         EventRedirectorToSink eventRedirector = new EventRedirectorToSink(5, testSink);
         ((IEventRedirector)eventRedirector).ForwardEvent(null);
     }
    );
 }
        public void RegisterGoodDiscriptions()
        {
            string configurableClassName = "Microsoft.Build.Logging.ConfigurableForwardingLogger";
            string distributedClassName = "Microsoft.Build.BackEnd.Logging.CentralForwardingLogger";
            EventSourceSink sink = new EventSourceSink();
            EventSourceSink sink2 = new EventSourceSink();
            List<LoggerDescription> loggerDescriptions = new List<LoggerDescription>();
            loggerDescriptions.Add(CreateLoggerDescription(configurableClassName, Assembly.GetAssembly(typeof(ProjectCollection)).FullName, true));
            loggerDescriptions.Add(CreateLoggerDescription(distributedClassName, Assembly.GetAssembly(typeof(ProjectCollection)).FullName, true));

            // Register some descriptions with a sink
            _initializedService.InitializeNodeLoggers(loggerDescriptions, sink, 1);

            // Register the same descriptions with another sink (so we can see that another sink was added)
            _initializedService.InitializeNodeLoggers(loggerDescriptions, sink2, 1);

            // Register the descriptions again with the same sink so we can verify that another sink was not created
            _initializedService.InitializeNodeLoggers(loggerDescriptions, sink, 1);

            Assert.NotNull(_initializedService.RegisteredLoggerTypeNames);

            // Should have 6 forwarding logger. three of each type
            Assert.Equal(6, _initializedService.RegisteredLoggerTypeNames.Count);
            Assert.True(_initializedService.RegisteredLoggerTypeNames.Contains("Microsoft.Build.Logging.ConfigurableForwardingLogger"));
            Assert.True(_initializedService.RegisteredLoggerTypeNames.Contains("Microsoft.Build.BackEnd.Logging.CentralForwardingLogger"));

            int countForwardingLogger = 0;
            foreach (string loggerName in _initializedService.RegisteredLoggerTypeNames)
            {
                if (String.Compare("Microsoft.Build.Logging.ConfigurableForwardingLogger", loggerName, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    countForwardingLogger++;
                }
            }

            // Should be 3, one for each call to RegisterLoggerDescriptions
            Assert.Equal(3, countForwardingLogger);

            countForwardingLogger = 0;
            foreach (string loggerName in _initializedService.RegisteredLoggerTypeNames)
            {
                if (String.Compare("Microsoft.Build.BackEnd.Logging.CentralForwardingLogger", loggerName, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    countForwardingLogger++;
                }
            }

            // Should be 3, one for each call to RegisterLoggerDescriptions
            Assert.Equal(3, countForwardingLogger);

            // Should have 2 event sink
            Assert.NotNull(_initializedService.RegisteredSinkNames);
            Assert.Equal(2, _initializedService.RegisteredSinkNames.Count);

            // There should not be any (this method is to be called on a child node)
            Assert.Equal(0, _initializedService.LoggerDescriptions.Count);
        }
        public void NullEventFields()
        {
            EventSourceSink es = new EventSourceSink();
            SimulatedConsole sc = new SimulatedConsole();
            ConsoleLogger L = new ConsoleLogger(LoggerVerbosity.Diagnostic,
                                                sc.Write, sc.SetColor,
                                                sc.ResetColor);
            L.Initialize(es);

            // Not all parameters are null here, but that's fine, we assume the engine will never
            // fire a ProjectStarted without a project name, etc.
            es.Consume(new BuildStartedEventArgs(null, null));
            es.Consume(new ProjectStartedEventArgs(null, null, "p", null, null, null));
            es.Consume(new TargetStartedEventArgs(null, null, "t", null, null));
            es.Consume(new TaskStartedEventArgs(null, null, null, null, "task"));
            es.Consume(new BuildMessageEventArgs(null, null, null, MessageImportance.High));
            es.Consume(new BuildWarningEventArgs(null, null, null, 0, 0, 0, 0, null, null, null));
            es.Consume(new BuildErrorEventArgs(null, null, null, 0, 0, 0, 0, null, null, null));
            es.Consume(new TaskFinishedEventArgs(null, null, null, null, "task", true));
            es.Consume(new TargetFinishedEventArgs(null, null, "t", null, null, true));
            es.Consume(new ProjectFinishedEventArgs(null, null, "p", true));
            es.Consume(new BuildFinishedEventArgs(null, null, true));
            es.Consume(new BuildFinishedEventArgs(null, null, true));
            es.Consume(new BuildFinishedEventArgs(null, null, true));
            es.Consume(new MyCustomBuildEventArgs2());
            // No exception raised
        }