/// <summary>
        /// Handles a generic BuildMessage event and assigns it to the appropriate logging node.
        /// </summary>
        /// <param name="args">The <see cref="BuildMessageEventArgs"/> instance containing the event data.</param>
        public void AddMessage(LazyFormattedBuildEventArgs args, string message)
        {
            message = Intern(message);

            TreeNode node        = null;
            var      messageNode = new Message
            {
                Text      = message,
                Timestamp = args.Timestamp
            };
            BaseNode nodeToAdd = messageNode;

            var buildEventContext = args.BuildEventContext;

            if (buildEventContext?.TaskId > 0)
            {
                node = GetTask(args);
                if (node is Task task)
                {
                    if (task.Name == "ResolveAssemblyReference")
                    {
                        Folder inputs  = task.FindChild <Folder>(Strings.Inputs);
                        Folder results = task.FindChild <Folder>(Strings.Results);
                        node = results ?? inputs;

                        if (message.StartsWith("    ", StringComparison.Ordinal))
                        {
                            message = message.Substring(4);

                            var parameter = node?.FindLastChild <Parameter>();
                            if (parameter != null)
                            {
                                bool thereWasAConflict = Strings.IsThereWasAConflictPrefix(parameter.ToString()); //parameter.ToString().StartsWith(Strings.ThereWasAConflictPrefix);
                                if (thereWasAConflict)
                                {
                                    HandleThereWasAConflict(parameter, message, stringTable);
                                    return;
                                }

                                if (!string.IsNullOrWhiteSpace(message))
                                {
                                    node = parameter;

                                    if (message.StartsWith("    ", StringComparison.Ordinal))
                                    {
                                        message = message.Substring(4);

                                        var lastItem = parameter.FindLastChild <Item>();

                                        // only indent if it's not a "For SearchPath..." message - that one needs to be directly under parameter
                                        // also don't indent if it's under AssemblyFoldersEx in Results
                                        if (lastItem != null &&
                                            !Strings.ForSearchPathPrefix.IsMatch(message) &&
                                            !parameter.Name.StartsWith("AssemblyFoldersEx", StringComparison.Ordinal))
                                        {
                                            node = lastItem;
                                        }
                                    }

                                    if (!string.IsNullOrEmpty(message))
                                    {
                                        var equals = message.IndexOf('=');
                                        if (equals != -1)
                                        {
                                            var kvp = TextUtilities.ParseNameValue(message);
                                            node.AddChild(new Metadata
                                            {
                                                Name  = Intern(kvp.Key.TrimEnd(space)),
                                                Value = Intern(kvp.Value.TrimStart(space))
                                            });
                                        }
                                        else
                                        {
                                            node.AddChild(new Item()
                                            {
                                                Text = Intern(message)
                                            });
                                        }
                                    }
                                }

                                return;
                            }
                        }
                        else
                        {
                            if (results == null)
                            {
                                bool isResult = Strings.UnifiedPrimaryReferencePrefix.IsMatch(message) ||
                                                Strings.PrimaryReferencePrefix.IsMatch(message) ||
                                                Strings.DependencyPrefix.IsMatch(message) ||
                                                Strings.UnifiedDependencyPrefix.IsMatch(message) ||
                                                Strings.AssemblyFoldersExLocation.IsMatch(message) ||
                                                Strings.IsThereWasAConflictPrefix(message);

                                if (isResult)
                                {
                                    results = task.GetOrCreateNodeWithName <Folder>(Strings.Results);
                                    node    = results;
                                }
                                else
                                {
                                    if (inputs == null)
                                    {
                                        inputs = task.GetOrCreateNodeWithName <Folder>(Strings.Inputs);
                                    }

                                    node = inputs;
                                }
                            }
                            else
                            {
                                node = results;
                            }

                            node.GetOrCreateNodeWithName <Parameter>(Intern(message.TrimEnd(':')));
                            return;
                        }
                    }
                    else if (string.Equals(task.Name, "MSBuild", StringComparison.OrdinalIgnoreCase))
                    {
                        var additionalPropertiesMatch = Strings.AdditionalPropertiesPrefix.Match(message);
                        if (message.StartsWith(Strings.GlobalPropertiesPrefix, StringComparison.Ordinal) ||
                            additionalPropertiesMatch.Success ||
                            Strings.OverridingGlobalPropertiesPrefix.IsMatch(message) ||
                            message.StartsWith(Strings.RemovingPropertiesPrefix, StringComparison.Ordinal) ||
                            Strings.RemovingProjectProperties.IsMatch(message))
                        {
                            if (additionalPropertiesMatch.Success)
                            {
                                node = node.GetOrCreateNodeWithName <Folder>(Strings.AdditionalProperties);
                            }

                            node.GetOrCreateNodeWithName <Folder>(message);
                            return;
                        }

                        node = node.FindLastChild <Folder>() ?? node;
                        if (message.Length > 2 && message[0] == ' ' && message[1] == ' ')
                        {
                            if (node is Folder f && f.Name == Strings.AdditionalProperties)
                            {
                                node = f.FindLastChild <Folder>() ?? node;
                            }

                            message = message.Substring(2);
                        }

                        var kvp = TextUtilities.ParseNameValue(message);
                        if (kvp.Value == "")
                        {
                            nodeToAdd = new Item
                            {
                                Text = Intern(kvp.Key)
                            };
                        }
                        else
                        {
                            nodeToAdd = new Property
                            {
                                Name  = Intern(kvp.Key),
                                Value = Intern(kvp.Value)
                            };
                        }
                    }
                    else if (string.Equals(task.Name, "RestoreTask"))
                    {
                        // just throw these away to save space
                        // https://github.com/NuGet/Home/issues/10383
                        if (message.StartsWith(Strings.RestoreTask_CheckingCompatibilityFor, StringComparison.Ordinal))
                        {
                            return;
                        }
                        else if (message.StartsWith("  GET", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("GET");
                        }
                        else if (message.StartsWith("  CACHE", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("CACHE");
                        }
                        else if (message.StartsWith("  OK", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("OK");
                        }
                        else if (message.StartsWith("  NotFound", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("NotFound");
                        }
                        else if (message.StartsWith("PackageSignatureVerificationLog:", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("PackageSignatureVerificationLog");
                        }
                        else if (message.StartsWith("Writing assets file to disk", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("Assets file");
                        }
                        else if (message.StartsWith("Writing cache file to disk", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("Cache file");
                        }
                        else if (message.StartsWith("Persisting dg to", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("dg file");
                        }
                        else if (message.StartsWith("Generating MSBuild file", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("MSBuild file");
                        }
                        else if (message.StartsWith("Lock not required", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("Lock not required");
                        }
                        else if (message.StartsWith("Installing", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("Installing");
                        }
                        else if (message.StartsWith("Restoring packages for", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("Restoring packages for");
                        }
                        else if (message.StartsWith("Reading project file", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("Reading project file");
                        }
                        else if (message.StartsWith("Scanning packages for", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("Scanning packages for");
                        }
                        else if (message.StartsWith("Merging in runtimes", StringComparison.Ordinal))
                        {
                            node = node.GetOrCreateNodeWithName <Folder>("Merging in runtimes");
                        }
                        else if (
                            message.StartsWith(Strings.RestoreTask_CheckingCompatibilityFor, StringComparison.Ordinal) ||
                            message.StartsWith(Strings.RestoreTask_CheckingCompatibilityOfPackages, StringComparison.Ordinal) ||
                            message.StartsWith(Strings.RestoreTask_AcquiringLockForTheInstallation, StringComparison.Ordinal) ||
                            message.StartsWith(Strings.RestoreTask_AcquiredLockForTheInstallation, StringComparison.Ordinal) ||
                            message.StartsWith(Strings.RestoreTask_CompletedInstallationOf, StringComparison.Ordinal) ||
                            message.StartsWith(Strings.RestoreTask_ResolvingConflictsFor, StringComparison.Ordinal) ||
                            message.StartsWith(Strings.RestoreTask_AllPackagesAndProjectsAreCompatible, StringComparison.Ordinal) ||
                            message.StartsWith(Strings.RestoreTask_Committing, StringComparison.Ordinal)
                            )
                        {
                            return;
                        }
                    }
                }
            }
            else if (buildEventContext?.TargetId > 0)
            {
                node = GetTarget(args);

                if (Strings.TaskSkippedFalseCondition.Match(message).Success)
                {
                    messageNode.IsLowRelevance = true;
                }
            }
            else if (buildEventContext?.ProjectContextId > 0)
            {
                var project = construction.GetOrAddProject(buildEventContext.ProjectContextId);
                node = project;

                if (Strings.IsTargetSkipped(message))
                {
                    var targetName = Intern(TextUtilities.ParseQuotedSubstring(message));
                    if (targetName != null)
                    {
                        var skippedTarget = project.GetOrAddTargetByName(targetName, args.Timestamp);
                        skippedTarget.StartTime = args.Timestamp;
                        skippedTarget.EndTime   = args.Timestamp;
                        node = skippedTarget;
                        messageNode.IsLowRelevance = true;
                    }
                }
            }
            else if (buildEventContext.EvaluationId != -1)
            {
                node = construction.EvaluationFolder;

                var evaluationId = buildEventContext.EvaluationId;
                var evaluation   = construction.Build.FindEvaluation(evaluationId);
                if (evaluation != null)
                {
                    node = evaluation;
                }

                if (Strings.PropertyReassignmentRegex.IsMatch(message))
                {
                    TimedNode properties;
                    if (evaluation != null)
                    {
                        properties = evaluation.PropertyReassignmentFolder;
                    }
                    else
                    {
                        properties = node.GetOrCreateNodeWithName <TimedNode>(Strings.PropertyReassignmentFolder, addAtBeginning: true);
                    }

                    var propertyName = Strings.GetPropertyName(message);
                    node = properties.GetOrCreateNodeWithName <Folder>(propertyName);
                }

                if (node != null && node.FindChild <Message>(message) != null)
                {
                    // avoid duplicate messages
                    return;
                }
            }

            if (node == null)
            {
                node = construction.Build;

                if (Strings.IsEvaluationMessage(message))
                {
                    if (!evaluationMessagesAlreadySeen.Add(message))
                    {
                        return;
                    }

                    node = construction.EvaluationFolder;
                }
                else if (Strings.PropertyReassignmentRegex.IsMatch(message))
                {
                    if (!evaluationMessagesAlreadySeen.Add(message))
                    {
                        return;
                    }

                    var properties = construction.EvaluationFolder.GetOrCreateNodeWithName <Folder>(Strings.PropertyReassignmentFolder);
                    node = properties.GetOrCreateNodeWithName <Folder>(Strings.GetPropertyName(message));
                }
                else if (Strings.IsTargetDoesNotExistAndWillBeSkipped(message))
                {
                    var folder = construction.EvaluationFolder;
                    node = folder;
                    messageNode.IsLowRelevance = true;
                }
                else if (buildEventContext != null && (buildEventContext.NodeId == 0 &&
                                                       buildEventContext.ProjectContextId == 0 &&
                                                       buildEventContext.ProjectInstanceId == 0 &&
                                                       buildEventContext.TargetId == 0 &&
                                                       buildEventContext.TaskId == 0))
                {
                    // must be Detailed Build Summary
                    // https://github.com/Microsoft/msbuild/blob/master/src/XMakeBuildEngine/BackEnd/Components/Scheduler/Scheduler.cs#L509
                    DetailedSummary.AppendLine(message);
                    return;
                }
            }

            node.AddChild(nodeToAdd);
        }
        public void Process(BuildMessageEventArgs args)
        {
            if (args == null)
            {
                return;
            }

            if (args is TaskParameterEventArgs taskParameter)
            {
                ProcessTaskParameter(taskParameter);
                return;
            }
            else if (args is ProjectImportedEventArgs projectImported)
            {
                ProcessProjectImported(projectImported);
                return;
            }

            var message = args.Message;

            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            var buildEventContext = args.BuildEventContext;

            if (buildEventContext.TaskId != BuildEventContext.InvalidTaskId)
            {
                if (message.StartsWith(Strings.OutputItemsMessagePrefix, StringComparison.Ordinal))
                {
                    var task = GetTask(args);

                    //this.construction.Build.Statistics.ReportOutputItemMessage(task, message);

                    var folder    = task.GetOrCreateNodeWithName <Folder>(Strings.OutputItems);
                    var parameter = ItemGroupParser.ParsePropertyOrItemList(message, Strings.OutputItemsMessagePrefix, stringTable);
                    folder.AddChild(parameter);
                    return;
                }

                if (message.StartsWith(Strings.OutputPropertyMessagePrefix, StringComparison.Ordinal))
                {
                    var task      = GetTask(args);
                    var folder    = task.GetOrCreateNodeWithName <Folder>(Strings.OutputProperties);
                    var parameter = ItemGroupParser.ParsePropertyOrItemList(message, Strings.OutputPropertyMessagePrefix, stringTable);
                    folder.AddChild(parameter);
                    return;
                }

                if (message.StartsWith(Strings.TaskParameterMessagePrefix, StringComparison.Ordinal))
                {
                    var task = GetTask(args);
                    if (IgnoreParameters(task))
                    {
                        return;
                    }

                    //this.construction.Build.Statistics.ReportTaskParameterMessage(task, message);

                    var folder    = task.GetOrCreateNodeWithName <Folder>(Strings.Parameters);
                    var parameter = ItemGroupParser.ParsePropertyOrItemList(message, Strings.TaskParameterMessagePrefix, stringTable);
                    folder.AddChild(parameter);
                    return;
                }

                if (args is TaskCommandLineEventArgs taskArgs)
                {
                    if (AddCommandLine(taskArgs))
                    {
                        return;
                    }
                }
            }
            else if (buildEventContext.TargetId != BuildEventContext.InvalidTargetId)
            {
                if (message.StartsWith(Strings.ItemGroupIncludeMessagePrefix, StringComparison.Ordinal))
                {
                    AddItemGroup(args, Strings.ItemGroupIncludeMessagePrefix, new AddItem());
                    return;
                }

                if (message.StartsWith(Strings.ItemGroupRemoveMessagePrefix, StringComparison.Ordinal))
                {
                    AddItemGroup(args, Strings.ItemGroupRemoveMessagePrefix, new RemoveItem());
                    return;
                }

                if (message.StartsWith(Strings.PropertyGroupMessagePrefix, StringComparison.Ordinal))
                {
                    AddPropertyGroup(args, Strings.PropertyGroupMessagePrefix);
                    return;
                }

                // A task from assembly message (parses out the task name and assembly path).
                var match = Strings.UsingTask(message);
                if (match.Success)
                {
                    construction.SetTaskAssembly(
                        Intern(match.Groups["task"].Value),
                        Intern(match.Groups["assembly"].Value));
                    return;
                }
            }
            else if (buildEventContext.EvaluationId != BuildEventContext.InvalidEvaluationId)
            {
            }
            else
            {
                if (args.SenderName == "BinaryLogger")
                {
                    var parameter = ItemGroupParser.ParsePropertyOrItemList(message, string.Empty, stringTable);
                    construction.Build.AddChild(parameter);
                    return;
                }
            }

            // Just the generic log message or something we currently don't handle in the object model.
            AddMessage(args, message);
        }