/// <summary>
        /// Gets a text serialized value of a parameter for logging.
        /// </summary>
        internal static string GetParameterText(string prefix, string parameterName, IList parameterValue)
        {
            if (parameterValue == null || parameterValue.Count == 0)
            {
                return parameterName;
            }

            using (var sb = new ReuseableStringBuilder())
            {
                sb.Append(prefix);

                bool firstEntryIsTaskItemWithSomeCustomMetadata = false;
                var firstItem = parameterValue[0] as ITaskItem;
                if (firstItem != null)
                {
                    if (firstItem.CloneCustomMetadata().Count > 0)
                    {
                        firstEntryIsTaskItemWithSomeCustomMetadata = true;
                    }
                }

                // If it's just one entry in the list, and it's not a task item with metadata, keep it on one line like a scalar
                bool specialTreatmentForSingle = (parameterValue.Count == 1 && !firstEntryIsTaskItemWithSomeCustomMetadata);

                if (!specialTreatmentForSingle)
                {
                    sb.Append("\n    ");
                }

                sb.Append(parameterName + "=");

                if (!specialTreatmentForSingle)
                {
                    sb.Append("\n");
                }

                for (int i = 0; i < parameterValue.Count; i++)
                {
                    if (parameterValue[i] == null)
                    {
                        continue;
                    }

                    if (!specialTreatmentForSingle)
                    {
                        sb.Append("        ");
                    }

                    sb.Append(GetStringFromParameterValue(parameterValue[i]));

                    if (!specialTreatmentForSingle && i < parameterValue.Count - 1)
                    {
                        sb.Append("\n");
                    }
                }

                return sb.ToString();
            }
        }
Example #2
0
        internal static string FormatEventMessage
        (
            string category,
            string subcategory,
            string message,
            string code,
            string file,
            string projectFile,
            int lineNumber,
            int endLineNumber,
            int columnNumber,
            int endColumnNumber,
            int threadId,
            string logOutputProperties
        )
        {
            // capacity is the longest possible path through the below
            // to avoid reallocating while constructing the string
            using ReuseableStringBuilder format = new(51);

            // Uncomment these lines to show show the processor, if present.

            /*
             * if (threadId != 0)
             * {
             *  format.Append("{0}>");
             * }
             */

            if (string.IsNullOrEmpty(file))
            {
                format.Append("MSBUILD : ");    // Should not be localized.
            }
            else
            {
                format.Append("{1}");

                if (lineNumber == 0)
                {
                    format.Append(" : ");
                }
                else
                {
                    if (columnNumber == 0)
                    {
                        if (endLineNumber == 0)
                        {
                            format.Append("({2}): ");
                        }
                        else
                        {
                            format.Append("({2}-{7}): ");
                        }
                    }
                    else
                    {
                        if (endLineNumber == 0)
                        {
                            if (endColumnNumber == 0)
                            {
                                format.Append("({2},{3}): ");
                            }
                            else
                            {
                                format.Append("({2},{3}-{8}): ");
                            }
                        }
                        else
                        {
                            if (endColumnNumber == 0)
                            {
                                format.Append("({2}-{7},{3}): ");
                            }
                            else
                            {
                                format.Append("({2},{3},{7},{8}): ");
                            }
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(subcategory))
            {
                format.Append("{9} ");
            }

            // The category as a string (should not be localized)
            format.Append("{4} ");

            // Put a code in, if available and necessary.
            if (code == null)
            {
                format.Append(": ");
            }
            else
            {
                format.Append("{5}: ");
            }

            // Put the message in, if available.
            if (message != null)
            {
                format.Append("{6}");
            }

            // If the project file was specified, tack that onto the very end.
            // Check for additional properties that should be output with project file
            if (projectFile != null)
            {
                // If the project file was specified, tack that onto the very end.
                if (!string.Equals(projectFile, file))
                {
                    // Check for additional properties that should be output with project file
                    if (logOutputProperties?.Length > 0)
                    {
                        format.Append(" [{10}::{11}]");
                    }
                    else
                    {
                        format.Append(" [{10}]");
                    }
                }
                else
                {
                    // If the file location of the error _was_ the project file, append only the
                    // additional output properties

                    if (logOutputProperties?.Length > 0)
                    {
                        format.Append(" [{11}]");
                    }
                }
            }

            // A null message is allowed and is to be treated as a blank line.
            if (message == null)
            {
                message = String.Empty;
            }

            string finalFormat = format.ToString();

            // Reuse the string builder to create the final message
            ReuseableStringBuilder formattedMessage = format.Clear();

            // If there are multiple lines, show each line as a separate message.
            string[] lines = SplitStringOnNewLines(message);

            for (int i = 0; i < lines.Length; i++)
            {
                formattedMessage.AppendFormat(
                    CultureInfo.CurrentCulture, finalFormat,
                    threadId, file,
                    lineNumber, columnNumber, category, code,
                    lines[i], endLineNumber, endColumnNumber,
                    subcategory, projectFile, logOutputProperties);

                if (i < (lines.Length - 1))
                {
                    formattedMessage.AppendLine();
                }
            }

            return(formattedMessage.ToString());
        }