Created
August 7, 2019 20:02
-
-
Save DragoonAethis/a4a82c88eb462c2f4e9b776c34ff6317 to your computer and use it in GitHub Desktop.
Serilog Property Filtering Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.2</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Serilog" Version="2.8.0" /> | |
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" /> | |
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Serilog; | |
using Serilog.Core; | |
using Serilog.Events; | |
// Filter out ThreadId=1 from logs (or any other property with a specified value) | |
// https://github.com/serilog/serilog-enrichers-thread/issues/15#issuecomment-518406791 | |
namespace SerilogTests { | |
class DefaultPropertyValueRemover : ILogEventEnricher { | |
readonly string _propertyName; | |
readonly ScalarValue _targetValue; | |
public DefaultPropertyValueRemover(string propertyName, object targetValue) { | |
_propertyName = propertyName; | |
_targetValue = new ScalarValue(targetValue); | |
} | |
public void Enrich(LogEvent le, ILogEventPropertyFactory _) { | |
if (le.Properties.TryGetValue(_propertyName, out var val) && _targetValue.Equals(val)) { | |
le.RemovePropertyIfPresent(_propertyName); | |
} | |
} | |
} | |
class Program { | |
static void DoStuffInBackground(object stateInfo) { | |
Log.Information("Hello from the background!"); | |
} | |
static void Main(string[] args) { | |
Log.Logger = new LoggerConfiguration() | |
.WriteTo.Console(outputTemplate: "[{Level:u3}] {Message:lj} {Properties}{NewLine}") | |
.Enrich.WithThreadId() | |
.Enrich.With(new DefaultPropertyValueRemover("ThreadId", 1)) | |
.CreateLogger(); | |
Log.Information("Hello, Serilog!"); | |
ThreadPool.QueueUserWorkItem(DoStuffInBackground); | |
Thread.Sleep(250); // "Work" "very hard" on the main thread :) | |
Log.Information("Hello from the foreground again!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet run | |
[INF] Hello, Serilog! {} | |
[INF] Hello from the background! {ThreadId=4} | |
[INF] Hello from the foreground again! {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment