Skip to content

Instantly share code, notes, and snippets.

@djeikyb
Created May 25, 2021 22:24
Show Gist options
  • Save djeikyb/0919da3e57aa32278c47f73fdcc42054 to your computer and use it in GitHub Desktop.
Save djeikyb/0919da3e57aa32278c47f73fdcc42054 to your computer and use it in GitHub Desktop.
Personal serilog console formatter that incentivizes high cardinality logs
using System.IO;
using Serilog.Events;
using Serilog.Formatting;
namespace com.example
{
public class EventCentricSerilogFormatter : ITextFormatter
{
private const string FgBrightWhite = "\x001B[97m";
private const string Reset = "\x001B[0m";
private readonly bool _plain;
public EventCentricSerilogFormatter(bool plain = false)
{
_plain = plain;
}
public void Format(LogEvent logEvent, TextWriter output)
{
var w = _plain ? Reset : FgBrightWhite;
output.WriteLine("---");
output.WriteLine($"level={w}{logEvent.Level}{Reset}");
// output.WriteLine($"timestamp={w}{logEvent.Timestamp}{Reset}");
output.WriteLine($"message={w}{logEvent.RenderMessage()}{Reset}");
foreach (var prop in logEvent.Properties)
{
switch (prop.Key.ToLowerInvariant())
{
// case "spanid":
case "traceid":
case "parentid":
case "connectionid":
case "eventid":
// case "envname":
continue;
}
output.WriteLine($"{prop.Key}={w}{prop.Value}{Reset}");
}
if (logEvent.Exception != null)
{
output.WriteLine(logEvent.Exception.ToString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment