LogSerializer 1.0.0
dotnet add package LogSerializer --version 1.0.0
NuGet\Install-Package LogSerializer -Version 1.0.0
<PackageReference Include="LogSerializer" Version="1.0.0" />
paket add LogSerializer --version 1.0.0
#r "nuget: LogSerializer, 1.0.0"
// Install LogSerializer as a Cake Addin #addin nuget:?package=LogSerializer&version=1.0.0 // Install LogSerializer as a Cake Tool #tool nuget:?package=LogSerializer&version=1.0.0
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 | Versions 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. |
-
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 |