Serilog.Destructure.NamedValuesHandler 0.2.0-alpha

This is a prerelease version of Serilog.Destructure.NamedValuesHandler.
dotnet add package Serilog.Destructure.NamedValuesHandler --version 0.2.0-alpha                
NuGet\Install-Package Serilog.Destructure.NamedValuesHandler -Version 0.2.0-alpha                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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                
#r "nuget: Serilog.Destructure.NamedValuesHandler, 0.2.0-alpha"                
#r directive can be used in F# Interactive and Polyglot Notebooks. 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                

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 Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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 229 9/16/2021
0.1.0-alpha 232 9/12/2021
0.0.2-alpha 236 9/11/2021
0.0.1-alpha 222 9/11/2021

Early development phase