Skip to content

Instantly share code, notes, and snippets.

@nblumhardt
nblumhardt / LateInitExample.cs
Created Aug 28, 2019
Serilog delayed sink initialization example
View LateInitExample.cs
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 Aug 7, 2019
Properly serialize System.Text.Json documents with Serilog
View JsonDocumentDestructuringPolicy.cs
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)
{
View OperationIdEnricher.cs
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 Feb 2, 2022
Enrich.WithCaller()
View Program.cs
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 Jun 5, 2017
A sketch implementation of a mapped sink wrapper for Serilog
View Program.cs
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
CLEF event with formatted property
View example.js
// 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 Oct 28, 2016
Shows how to stream events matching a filter in Seq 3.4+ back out through the console via Serilog
View Install-Dependencies.ps1
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 Oct 3, 2016
Render JSON data using MessageTemplates
View Program.cs
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 Sep 20, 2016
Show a watermark in the Seq navbar, e.g. "production", "my app", etc.
View watermark.css
// 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 Jun 3, 2016
Visitor pattern implementation for exploring/printing/rewriting structured data from Serilog
View SerilogStructuredDataToolkit.cs
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)