Exemple #1
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);
            }
        }