Serilog.Destructure.NamedValuesHandler 0.2.0-alpha

.NET 5.0
This is a prerelease version of Serilog.Destructure.NamedValuesHandler.
Install-Package Serilog.Destructure.NamedValuesHandler -Version 0.2.0-alpha
dotnet add package Serilog.Destructure.NamedValuesHandler --version 0.2.0-alpha
<PackageReference Include="Serilog.Destructure.NamedValuesHandler" Version="0.2.0-alpha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Serilog.Destructure.NamedValuesHandler --version 0.2.0-alpha
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Serilog.Destructure.NamedValuesHandler, 0.2.0-alpha"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Serilog.Destructure.NamedValuesHandler as a Cake Addin
#addin nuget:?package=Serilog.Destructure.NamedValuesHandler&version=0.2.0-alpha&prerelease

// Install Serilog.Destructure.NamedValuesHandler as a Cake Tool
#tool nuget:?package=Serilog.Destructure.NamedValuesHandler&version=0.2.0-alpha&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Serilog.Destructure.NamedValuesHandler

GitHub Workflow Status (branch) Nuget

Work In Progress. No guaranties.

ToDos

  • Investigate how Serilog handles system values(string, DateTime, etc.) at destructuring
  • Resolve todos in code

Idea

You use Serilog and structured logging in your project.

You want to mask or even omit some special values from properties of your log events.

This package was created exactly for these purposes.

Usage

Log Example

So you have an object to log:

var user = new User
{
    Name = "John Watson",
    Email = "dr.john.h.watson@johnwatsonblog.co.uk",
    Age = 42,
};

We do want to mask doctor's email for whatever reason, but we want to keep some part of that information. Let's setup logger so we could see just the latest 7 characters of the email:

var logger = new LoggerConfiguration()
    .WriteTo.Console()
    .HandleValues(p => p
        .Mask("email", visibleCharsAmount:7)
    )
    .CreateLogger();

And then let's check how it logs this object:

logger.Information("Logged User: {@User}", user);
[01:36:10 INF] Logged User: {"Name": "John Watson", "Email": "******************************g.co.uk", "Age": 42}

But we may have more complex situation. For example, we still want to see at least a domain name.

Let's change mask handling:

.HandleValues(p => p
    .Handle<string>(
      "email",
      value => string.IsNullOrWhiteSpace(value)
          ? value
          : $"*****@{value.Split("@").Last()}")
    )
)

Now we have only domain name in a log message:

[01:43:25 INF] Logged User: {"Name": "John Watson", "Email": "*****@johnwatsonblog.co.uk", "Age": 42}

We can also omit some values so they disappear from log message:

.HandleValues(p => p
    .Omit("email", "age")
)
[01:49:19 INF] Logged User: {"Name": "John Watson"}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.0-alpha 101 9/16/2021
0.1.0-alpha 122 9/12/2021
0.0.2-alpha 134 9/11/2021
0.0.1-alpha 120 9/11/2021

Early development phase