Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@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)
{
@nblumhardt
nblumhardt / Program.cs
Last active October 27, 2023 18:39
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
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 / 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 / PerMinuteInExcessOf.cs
Created November 17, 2015 10:50
Proof of concept rate limiting filter for Serilog
using System;
using Serilog;
using Serilog.Configuration;
namespace ConsoleApplication8
{
static class Program
{
static void Main()
{
@nblumhardt
nblumhardt / Program.cs
Created June 25, 2014 21:52
Exception data enricher for Serilog
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting.Json;
@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 / Example.ps1
Last active November 8, 2019 11:03
Seq from Powershell POC
Import-Module Seq
# Specify the Seq server URL; an -apiKey can also be provided.
# Any properties set here will be attached to all events.
$seq = Open-Seq "http://my-seq" -properties @{ Machine = $env:ComputerName }
# Simple information method
Send-SeqEvent $seq "Hello from PowerShell"
@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"]}