/// <inheritdoc cref="IPropertyRenderer"/> public void Render(ExtendedLogEvent logEvent, TextWriter output) { if (!logEvent.LogEvent.Properties.TryGetValue(this.propertyToken.PropertyName, out var propertyValue)) { output.Write(this.propertyToken.ToString()); return; } propertyValue.Render(output, this.propertyToken.Format); }
/// <summary> /// Formats the log event. /// </summary> /// <param name="logEvent">The log event.</param> /// <returns>The formatted log event as <see cref="string"/>.</returns> public string Format(ExtendedLogEvent logEvent) { using var writer = new StringWriter(new StringBuilder(DefaultWriteCapacity)); foreach (Action <ExtendedLogEvent, TextWriter> renderAction in this.renderActions) { renderAction(logEvent, writer); } return(writer.ToString()); }
/// <summary> /// Renders the message. /// </summary> /// <param name="extLogEvent">The log event.</param> /// <param name="options">The options.</param> /// <returns>The rendered message.</returns> private static string RenderMessage(ExtendedLogEvent extLogEvent, TelegramSinkOptions options) { var sb = new StringBuilder(); var emoji = LogLevelRenderer.GetEmoji(extLogEvent.LogEvent); var renderedMessage = HtmlEscaper.Escape(options, extLogEvent.LogEvent.RenderMessage()); sb.AppendLine($"{emoji} {renderedMessage}"); sb.AppendLine(string.Empty); if (!string.IsNullOrWhiteSpace(options.ApplicationName) || !string.IsNullOrWhiteSpace(options.DateFormat)) { string applicationNamePart = string.IsNullOrWhiteSpace(options.ApplicationName) ? string.Empty : $"{HtmlEscaper.Escape(options, options.ApplicationName)}: "; string datePart = string.IsNullOrWhiteSpace(options.DateFormat) ? string.Empty : extLogEvent.FirstOccurrence != extLogEvent.LastOccurrence ? $"The message occurred first on {extLogEvent.FirstOccurrence.ToString(options.DateFormat)} and last on {extLogEvent.LastOccurrence.ToString(options.DateFormat)}" : $"The message occurred on {extLogEvent.FirstOccurrence.ToString(options.DateFormat)}"; sb.AppendLine($"<i>{applicationNamePart}{datePart}</i>"); } if (extLogEvent.LogEvent.Exception is null) { return sb.ToString(); } var message = HtmlEscaper.Escape(options, extLogEvent.LogEvent.Exception.Message); var exceptionType = HtmlEscaper.Escape(options, extLogEvent.LogEvent.Exception.GetType().Name); sb.AppendLine($"\n<strong>{message}</strong>\n"); sb.AppendLine($"Message: <code>{message}</code>"); sb.AppendLine($"Type: <code>{exceptionType}</code>\n"); if (extLogEvent.IncludeStackTrace) { var exception = HtmlEscaper.Escape(options, $"{extLogEvent.LogEvent.Exception}"); sb.AppendLine($"Stack Trace\n<code>{exception}</code>"); } return sb.ToString(); }
/// <inheritdoc cref="IPropertyRenderer"/> public void Render(ExtendedLogEvent logEvent, TextWriter output) { using var writer = new StringWriter(new StringBuilder()); foreach (MessageTemplateToken token in logEvent.LogEvent.MessageTemplate.Tokens) { switch (token) { case TextToken textToken: TextTokenRenderer.Render(textToken, output, options); break; case PropertyToken propertyToken: new DefaultPropertyRenderer(propertyToken).Render(logEvent, writer); break; } } output.Write(HtmlEscaper.Escape(options, writer.ToString())); }
/// <inheritdoc cref="IPropertyRenderer"/> public void Render(ExtendedLogEvent extLogEvent, TextWriter output) { if (extLogEvent.LogEvent.Exception is null) { return; } var message = HtmlEscaper.Escape(this.options, extLogEvent.LogEvent.Exception.Message); var exceptionType = HtmlEscaper.Escape(this.options, extLogEvent.LogEvent.Exception.GetType().Name); output.WriteLine($"\n<strong>{message}</strong>\n"); output.WriteLine($"Message: <code>{message}</code>"); output.WriteLine($"Type: <code>{exceptionType}</code>\n"); if (extLogEvent.IncludeStackTrace) { var exception = HtmlEscaper.Escape(this.options, $"{extLogEvent.LogEvent.Exception}"); output.WriteLine($"Stack Trace\n<code>{exception}</code>"); } }
/// <inheritdoc cref="IPropertyRenderer"/> public void Render(ExtendedLogEvent logEvent, TextWriter output) { string stringLevel = logEvent.LogEvent.Level.ToString(); switch (this.propertyToken.Format) { case "e": stringLevel = GetEmoji(logEvent.LogEvent); break; case "u": stringLevel = stringLevel.ToUpperInvariant(); break; case "l": stringLevel = stringLevel.ToLowerInvariant(); break; } output.Write(stringLevel); }
/// <inheritdoc cref="IPropertyRenderer"/> public void Render(ExtendedLogEvent logEvent, TextWriter output) { var scalarValue = new ScalarValue(logEvent.LogEvent.Timestamp); scalarValue.Render(output, this.propertyToken.Format); }