Esempio n. 1
0
    /// <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()));
    }
Esempio n. 3
0
    /// <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>");
        }
    }
 /// <summary>
 /// Renders the given text <paramref name="token"/>. Results are written to the <paramref name="output"/>.
 /// </summary>
 /// <param name="token">The text token.</param>
 /// <param name="output">The output.</param>
 /// <param name="options">The sink options.</param>
 public static void Render(TextToken token, TextWriter output, TelegramSinkOptions options)
 {
     output.Write(HtmlEscaper.Escape(options, token.Text));
 }