Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor message formatters #22

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,48 @@ private List<string> DefaultFormatter(IEnumerable<LogEntry> 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("<em>[").Append($"{batchBeginTimestamp:G}").Append('—').Append($"{batchEndTimestamp:G}")
.Append("]</em>").Append(' ')
.Append(config.ReadableApplicationName)
sb.Append("<b>Logs from ").Append($"{batchBeginTimestamp:G}").Append(" to ")
.Append($"{batchEndTimestamp:G}").Append("</b>")
.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("<em>[").Append($"{logEntry.UtcTimeStamp:G}").Append("]</em>");

sb.Append(level).Append(' ').Append("<em>[").Append($"{logEntry.UtcTimeStamp:T}").Append("]</em>");
if (!string.IsNullOrEmpty(config.ReadableApplicationName))
{
sb.Append(" <strong>").Append(config.ReadableApplicationName).Append("</strong>");
}

if (!string.IsNullOrWhiteSpace(logEntry.RenderedMessage))
{
sb.AppendLine().Append("Message: <code>").Append(logEntry.RenderedMessage).Append("</code>");
}

if (NotEmpty(logEntry.RenderedMessage))
if (config.IncludeException && !string.IsNullOrWhiteSpace(logEntry.Exception))
{
sb.Append(" <code>").Append(logEntry.RenderedMessage).Append("</code>;")
.AppendLine();
sb.AppendLine().Append("Exception: <code>").Append(logEntry.Exception).Append("</code>");
}

if (config.IncludeProperties && logEntry.Properties != null && logEntry.Properties.Any())
{
sb.AppendLine().Append("Properties: ");
foreach (var property in logEntry.Properties)
{
sb.Append("<code>").Append(property.Key).Append(": ").Append(property.Value)
.Append("</code>; ");
}
}

sb.AppendLine().AppendLine();
}

return new List<string> { sb.ToString() };
return [sb.ToString().TrimEnd()];
}
}
40 changes: 17 additions & 23 deletions src/X.Serilog.Sinks.Telegram/Formatters/DefaultLogFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<em>[").Append($"{timestamp:G}").Append("]</em>").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("<em>").Append("Message: ").Append("</em>").Append("<code>").Append(logEntry.RenderedMessage)
.Append("</code>").AppendLine();
sb.AppendLine().Append("<b>Message:</b> <code>").Append(logEntry.RenderedMessage).Append("</code>").AppendLine();
}

if (config.IncludeException &&
NotEmpty(logEntry.Exception))
if (config.IncludeException && NotEmpty(logEntry.Exception))
{
sb.Append("<em>").Append("Exception: ").Append("</em>").Append("<code>").Append(logEntry.Exception)
.Append("</code>").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("<em>").Append("Properties: ").Append("</em>").AppendLine()
.Append("<code>").Append(logEntry.Properties).Append("</code>").AppendLine();
sb.AppendLine().Append("<b>").Append("Properties: ").Append("</b>").AppendLine();
foreach (var property in logEntry.Properties)
{
sb.Append("<code>").Append(property.Key).Append(": ").Append(property.Value).Append("</code>")
.AppendLine();
}
}

return sb.ToString();
Expand Down
4 changes: 1 addition & 3 deletions src/X.Serilog.Sinks.Telegram/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@

global using Serilog;
global using Serilog.Configuration;
global using Serilog.Events;

global using Newtonsoft.Json;
global using Serilog.Events;
17 changes: 9 additions & 8 deletions src/X.Serilog.Sinks.Telegram/LogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@

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<string, string>? Properties { get; private init; }

public string? Exception { get; init; }
public string? Exception { get; private init; }

public static LogEntry MapFrom(LogEvent logEvent)
{
ArgumentNullException.ThrowIfNull(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())
};
}
}