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()); } }
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)); }
public override string ToString() => TraceMessageHelper.FormatTraceMessage(Prefix, ConvertNamedFormatTokens, Exception, Message, FormatArgs) .ToString();
public static string FormatNamedTokens(string format, params object[] values) => string.Format( TraceMessageHelper.ConvertNamedTokens(format, out _) .ToString(), values);
public static StringBuilder FormatTraceMessage( bool convertNamedFormatTokens, Exception exception, string message, params object[] args) => TraceMessageHelper.FormatTraceMessage(null, convertNamedFormatTokens, exception, message, args);