Serilog.Sanitize 1.1.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Serilog.Sanitize --version 1.1.3
NuGet\Install-Package Serilog.Sanitize -Version 1.1.3
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.Sanitize" Version="1.1.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Serilog.Sanitize --version 1.1.3
#r "nuget: Serilog.Sanitize, 1.1.3"
#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.Sanitize as a Cake Addin
#addin nuget:?package=Serilog.Sanitize&version=1.1.3

// Install Serilog.Sanitize as a Cake Tool
#tool nuget:?package=Serilog.Sanitize&version=1.1.3

Serilog Log Sanitize Library

Serilog sanitize package is usable to mask or completely delete sensitive data.


Multiple Sanitize And Ignore Properties:

var logger = new LoggerConfiguration()
                    .Sanitizer()
                        .SanitizeViaRegex(new[] { "[Cc]vv", "[Cc]vc", "[Cc]vv" }, "***")
                        .SanitizeViaRegex(new[] { "[Cc]ard", "[Cc]ard[Nn]umber", "[Pp]an", "[Cc]ard[Nn]o" }, x => string.Concat(x.Substring(0, 6), "******", x.Substring(12, 4)))
                        .IgnoreProp(ignoreCase: true, "expmonth", "expyear", "expire", "expireYear", "expireMonth")
                        .Build()
                    .WriteTo.Debug()
                    .CreateLogger();

var model = new { CardNumber = "4355084355084358", Cvv = "000", ExpireMonth = "12", ExpireYear = "2026" };

logger.Information(
        "Sensitive Information {@p}",
        model
);

Output:

[13:36:28 INF] Sensitive Information {"CardNumber":"435508******4358","Cvv":"***","$type":"<>f__AnonymousType0`4"}

Typed Sanitize:

class Person
{
    public string Name { get; set; }

    public string Surname { get; set; }

    public string Phone { get; set; }

    public string Email { get; set; }
}

var logger = new LoggerConfiguration()
                    .Sanitizer()
                        .Typed<Person>()
                            .Sanitize(x => x.Name, x => string.Concat(x.Substring(0, 2), "***"))
                            .Sanitize(x => x.Surname, x => string.Concat(x.Substring(0, 2), "***"))
                            .Sanitize(x => x.Email, x => string.Concat(x.Substring(0, 2), "***"))
                            .Sanitize(x => x.Phone, x => string.Concat(x.Substring(0, 2), "***"))
                            .Continue()
                        .Build()
                        .WriteTo.Debug()
                        .CreateLogger();

            var model = new Person { Name = "Bruce", Surname = "Lee", Email = "brucelee@gmai.com", Phone = "5076589898" };

logger.Information(
    "Sensitive Information {@p1} {@p2}",
    model,
    company
);

Output:

[13:53:52 INF] Sensitive Information {"Name":"Br***","Surname":"Le***","Phone":"50***","Email":"br***","$type":"Person"} {"Phone":"2226663355","Name":"Foo corp.","Email":"foo@bar.com","$type":"Company"}

Sanitize Via Property Name

var logger = new LoggerConfiguration()
                    .Sanitizer()
                        .Sanitize("Phone", phone => string.Concat(phone.Substring(0, 4), "****", phone.Substring(8, 2)))
                        .Build()
                    .WriteTo.Debug()
                    .CreateLogger();

var model = new { Name = "Bruce", Phone = "5076589898", WorkPhone = "2123256985" };

logger.Information(
        "Sensitive Information {@p}",
        model
);

Output:

[13:56:19 INF] Sensitive Information {"Name":"Bruce","Phone":"5076****98","WorkPhone":"2123256985","$type":"<>f__AnonymousType1`3"}

Sanitize Via Regex

var logger = new LoggerConfiguration()
                    .Sanitizer()
                        .SanitizeViaRegex("[Pp]hone", phone => string.Concat(phone.Substring(0, 4), "****", phone.Substring(8, 2)))
                        .Build()
                    .WriteTo.Debug()
                    .CreateLogger();

var model = new { Name = "Bruce", Phone = "5076589898", WorkPhone = "2123256985" };

logger.Information(
        "Sensitive Information {@p}",
        model
);

Output:

[13:59:18 INF] Sensitive Information {"Name":"Bruce","Phone":"5076****98","WorkPhone":"2123****85","$type":"<>f__AnonymousType1`3"}

Sanitize Property With Constant Value

const string SANITIZE_PASSWORD = "***";

var logger = new LoggerConfiguration()
                    .Sanitizer()
                        .SanitizeViaRegex("[Pp]assword", SANITIZE_PASSWORD)
                    .Build()
                    .WriteTo.Debug()
                    .CreateLogger();

var model = new { Name = "Bruce", Password = "123" };

logger.Information(
        "Sensitive Information {@p}",
        model
);

Output:

[14:01:22 INF] Sensitive Information {"Name":"Bruce","Password":"***","$type":"<>f__AnonymousType2`2"}
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. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 is compatible.  netcoreapp3.1 is compatible. 
.NET Framework net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 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
1.2.2 177 8/18/2023
1.2.1 159 5/25/2023
1.1.4 513 6/30/2022
1.1.3 397 6/29/2022
1.1.2 485 4/7/2022
1.1.0 408 3/23/2022
1.0.1 467 1/11/2022
1.0.0 395 1/10/2022