Ejemplo n.º 1
0
        public static void TraceEvent(
            this ITrace trace,
            bool convertNamedFormatTokens,
            TraceEventType traceEventType,
            int eventId,
            Exception exception,
            string message,
            params object[] args)
        {
            if (trace == null)
            {
                throw new ArgumentNullException(nameof(trace));
            }
            if (!trace.ShouldTrace(traceEventType))
            {
                return;
            }
            StringBuilder sb
                = TraceMessageHelper.FormatTraceMessage(
                      convertNamedFormatTokens,
                      exception,
                      message,
                      args);

            if (sb.Length != 0)
            {
                trace.TraceEvent(traceEventType, eventId, sb.ToString());
            }
        }
Ejemplo n.º 2
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));
        }
Ejemplo n.º 3
0
 public override string ToString()
 => TraceMessageHelper.FormatTraceMessage(Prefix, ConvertNamedFormatTokens, Exception, Message, FormatArgs)
 .ToString();
Ejemplo n.º 4
0
 public static string FormatNamedTokens(string format, params object[] values)
 => string.Format(
     TraceMessageHelper.ConvertNamedTokens(format, out _)
     .ToString(),
     values);
Ejemplo n.º 5
0
 public static StringBuilder FormatTraceMessage(
     bool convertNamedFormatTokens,
     Exception exception,
     string message,
     params object[] args)
 => TraceMessageHelper.FormatTraceMessage(null, convertNamedFormatTokens, exception, message, args);