LogSerializer 1.0.0-beta002

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

// Install LogSerializer as a Cake Tool
#tool nuget:?package=LogSerializer&version=1.0.0-beta002&prerelease                

LogSerializer .NET

LogSerializer is a .NET package for serializing objects to JSON with sensitive data (such as access tokens or personally identifiable information) masked out so that it can be used for logging and telemetry purposes.

Installation

Install the package via NuGet:

dotnet add package LogSerializer

Configuration

Configure the default options with the LogSerializer.Configure method:

LogSerializer.Configure(o => {

  o.MaskText = "<REDACTED>",
  o.SensitiveDataProperties.Add(new("Person", "LastName")),
  o.JsonSerializerOptions = new()
  {
    PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
  }
})

Or construct a new LogSerializerOptions and pass it to the methods:

var options = new LogSerializerOptions()
{
  MaskText = "<REDACTED>",
  SensitiveDataProperties = { new("Person", "LastName") },
  JsonSerializerOptions = new()
  {
    PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
  }
};
LogSerializer.Serialize(obj, options);

Sensitive data can be marked with annotations:

public class TokenResponse 
{
  [SensitiveData]
  public string Token { get; set; }
}

Or configured in LogSerializerOptions.SensitiveDataProperties. The list of type name and property name can accept null type name to match the property on any type:

options.SensitiveDataProperties.Add(new(null, "LastName"))

Or the short type name:

options.SensitiveDataProperties.Add(new("Person", "LastName"))

Or the fully qualified type name:

options.SensitiveDataProperties.Add(new("Foo.Bar.Person", "LastName"))

Usage

Use LogSerializer.Serialize to serialize objects to JSON for logging:

logger.LogInformation("Processed message: {message}", LogSerializer.Serialize(message));

Use LogSerializer.Destructure to create a dictionary out of an object's properties where the key is the property name and the value is the serialized JSON of the property value. This is useful with telemetry logging libraries that track custom telemetry properties as key value dictionaries, such as TelemetryClient.TrackEvent in Application Insights:

telemetry.TrackEvent("ProcessMessage", LogSerializer.Destructure(message));

License

This project is licensed under the Apache License Version 2.0 - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.
  • net8.0

    • No dependencies.

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.0.0 523 7/22/2024
1.0.0-beta003 85 7/22/2024
1.0.0-beta002 102 5/6/2024
1.0.0-beta001 106 5/6/2024