Skip to content

Instantly share code, notes, and snippets.

@sasimpson
Created April 28, 2025 19:31
Show Gist options
  • Save sasimpson/4beffdc0469453cba7852048b37e0e19 to your computer and use it in GitHub Desktop.
Save sasimpson/4beffdc0469453cba7852048b37e0e19 to your computer and use it in GitHub Desktop.
Demo of using structured logging with SeriLog in .NET
using Serilog;
using Serilog.Formatting.Compact;
namespace LogDemo;
class Foo(string name, string description)
{
public string Name { get; set; } = name;
public string Description { get; set; } = description;
public string[] Things { get; set; } = [];
}
internal static class Program
{
private static void Main()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console(new CompactJsonFormatter())
.CreateLogger();
Log.Information("Hello Info");
Log.Debug("Hello Debug");
var thing = new Foo("Test", "Test Desc");
thing.Things = ["one", "two", "three"];
Log.Information("things: {@Things}", thing);
}
}
/*
{"@t":"2025-04-28T19:26:40.9145290Z","@mt":"Hello Info"}
{"@t":"2025-04-28T19:26:40.9491260Z","@mt":"Hello Debug","@l":"Debug"}
{"@t":"2025-04-28T19:26:40.9501810Z","@mt":"things: {@Things}","Things":{"Name":"Test","Description":"Test Desc","Things":["one","two","three"],"$type":"Foo"}}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment