Skip to content

Instantly share code, notes, and snippets.

@nblumhardt
nblumhardt / LateInitExample.cs
Created August 28, 2019 08:53
Serilog delayed sink initialization example
using System;
using Serilog;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
class Program
{
static void Main(string[] args)
{
@nblumhardt
nblumhardt / JsonDocumentDestructuringPolicy.cs
Created August 7, 2019 08:13
Properly serialize System.Text.Json documents with Serilog
using System;
using System.Linq;
using System.Text.Json;
using Serilog.Core;
using Serilog.Events;
class JsonDocumentDestructuringPolicy : IDestructuringPolicy
{
public bool TryDestructure(object value, ILogEventPropertyValueFactory _, out LogEventPropertyValue result)
{
using System;
using Serilog.Core;
using Serilog.Events;
// .Enrich.With(new OperationIdEnricher())
class OperationIdEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (logEvent.Properties.TryGetValue("RequestId", out var requestId))
@nblumhardt
nblumhardt / Program.cs
Last active September 4, 2024 10:05
Enrich.WithCaller()
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using Serilog;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
namespace ConsoleApp24
@nblumhardt
nblumhardt / Program.cs
Created June 5, 2017 22:39
A sketch implementation of a mapped sink wrapper for Serilog
using System;
using System.Collections.Generic;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
namespace Serilog.Sinks.Map
{
class Program
{
@nblumhardt
nblumhardt / example.js
Created May 25, 2017 20:37
CLEF event with formatted property
// Needs to be sent to Seq with the ?clef flag, e.g. /api/events/raw?clef
//
// See also:
// https://github.com/serilog/serilog-formatting-compact#reified-properties
{"@t":"...","@mt":"The {Item} cost ${Amount:0.00}","Item":"apple","Amount":3.2,"@r":["3.20"]}
@nblumhardt
nblumhardt / Install-Dependencies.ps1
Created October 28, 2016 06:09
Shows how to stream events matching a filter in Seq 3.4+ back out through the console via Serilog
Install-Package Seq.Api -Pre
Install-Package Serilog.Formatting.Compact.Reader -Pre
Install-Package Serilog.Sinks.Literate
Install-Package System.Reactive
@nblumhardt
nblumhardt / Program.cs
Created October 3, 2016 21:25
Render JSON data using MessageTemplates
using System;
using System.Linq;
using MessageTemplates.Core;
using MessageTemplates.Parsing;
using MessageTemplates.Structure;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MessageTemplateRenderer
{
@nblumhardt
nblumhardt / watermark.css
Created September 20, 2016 22:18
Show a watermark in the Seq navbar, e.g. "production", "my app", etc.
// Paste into Settings > Theme, <refresh>
.masthead h1:after {
content: 'PRODUCTION';
position: absolute;
top: 10px;
left: 110px;
color: white;
opacity: 0.4;
font-size: 16px;
@nblumhardt
nblumhardt / SerilogStructuredDataToolkit.cs
Last active June 3, 2016 01:53
Visitor pattern implementation for exploring/printing/rewriting structured data from Serilog
using System;
using System.Collections.Generic;
using System.Linq;
using Serilog.Events;
namespace SerilogStructuredDataToolkit
{
public abstract class LogEventPropertyValueVisitor<TResult>
{
public virtual TResult Visit(LogEventPropertyValue value)