Esempio n. 1
0
        internal static string GetParentTargetFromTargetSkipped(BuildEventArgs e)
        {
            if (parentTargetFromTargetSkipped == null)
            {
                Type       type   = e.GetType();
                MethodInfo method = type.GetProperty("ParentTarget").GetGetMethod();
                parentTargetFromTargetSkipped = b => method.Invoke(b, null) as string;
            }

            return(parentTargetFromTargetSkipped(e));
        }
Esempio n. 2
0
        internal static string GetUnexpandedProject(BuildEventArgs e)
        {
            if (UnexpandedProjectGetter == null)
            {
                var type   = e.GetType().GetTypeInfo();
                var method = type.GetProperty("UnexpandedProject").GetGetMethod();
                UnexpandedProjectGetter = b => method.Invoke(b, null) as string;
            }

            return(UnexpandedProjectGetter(e));
        }
Esempio n. 3
0
        internal static string GetProjectFileFromEvaluationStarted(BuildEventArgs e)
        {
            if (projectFileFromEvaluationStarted == null)
            {
                Type       type   = e.GetType();
                MethodInfo method = type.GetProperty("ProjectFile").GetGetMethod();
                projectFileFromEvaluationStarted = b => method.Invoke(b, null) as string;
            }

            return(projectFileFromEvaluationStarted(e));
        }
Esempio n. 4
0
        internal static string GetImportedProjectFile(BuildEventArgs e)
        {
            if (ImportedProjectFileGetter == null)
            {
                var type   = e.GetType().GetTypeInfo();
                var method = type.GetProperty("ImportedProjectFile").GetGetMethod();
                ImportedProjectFileGetter = b => method.Invoke(b, null) as string;
            }

            return(ImportedProjectFileGetter(e));
        }
Esempio n. 5
0
        internal static string GetTargetNameFromTargetSkipped(BuildEventArgs e)
        {
            if (TargetNameFromTargetSkipped == null)
            {
                var type   = e.GetType().GetTypeInfo();
                var method = type.GetProperty("TargetName").GetGetMethod();
                TargetNameFromTargetSkipped = b => method.Invoke(b, null) as string;
            }

            return(TargetNameFromTargetSkipped(e));
        }
Esempio n. 6
0
        internal static string GetProjectFileFromEvaluationFinished(BuildEventArgs e)
        {
            if (ProjectFileFromEvaluationFinished == null)
            {
                var type   = e.GetType().GetTypeInfo();
                var method = type.GetProperty("ProjectFile").GetGetMethod();
                ProjectFileFromEvaluationFinished = b => method.Invoke(b, null) as string;
            }

            return(ProjectFileFromEvaluationFinished(e));
        }
        internal static TargetBuiltReason GetBuildReasonFromTargetStarted(BuildEventArgs e)
        {
            var type = e.GetType().GetTypeInfo();
            var property = type.GetProperty("BuildReason");
            if (property == null)
            {
                return TargetBuiltReason.None;
            }

            var method = property.GetGetMethod();
            return (TargetBuiltReason)method.Invoke(e, null);
        }
Esempio n. 8
0
        /// <summary>
        /// Extension method to help our tests without adding shipping code.
        /// Compare this build event context with another object to determine
        /// equality. This means the values inside the object are identical.
        /// </summary>
        /// <param name="args">The 'this' object</param>
        /// <param name="other">Object to compare to this object</param>
        /// <returns>True if the object values are identical, false if they are not identical</returns>
        public static bool IsEquivalent(this BuildEventArgs args, BuildEventArgs other)
        {
            if (Object.ReferenceEquals(args, other))
            {
                return(true);
            }

            if (Object.ReferenceEquals(other, null) || Object.ReferenceEquals(args, null))
            {
                return(false);
            }

            if (args.GetType() != other.GetType())
            {
                return(false);
            }

            if (args.Timestamp.Ticks != other.Timestamp.Ticks)
            {
                return(false);
            }

            if (args.BuildEventContext != other.BuildEventContext)
            {
                return(false);
            }

            if (!String.Equals(args.HelpKeyword, other.HelpKeyword, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // Just in case we're matching chk against ret or vice versa, make sure the message still registers as the same
            string fixedArgsMessage  = args.Message.Replace("\r\nThis is an unhandled exception from a task -- PLEASE OPEN A BUG AGAINST THE TASK OWNER.", String.Empty);
            string fixedOtherMessage = other.Message.Replace("\r\nThis is an unhandled exception from a task -- PLEASE OPEN A BUG AGAINST THE TASK OWNER.", String.Empty);

            if (!String.Equals(fixedArgsMessage, fixedOtherMessage, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            if (!String.Equals(args.SenderName, other.SenderName, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            if (args.ThreadId != other.ThreadId)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 9
0
 private void OnAnyEvent(object sender, BuildEventArgs args)
 {
     if (args is TargetStartedEventArgs targetStartedArgs)
     {
         debugger.OnTargetEnter(targetStartedArgs.TargetName);
     }
     else if (args is TargetFinishedEventArgs targetFinishedArgs)
     {
         debugger.OnTargetLeave(targetFinishedArgs.TargetName);
     }
     else if (args is BuildMessageEventArgs)
     {
         //Console.WriteLine("Message: " + ((BuildMessageEventArgs)args).Message);
     }
     else if (args is TaskStartedEventArgs)
     {
         // Nothing to do
     }
     else if (args is TaskFinishedEventArgs)
     {
         // Nothing to do
     }
     else if (args is BuildStartedEventArgs buildStartedArgs)
     {
         OnBuildStarted(sender, buildStartedArgs);
     }
     else if (args is BuildFinishedEventArgs buildFinishedArgs)
     {
         OnBuildFinished(sender, buildFinishedArgs);
     }
     else if (args is ProjectEvaluationStartedEventArgs)
     {
         // Nothing to do
     }
     else if (args is ProjectEvaluationFinishedEventArgs)
     {
         // Nothing to do
     }
     else if (args is ProjectStartedEventArgs projectStartedArgs)
     {
         OnProjectStarted(sender, projectStartedArgs);
     }
     else if (args is ProjectFinishedEventArgs projectFinishedArgs)
     {
         OnProjectFinished(sender, projectFinishedArgs);
     }
     else
     {
         Console.WriteLine("Unknown event: " + args.GetType().Name);
     }
 }
Esempio n. 10
0
        internal static TargetBuiltReason GetBuildReasonFromTargetStarted(BuildEventArgs e)
        {
            Type         type     = e.GetType();
            PropertyInfo property = type.GetProperty("BuildReason");

            if (property == null)
            {
                return(TargetBuiltReason.None);
            }

            MethodInfo method = property.GetGetMethod();

            return((TargetBuiltReason)method.Invoke(e, null));
        }
Esempio n. 11
0
        /// <summary>
        /// Determine if the event is serializable. If we are running with multiple nodes we need to make sure the logging events are serializable. If not
        /// we need to log a warning.
        /// </summary>
        internal bool IsEventSerializable(BuildEventArgs e)
        {
#if FEATURE_BINARY_SERIALIZATION
            if (!e.GetType().GetTypeInfo().IsSerializable)
#else
            if (!NodePacketTranslator.IsSerializable(e))
#endif
            {
                _taskLoggingContext.LogWarning(null, new BuildEventFileInfo(string.Empty), "ExpectedEventToBeSerializable", e.GetType().Name);
                return(false);
            }

            return(true);
        }
        protected override void OnAnyMessage(object sender, BuildEventArgs e)
        {
            // data that's being shown in the UI
            BuildMessage message = new BuildMessage()
            {
                Type          = e.GetType().Name,
                Message       = e.Message,
                Context       = e.BuildEventContext,
                ParentContext = e is ProjectStartedEventArgs ? (e as ProjectStartedEventArgs).ParentProjectBuildEventContext : null,
                Timestamp     = e.Timestamp,
            };

            m_onBuildEvent(message);
        }
Esempio n. 13
0
        internal static TargetBuiltReason GetBuildReasonFromTargetSkipped(BuildEventArgs e)
        {
            if (buildReasonFromTargetSkipped == null)
            {
                Type         type     = e.GetType();
                PropertyInfo property = type.GetProperty("BuildReason");
                if (property == null)
                {
                    return(TargetBuiltReason.None);
                }

                MethodInfo method = property.GetGetMethod();
                buildReasonFromTargetSkipped = b => (TargetBuiltReason)method.Invoke(b, null);
            }

            return(buildReasonFromTargetSkipped(e));
        }
Esempio n. 14
0
        internal static TargetBuiltReason GetBuildReasonFromTargetSkipped(BuildEventArgs e)
        {
            if (BuildReasonFromTargetSkipped == null)
            {
                var type     = e.GetType().GetTypeInfo();
                var property = type.GetProperty("BuildReason");
                if (property == null)
                {
                    return(TargetBuiltReason.None);
                }

                var method = property.GetGetMethod();
                BuildReasonFromTargetSkipped = b => (TargetBuiltReason)method.Invoke(b, null);
            }

            return(BuildReasonFromTargetSkipped(e));
        }
Esempio n. 15
0
        private void EventSource_AnyEventRaised(object sender, BuildEventArgs e)
        {
            try
            {
                if (projectImportedEventArgsType != null && e.GetType() == projectImportedEventArgsType)
                {
                    string importedProjectFile = (string)this.importedProjectFile.GetValue(e);
                    //string unexpandedProject = (string)this.unexpandedProject.GetValue(e);
                    //var buildMessage = (BuildMessageEventArgs)e;
                    //ProjectImportedEventArgs args = new ProjectImportedEventArgs(buildMessage.LineNumber, buildMessage.ColumnNumber, buildMessage.Message);
                    //args.ImportedProjectFile = importedProjectFile;
                    //args.UnexpandedProject = unexpandedProject;
                    //args.BuildEventContext = buildMessage.BuildEventContext;
                    projectImportsCollector.AddFile(importedProjectFile);
                    return;
                }

                projectImportsCollector?.IncludeSourceFiles(e);
            }
            catch
            {
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Reads the logging packet from the translator.
        /// </summary>
        internal void ReadFromStream(ITranslator translator)
        {
            if (LoggingEventType.CustomEvent != _eventType)
            {
                _buildEvent = GetBuildEventArgFromId();

                // The other side is telling us whether the event knows how to log itself, or whether we're going to have
                // to do it manually
                int packetVersion = s_defaultPacketVersion;
                translator.Translate(ref packetVersion);

                bool eventCanSerializeItself = true;
                translator.Translate(ref eventCanSerializeItself);

                if (eventCanSerializeItself)
                {
                    MethodInfo methodInfo = null;
                    lock (s_readMethodCache)
                    {
                        if (!s_readMethodCache.TryGetValue(_eventType, out methodInfo))
                        {
                            Type eventDerivedType = _buildEvent.GetType();
                            methodInfo = eventDerivedType.GetMethod("CreateFromStream", BindingFlags.NonPublic | BindingFlags.Instance);
                            s_readMethodCache.Add(_eventType, methodInfo);
                        }
                    }

                    ArgsReaderDelegate readerMethod = (ArgsReaderDelegate)CreateDelegateRobust(typeof(ArgsReaderDelegate), _buildEvent, methodInfo);

                    readerMethod(translator.Reader, packetVersion);
                    if (_eventType == LoggingEventType.TargetFinishedEvent && _targetFinishedTranslator != null)
                    {
                        _targetFinishedTranslator(translator, (TargetFinishedEventArgs)_buildEvent);
                    }
                }
                else
                {
                    _buildEvent = ReadEventFromStream(_eventType, translator);
                }
            }
            else
            {
                string fileLocation = null;
                translator.Translate(ref fileLocation);

                bool resolveAssembly = false;
                lock (s_lockObject)
                {
                    if (!s_customEventsLoaded.Contains(fileLocation))
                    {
                        resolveAssembly = true;
                    }

                    // If we are to resolve the assembly add it to the list of assemblies resolved
                    if (resolveAssembly)
                    {
                        s_customEventsLoaded.Add(fileLocation);
                    }
                }

#if FEATURE_APPDOMAIN
                if (resolveAssembly)
                {
                    s_resolver = new TaskEngineAssemblyResolver();
                    s_resolver.InstallHandler();
                    s_resolver.Initialize(fileLocation);
                }
#endif

                try
                {
                    translator.TranslateDotNet(ref _buildEvent);
                }
                finally
                {
#if FEATURE_APPDOMAIN
                    if (resolveAssembly)
                    {
                        s_resolver.RemoveHandler();
                        s_resolver = null;
                    }
#endif
                }
            }

            _eventType = GetLoggingEventId(_buildEvent);
        }
Esempio n. 17
0
        /// <summary>
        /// Based on the type of the BuildEventArg to be wrapped
        /// generate an Id which identifies which concrete type the
        /// BuildEventArg is.
        /// </summary>
        /// <param name="eventArg">Argument to get the type Id for</param>
        /// <returns>An enumeration entry which represents the type</returns>
        private LoggingEventType GetLoggingEventId(BuildEventArgs eventArg)
        {
            Type eventType = eventArg.GetType();

            if (eventType == typeof(BuildMessageEventArgs))
            {
                return(LoggingEventType.BuildMessageEvent);
            }
            else if (eventType == typeof(TaskCommandLineEventArgs))
            {
                return(LoggingEventType.TaskCommandLineEvent);
            }
#if !TASKHOST
            else if (eventType == typeof(TaskParameterEventArgs))
            {
                return(LoggingEventType.TaskParameterEvent);
            }
#endif
            else if (eventType == typeof(ProjectFinishedEventArgs))
            {
                return(LoggingEventType.ProjectFinishedEvent);
            }
            else if (eventType == typeof(ProjectStartedEventArgs))
            {
                return(LoggingEventType.ProjectStartedEvent);
            }
            else if (eventType == typeof(TargetStartedEventArgs))
            {
                return(LoggingEventType.TargetStartedEvent);
            }
            else if (eventType == typeof(TargetFinishedEventArgs))
            {
                return(LoggingEventType.TargetFinishedEvent);
            }
            else if (eventType == typeof(TaskStartedEventArgs))
            {
                return(LoggingEventType.TaskStartedEvent);
            }
            else if (eventType == typeof(TaskFinishedEventArgs))
            {
                return(LoggingEventType.TaskFinishedEvent);
            }
            else if (eventType == typeof(BuildFinishedEventArgs))
            {
                return(LoggingEventType.BuildFinishedEvent);
            }
            else if (eventType == typeof(BuildStartedEventArgs))
            {
                return(LoggingEventType.BuildStartedEvent);
            }
            else if (eventType == typeof(BuildWarningEventArgs))
            {
                return(LoggingEventType.BuildWarningEvent);
            }
            else if (eventType == typeof(BuildErrorEventArgs))
            {
                return(LoggingEventType.BuildErrorEvent);
            }
            else
            {
                return(LoggingEventType.CustomEvent);
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Write a provided instance of BuildEventArgs to the BinaryWriter
        /// </summary>
        public void Write(BuildEventArgs e)
        {
            var type = e.GetType().Name;

            // the cases are ordered by most used first for performance
            if (e is BuildMessageEventArgs && type != "ProjectImportedEventArgs" && type != "TargetSkippedEventArgs")
            {
                Write((BuildMessageEventArgs)e);
            }
            else if (e is TaskStartedEventArgs)
            {
                Write((TaskStartedEventArgs)e);
            }
            else if (e is TaskFinishedEventArgs)
            {
                Write((TaskFinishedEventArgs)e);
            }
            else if (e is TargetStartedEventArgs)
            {
                Write((TargetStartedEventArgs)e);
            }
            else if (e is TargetFinishedEventArgs)
            {
                Write((TargetFinishedEventArgs)e);
            }
            else if (e is BuildErrorEventArgs)
            {
                Write((BuildErrorEventArgs)e);
            }
            else if (e is BuildWarningEventArgs)
            {
                Write((BuildWarningEventArgs)e);
            }
            else if (e is ProjectStartedEventArgs)
            {
                Write((ProjectStartedEventArgs)e);
            }
            else if (e is ProjectFinishedEventArgs)
            {
                Write((ProjectFinishedEventArgs)e);
            }
            else if (e is BuildStartedEventArgs)
            {
                Write((BuildStartedEventArgs)e);
            }
            else if (e is BuildFinishedEventArgs)
            {
                Write((BuildFinishedEventArgs)e);
            }
            else if (e is ProjectEvaluationStartedEventArgs)
            {
                Write((ProjectEvaluationStartedEventArgs)e);
            }
            else if (e is ProjectEvaluationFinishedEventArgs)
            {
                Write((ProjectEvaluationFinishedEventArgs)e);
            }
            // The following cases are due to the fact that StructuredLogger.dll
            // only references MSBuild 14.0 .dlls. The following BuildEventArgs types
            // were only introduced in MSBuild 15.3 so we can't refer to them statically.
            // To still provide a good experience to those who are using the BinaryLogger
            // from StructuredLogger.dll against MSBuild 15.3 or later we need to preserve
            // these new events, so use reflection to create our "equivalents" of those
            // and populate them to be binary identical to the originals. Then serialize
            // our copies so that it's impossible to tell what wrote these.
            else if (type == "ProjectEvaluationStartedEventArgs")
            {
                var evaluationStarted = new ProjectEvaluationStartedEventArgs(e.Message);
                evaluationStarted.BuildEventContext = e.BuildEventContext;
                evaluationStarted.ProjectFile       = Reflector.GetProjectFileFromEvaluationStarted(e);
                Write(evaluationStarted);
            }
            else if (type == "ProjectEvaluationFinishedEventArgs")
            {
                var evaluationFinished = new ProjectEvaluationFinishedEventArgs(e.Message);
                evaluationFinished.BuildEventContext = e.BuildEventContext;
                evaluationFinished.ProjectFile       = Reflector.GetProjectFileFromEvaluationFinished(e);
                Write(evaluationFinished);
            }
            else if (type == "ProjectImportedEventArgs")
            {
                var message         = e as BuildMessageEventArgs;
                var projectImported = new ProjectImportedEventArgs(message.LineNumber, message.ColumnNumber, e.Message);
                projectImported.BuildEventContext   = e.BuildEventContext;
                projectImported.ProjectFile         = message.ProjectFile;
                projectImported.ImportedProjectFile = Reflector.GetImportedProjectFile(e);
                projectImported.UnexpandedProject   = Reflector.GetUnexpandedProject(e);
                Write(projectImported);
            }
            else if (type == "TargetSkippedEventArgs")
            {
                var message       = e as BuildMessageEventArgs;
                var targetSkipped = new TargetSkippedEventArgs(e.Message);
                targetSkipped.BuildEventContext = e.BuildEventContext;
                targetSkipped.ProjectFile       = message.ProjectFile;
                targetSkipped.TargetName        = Reflector.GetTargetNameFromTargetSkipped(e);
                targetSkipped.TargetFile        = Reflector.GetTargetFileFromTargetSkipped(e);
                targetSkipped.ParentTarget      = Reflector.GetParentTargetFromTargetSkipped(e);
                targetSkipped.BuildReason       = Reflector.GetBuildReasonFromTargetSkipped(e);
                Write(targetSkipped);
            }
            else
            {
                // convert all unrecognized objects to message
                // and just preserve the message
                var buildMessageEventArgs = new BuildMessageEventArgs(
                    e.Message,
                    e.HelpKeyword,
                    e.SenderName,
                    MessageImportance.Normal,
                    e.Timestamp);
                buildMessageEventArgs.BuildEventContext = e.BuildEventContext ?? BuildEventContext.Invalid;
                Write(buildMessageEventArgs);
            }
        }
Esempio n. 19
0
        /// <summary>
        /// Handles the event source event of any type
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="BuildEventArgs"/> instance containing the event data.</param>
        private void HandleEventSourceEvent(object sender, BuildEventArgs e)
        {
#if DEBUG
            // log all events
            this.diagnostics.WriteMessage($"{e.SenderName}: {e.GetType().Name} {e.Message}");
#endif

            try
            {
                switch (this.Verbosity)
                {
                case LoggerVerbosity.Quiet:
                {
                    if (e is BuildWarningEventArgs || e is BuildErrorEventArgs)
                    {
                        break;
                    }

                    return;
                }

                case LoggerVerbosity.Minimal:
                {
                    if (e is BuildWarningEventArgs || e is BuildErrorEventArgs)
                    {
                        break;
                    }

                    var buildMessage = e as BuildMessageEventArgs;
                    if (buildMessage?.Importance == MessageImportance.High)
                    {
                        break;
                    }

                    return;
                }

                case LoggerVerbosity.Normal:
                {
                    if (e is BuildWarningEventArgs || e is BuildErrorEventArgs)
                    {
                        break;
                    }

                    var buildMessage = e as BuildMessageEventArgs;
                    if (buildMessage?.Importance == MessageImportance.High || buildMessage?.Importance == MessageImportance.Normal)
                    {
                        break;
                    }

                    return;
                }

                case LoggerVerbosity.Detailed:
                {
                    if (e is BuildWarningEventArgs || e is BuildErrorEventArgs || e is BuildMessageEventArgs)
                    {
                        break;
                    }

                    return;
                }
                }

                // write JSON element line break after previous element
                this.AddJsonArrayLineBreak();

                // write build event
                var rootObject = new JObject();
                rootObject.Add(e.GetType().Name, JObject.FromObject(e));
                this.serializer.Serialize(this.jsonWriter, rootObject);
            }
            catch (Exception ex)
            {
                this.diagnostics.WriteException(ex);
            }
        }