From 8777079e09004137dce2edfac26cc87afce675fa Mon Sep 17 00:00:00 2001 From: Bardin08 Date: Sat, 17 Feb 2024 23:44:00 +0200 Subject: [PATCH] Refactor message formatters --- ...DefaultAggregatedNotificationsFormatter.cs | 40 ++++++++++++++----- .../Formatters/DefaultLogFormatter.cs | 40 ++++++++----------- src/X.Serilog.Sinks.Telegram/GlobalUsings.cs | 4 +- src/X.Serilog.Sinks.Telegram/LogEntry.cs | 17 ++++---- 4 files changed, 56 insertions(+), 45 deletions(-) diff --git a/src/X.Serilog.Sinks.Telegram/Formatters/DefaultAggregatedNotificationsFormatter.cs b/src/X.Serilog.Sinks.Telegram/Formatters/DefaultAggregatedNotificationsFormatter.cs index 24c01b7..257637a 100644 --- a/src/X.Serilog.Sinks.Telegram/Formatters/DefaultAggregatedNotificationsFormatter.cs +++ b/src/X.Serilog.Sinks.Telegram/Formatters/DefaultAggregatedNotificationsFormatter.cs @@ -25,30 +25,48 @@ private List DefaultFormatter(IEnumerable logEntries, Formatte if (config.TimeZone is not null) { batchBeginTimestamp = TimeZoneInfo.ConvertTime(batchBeginTimestamp, config.TimeZone); - batchEndTimestamp = TimeZoneInfo.ConvertTime(batchBeginTimestamp, config.TimeZone); + batchEndTimestamp = TimeZoneInfo.ConvertTime(batchEndTimestamp, config.TimeZone); } - sb.Append("[").Append($"{batchBeginTimestamp:G}").Append('—').Append($"{batchEndTimestamp:G}") - .Append("]").Append(' ') - .Append(config.ReadableApplicationName) + sb.Append("Logs from ").Append($"{batchBeginTimestamp:G}").Append(" to ") + .Append($"{batchEndTimestamp:G}").Append("") .AppendLine() .AppendLine(); foreach (var logEntry in logEntries) { - if (!NotEmpty(logEntry)) continue; + var level = config.UseEmoji ? ToEmoji(logEntry.Level) : logEntry.Level.ToString(); - var level = config.UseEmoji ? ToEmoji(logEntry.Level) : ToString(logEntry.Level); + sb.Append(level).Append(' ').Append("[").Append($"{logEntry.UtcTimeStamp:G}").Append("]"); - sb.Append(level).Append(' ').Append("[").Append($"{logEntry.UtcTimeStamp:T}").Append("]"); + if (!string.IsNullOrEmpty(config.ReadableApplicationName)) + { + sb.Append(" ").Append(config.ReadableApplicationName).Append(""); + } + + if (!string.IsNullOrWhiteSpace(logEntry.RenderedMessage)) + { + sb.AppendLine().Append("Message: ").Append(logEntry.RenderedMessage).Append(""); + } - if (NotEmpty(logEntry.RenderedMessage)) + if (config.IncludeException && !string.IsNullOrWhiteSpace(logEntry.Exception)) { - sb.Append(" ").Append(logEntry.RenderedMessage).Append(";") - .AppendLine(); + sb.AppendLine().Append("Exception: ").Append(logEntry.Exception).Append(""); } + + if (config.IncludeProperties && logEntry.Properties != null && logEntry.Properties.Any()) + { + sb.AppendLine().Append("Properties: "); + foreach (var property in logEntry.Properties) + { + sb.Append("").Append(property.Key).Append(": ").Append(property.Value) + .Append("; "); + } + } + + sb.AppendLine().AppendLine(); } - return new List { sb.ToString() }; + return [sb.ToString().TrimEnd()]; } } \ No newline at end of file diff --git a/src/X.Serilog.Sinks.Telegram/Formatters/DefaultLogFormatter.cs b/src/X.Serilog.Sinks.Telegram/Formatters/DefaultLogFormatter.cs index d513967..e751c79 100644 --- a/src/X.Serilog.Sinks.Telegram/Formatters/DefaultLogFormatter.cs +++ b/src/X.Serilog.Sinks.Telegram/Formatters/DefaultLogFormatter.cs @@ -50,40 +50,34 @@ private string FormatMessageInternal(LogEntry logEntry, FormatterConfiguration c { if (logEntry is null) throw new ArgumentNullException(nameof(logEntry)); - var level = config.UseEmoji ? ToEmoji(logEntry.Level) : ToString(logEntry.Level); - var sb = new StringBuilder(); + var timestamp = config.TimeZone != null + ? TimeZoneInfo.ConvertTime(logEntry.UtcTimeStamp, config.TimeZone) + : logEntry.UtcTimeStamp; - var timestamp = logEntry.UtcTimeStamp; - if (config.TimeZone is not null) - { - timestamp = TimeZoneInfo.ConvertTime(timestamp, config.TimeZone); - } - - sb.Append(level).Append(' ').Append("[").Append($"{timestamp:G}").Append("]").Append(' ') - .Append(config.ReadableApplicationName); - - sb.AppendLine(); - sb.AppendLine(); + sb.Append(config.UseEmoji ? ToEmoji(logEntry.Level) + logEntry.Level: logEntry.Level.ToString()) + .Append(' ').Append('[').Append(config.ReadableApplicationName ?? "YourApp").Append(']') + .Append(' ').Append('[').Append($"{timestamp:yyyy-MM-dd HH:mm:ss UTC}").Append(']') + .AppendLine(); if (NotEmpty(logEntry.RenderedMessage)) { - sb.Append("").Append("Message: ").Append("").Append("").Append(logEntry.RenderedMessage) - .Append("").AppendLine(); + sb.AppendLine().Append("Message: ").Append(logEntry.RenderedMessage).Append("").AppendLine(); } - if (config.IncludeException && - NotEmpty(logEntry.Exception)) + if (config.IncludeException && NotEmpty(logEntry.Exception)) { - sb.Append("").Append("Exception: ").Append("").Append("").Append(logEntry.Exception) - .Append("").AppendLine(); + sb.AppendLine().Append("Exception: `").Append(logEntry.Exception).Append("`").AppendLine(); } - if (config.IncludeProperties && - NotEmpty(logEntry.Properties)) + if (config.IncludeProperties && logEntry.Properties != null && logEntry.Properties.Count != 0) { - sb.Append("").Append("Properties: ").Append("").AppendLine() - .Append("").Append(logEntry.Properties).Append("").AppendLine(); + sb.AppendLine().Append("").Append("Properties: ").Append("").AppendLine(); + foreach (var property in logEntry.Properties) + { + sb.Append("").Append(property.Key).Append(": ").Append(property.Value).Append("") + .AppendLine(); + } } return sb.ToString(); diff --git a/src/X.Serilog.Sinks.Telegram/GlobalUsings.cs b/src/X.Serilog.Sinks.Telegram/GlobalUsings.cs index 9bbdc13..bae7642 100644 --- a/src/X.Serilog.Sinks.Telegram/GlobalUsings.cs +++ b/src/X.Serilog.Sinks.Telegram/GlobalUsings.cs @@ -7,6 +7,4 @@ global using Serilog; global using Serilog.Configuration; -global using Serilog.Events; - -global using Newtonsoft.Json; \ No newline at end of file +global using Serilog.Events; \ No newline at end of file diff --git a/src/X.Serilog.Sinks.Telegram/LogEntry.cs b/src/X.Serilog.Sinks.Telegram/LogEntry.cs index 7ec75b0..6b09345 100644 --- a/src/X.Serilog.Sinks.Telegram/LogEntry.cs +++ b/src/X.Serilog.Sinks.Telegram/LogEntry.cs @@ -2,17 +2,19 @@ public class LogEntry { - public LogEventLevel Level { get; init; } + private LogEntry() + { + } - public DateTime UtcTimeStamp { get; init; } + public LogEventLevel Level { get; private init; } - public MessageTemplate? MessageTemplate { get; init; } + public DateTime UtcTimeStamp { get; private init; } - public string? RenderedMessage { get; init; } + public string? RenderedMessage { get; private init; } - public string? Properties { get; init; } + public Dictionary? Properties { get; private init; } - public string? Exception { get; init; } + public string? Exception { get; private init; } public static LogEntry MapFrom(LogEvent logEvent) { @@ -20,12 +22,11 @@ public static LogEntry MapFrom(LogEvent logEvent) return new LogEntry { - MessageTemplate = logEvent.MessageTemplate, RenderedMessage = logEvent.RenderMessage(), Level = logEvent.Level, UtcTimeStamp = logEvent.Timestamp.ToUniversalTime().UtcDateTime, Exception = logEvent.Exception?.ToString(), - Properties = JsonConvert.SerializeObject(logEvent.Properties, Formatting.Indented) + Properties = logEvent.Properties.ToDictionary(x => x.Key, x => x.Value.ToString()) }; } } \ No newline at end of file