Exemple #1
0
        public static StringBuilder FormatTraceMessage(
            string prefix,
            bool convertNamedFormatTokens,
            Exception exception,
            string message,
            params object[] args)
        {
            StringBuilder sb = new StringBuilder();

            if (prefix != null)
            {
                sb.Append(prefix);
            }
            if ((args != null) &&
                (args.Length != 0))
            {
                if (!string.IsNullOrWhiteSpace(message))
                {
                    sb.AppendFormat(
                        convertNamedFormatTokens
                                                                        ? TraceMessageHelper.ConvertNamedTokens(message, out _)
                        .ToString()
                                                                        : message,
                        args);
                }
                else
                {
                    foreach (object formatArg in args)
                    {
                        if (formatArg == null)
                        {
                            continue;
                        }
                        if (sb.Length != 0)
                        {
                            sb.Append(", ");
                        }
                        sb.Append(formatArg);
                    }
                }
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(message))
                {
                    sb.Append(message);
                }
            }
            if (exception == null)
            {
                return(sb);
            }
            if (sb.Length != 0)
            {
                sb.AppendLine();
            }
            return(sb.Append(exception));
        }
Exemple #2
0
 public static string FormatNamedTokens(string format, params object[] values)
 => string.Format(
     TraceMessageHelper.ConvertNamedTokens(format, out _)
     .ToString(),
     values);