/// <summary> /// Formats log event for text representation, not including any properties. Error is included though. /// </summary> public static string Format(LogEvent e, FormattedString format = null) { if (format == null) format = DefaultFormat; var b = new StringBuilder(); foreach(Token token in format.Tokens) { switch(token.Type) { case TokenType.String: b.Append(token.Value); break; case TokenType.Parameter: switch(token.Name) { case Time: b.Append(e.EventTime.ToString(token.Format)); break; case Severity: string sev = e.Severity.ToString().ToUpper(); if (token.Format != null) sev = string.Format(token.NativeFormat, sev); b.Append(sev); break; case Source: b.Append(e.SourceName); break; case Message: b.Append(e.FormattedMessage); break; case Error: if (e.ErrorException != null) { b.AppendLine(); b.Append(e.ErrorException.ToString()); } break; case NewLine: b.AppendLine(); break; default: if(e.Properties != null) { object value; if(e.Properties.TryGetValue(token.Name, out value)) { string custom = format.Format(token, value); b.Append(custom); } } break; } break; } } return b.ToString(); }
/// <summary> /// Formats log event for text representation, not including any properties. Error is included though. /// </summary> public static string Format(LogEvent e, FormattedString format, bool includeProperties) { if (format == null) { format = DefaultFormat; } var b = new StringBuilder(); foreach (Token token in format.Tokens) { switch (token.Type) { case TokenType.String: b.Append(token.Value); break; case TokenType.Parameter: switch (token.Name) { case Time: b.Append(e.EventTime.ToString(token.Format)); break; case Severity: string sev = ToSeverityString(e); if (token.Format != null) { sev = string.Format(token.NativeFormat, sev); } b.Append(sev); break; case Source: b.Append(e.SourceName); break; case Message: b.Append(e.FormattedMessage); break; case Error: if (e.ErrorException != null) { b.AppendLine(); b.Append(e.ErrorException.ToString()); } break; case NewLine: b.AppendLine(); break; default: if (e.Properties != null) { object value; if (e.Properties.TryGetValue(token.Name, out value)) { string custom = format.Format(token, value); b.Append(custom); } } break; } break; } } if (includeProperties && e.Properties?.Count > 0) { int longestPropertyName = e.Properties.Max(p => p.Key.Length); IEnumerable <string> lines = e.Properties .Where(p => !TextFormatter.DoNotPrint(p.Key)) .Select(p => $" {p.Key.PadLeft(longestPropertyName)}: {p.Value}"); foreach (string line in lines) { b.Append(Environment.NewLine); b.Append(line); } } return(b.ToString()); }
/// <summary> /// Formats log event for text representation, not including any properties. Error is included though. /// </summary> public static string Format(LogEvent e, FormattedString format = null) { if (format == null) { format = DefaultFormat; } var b = new StringBuilder(); foreach (Token token in format.Tokens) { switch (token.Type) { case TokenType.String: b.Append(token.Value); break; case TokenType.Parameter: switch (token.Name) { case Time: b.Append(e.EventTime.ToString(token.Format)); break; case Severity: string sev = ToSeverityString(e); if (token.Format != null) { sev = string.Format(token.NativeFormat, sev); } b.Append(sev); break; case Source: b.Append(e.SourceName); break; case Message: b.Append(e.FormattedMessage); break; case Error: if (e.ErrorException != null) { b.AppendLine(); b.Append(e.ErrorException.ToString()); } break; case NewLine: b.AppendLine(); break; default: if (e.Properties != null) { object value; if (e.Properties.TryGetValue(token.Name, out value)) { string custom = format.Format(token, value); b.Append(custom); } } break; } break; } } return(b.ToString()); }