VOOZH about

URL: https://www.nuget.org/packages/Destructurama.Attributed/

⇱ NuGet Gallery | Destructurama.Attributed 5.3.0




👁 Image
Destructurama.Attributed 5.3.0

dotnet add package Destructurama.Attributed --version 5.3.0
 
 
NuGet\Install-Package Destructurama.Attributed -Version 5.3.0
 
 
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="Destructurama.Attributed" Version="5.3.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Destructurama.Attributed" Version="5.3.0" />
 
Directory.Packages.props
<PackageReference Include="Destructurama.Attributed" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Destructurama.Attributed --version 5.3.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Destructurama.Attributed, 5.3.0"
 
 
#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.
#:package Destructurama.Attributed@5.3.0
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Destructurama.Attributed&version=5.3.0
 
Install as a Cake Addin
#tool nuget:?package=Destructurama.Attributed&version=5.3.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Destructurama.Attributed

👁 License

👁 codecov
👁 Nuget
👁 Nuget

👁 GitHub Release Date
👁 GitHub commits since latest release (by date)
👁 Size

👁 GitHub contributors
👁 Activity
👁 Activity
👁 Activity

👁 Run unit tests
👁 Publish preview to GitHub registry
👁 Publish release to Nuget registry
👁 CodeQL analysis

This package makes it possible to manipulate how objects are logged to Serilog using attributes.

Installation

Install from NuGet:

Install-Package Destructurama.Attributed

Usage

Modify logger configuration:

using Destructurama;
...
var log = new LoggerConfiguration()
 .Destructure.UsingAttributes()
...

1. Changing a property name

Apply the LogWithName attribute:

<a id='snippet-LogWithName'></a>

public class PersonalData
{
 [LogWithName("FullName")]
 public string? Name { get; set; }
}

<sup><a href='/src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs#L64-L70' title='Snippet source file'>snippet source</a> | <a href='#snippet-LogWithName' title='Start of snippet'>anchor</a></sup>

2. Ignoring a property

Apply the NotLogged attribute:

<a id='snippet-LoginCommand'></a>

public class LoginCommand
{
 public string? Username { get; set; }

 [NotLogged]
 public string? Password { get; set; }
}

<sup><a href='/src/Destructurama.Attributed.Tests/Snippets.cs#L29-L37' title='Snippet source file'>snippet source</a> | <a href='#snippet-LoginCommand' title='Start of snippet'>anchor</a></sup>

When the object is passed using {@...} syntax the attributes will be consulted.

<a id='snippet-LogCommand'></a>

var command = new LoginCommand { Username = "logged", Password = "not logged" };
_log.Information("Logging in {@Command}", command);

<sup><a href='/src/Destructurama.Attributed.Tests/Snippets.cs#L44-L47' title='Snippet source file'>snippet source</a> | <a href='#snippet-LogCommand' title='Start of snippet'>anchor</a></sup>

3. Ignoring a property if it has default value

Apply the NotLoggedIfDefault attribute:

public class LoginCommand
{
 public string Username { get; set; }

 [NotLoggedIfDefault]
 public string Password { get; set; }

 [NotLoggedIfDefault]
 public DateTime TimeStamp { get; set; }
}

4. Ignoring a property if it has null value

Apply the NotLoggedIfNull attribute:

public class LoginCommand
{
 /// <summary>
 /// `null` value results in removed property
 /// </summary>
 [NotLoggedIfNull]
 public string Username { get; set; }

 /// <summary>
 /// Can be applied with [LogMasked] or [LogReplaced] attributes
 /// `null` value results in removed property
 /// "123456789" results in "***"
 /// </summary>
 [NotLoggedIfNull]
 [LogMasked]
 public string Password { get; set; }

 /// <summary>
 /// Attribute has no effect on non-reference and non-nullable types
 /// </summary>
 [NotLoggedIfNull]
 public int TimeStamp { get; set; }
}

Ignore null properties can be globally applied during logger configuration without need to apply attributes:

var log = new LoggerConfiguration()
 .Destructure.UsingAttributes(x => x.IgnoreNullProperties = true)
 ...

5. Treating types and properties as scalars

To prevent destructuring of a type or property at all, apply the LogAsScalar attribute.

6. Masking a property

Apply the LogMasked attribute with various settings:

  • Text: If set, the property value will be set to this text.
  • ShowFirst: Shows the first x characters in the property value.
  • ShowLast: Shows the last x characters in the property value.
  • PreserveLength: If set, it will swap out each character with the default value. Note that this property will be ignored if Text has been set to custom value.

Masking works for all properties calling ToString() on their values. Note that masking also works for properties of type IEnumerable<string> or derived from it, for example, string[] or List<string>.

Examples

<a id='snippet-CustomizedMaskedLogs'></a>

public class CustomizedMaskedLogs
{
 /// <summary>
 /// 123456789 results in "***"
 /// </summary>
 [LogMasked]
 public string? DefaultMasked { get; set; }

 /// <summary>
 /// 9223372036854775807 results in "***"
 /// </summary>
 [LogMasked]
 public long? DefaultMaskedLong { get; set; }

 /// <summary>
 /// 2147483647 results in "***"
 /// </summary>
 [LogMasked]
 public int? DefaultMaskedInt { get; set; }

 /// <summary>
 /// [123456789,123456789,123456789] results in [***,***,***]
 /// </summary>
 [LogMasked]
 public string[]? DefaultMaskedArray { get; set; }

 /// <summary>
 /// 123456789 results in "*********"
 /// </summary>
 [LogMasked(PreserveLength = true)]
 public string? DefaultMaskedPreserved { get; set; }

 /// <summary>
 /// "" results in "***"
 /// </summary>
 [LogMasked]
 public string? DefaultMaskedNotPreservedOnEmptyString { get; set; }

 /// <summary>
 /// 123456789 results in "#"
 /// </summary>
 [LogMasked(Text = "_REMOVED_")]
 public string? CustomMasked { get; set; }

 /// <summary>
 /// 123456789 results in "#"
 /// </summary>
 [LogMasked(Text = "")]
 public string? CustomMaskedWithEmptyString { get; set; }

 /// <summary>
 /// 123456789 results in "#########"
 /// </summary>
 [LogMasked(Text = "#", PreserveLength = true)]
 public string? CustomMaskedPreservedLength { get; set; }

 /// <summary>
 /// 123456789 results in "123******"
 /// </summary>
 [LogMasked(ShowFirst = 3)]
 public string? ShowFirstThreeThenDefaultMasked { get; set; }

 /// <summary>
 /// 9223372036854775807 results in "922***807"
 /// </summary>
 [LogMasked(ShowFirst = 3, ShowLast = 3)]
 public long? ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle { get; set; }

 /// <summary>
 /// 2147483647 results in "214****647"
 /// </summary>
 [LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)]
 public int? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; }

 /// <summary>
 /// 123456789 results in "123******"
 /// </summary>
 [LogMasked(ShowFirst = 3, PreserveLength = true)]
 public string? ShowFirstThreeThenDefaultMaskedPreservedLength { get; set; }

 /// <summary>
 /// 123456789 results in "***789"
 /// </summary>
 [LogMasked(ShowLast = 3)]
 public string? ShowLastThreeThenDefaultMasked { get; set; }

 /// <summary>
 /// 123456789 results in "******789"
 /// </summary>
 [LogMasked(ShowLast = 3, PreserveLength = true)]
 public string? ShowLastThreeThenDefaultMaskedPreservedLength { get; set; }

 /// <summary>
 /// 123456789 results in "123_REMOVED_"
 /// </summary>
 [LogMasked(Text = "_REMOVED_", ShowFirst = 3)]
 public string? ShowFirstThreeThenCustomMask { get; set; }

 /// <summary>
 /// d3c4a1f2-3b4e-4f5a-9b6c-7d8e9f0a1b2c results in "d3c4a_REMOVED_"
 /// </summary>
 [LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
 public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; }

 /// <summary>
 /// Descending results in "Desce_REMOVED_"
 /// </summary>
 [LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
 public ListSortDirection ShowFirstFiveThenCustomMaskEnum { get; set; }

 /// <summary>
 /// 123456789 results in "123_REMOVED_"
 /// </summary>
 [LogMasked(Text = "_REMOVED_", ShowFirst = 3, PreserveLength = true)]
 public string? ShowFirstThreeThenCustomMaskPreservedLengthIgnored { get; set; }

 /// <summary>
 /// 123456789 results in "_REMOVED_789"
 /// </summary>
 [LogMasked(Text = "_REMOVED_", ShowLast = 3)]
 public string? ShowLastThreeThenCustomMask { get; set; }

 /// <summary>
 /// 123456789 results in "_REMOVED_789"
 /// </summary>
 [LogMasked(Text = "_REMOVED_", ShowLast = 3, PreserveLength = true)]
 public string? ShowLastThreeThenCustomMaskPreservedLengthIgnored { get; set; }

 /// <summary>
 /// 123456789 results in "123***789"
 /// </summary>
 [LogMasked(ShowFirst = 3, ShowLast = 3)]
 public string? ShowFirstAndLastThreeAndDefaultMaskInTheMiddle { get; set; }

 /// <summary>
 /// 123456789 results in "123456789", no mask applied
 /// </summary>
 [LogMasked(ShowFirst = -1, ShowLast = -1)]
 public string? ShowFirstAndLastInvalidValues { get; set; }

 /// <summary>
 /// 123456789 results in "123***789"
 /// </summary>
 [LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)]
 public string? ShowFirstAndLastThreeAndDefaultMaskInTheMiddlePreservedLength { get; set; }

 /// <summary>
 /// 123456789 results in "123_REMOVED_789"
 /// </summary>
 [LogMasked(Text = "_REMOVED_", ShowFirst = 3, ShowLast = 3)]
 public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddle { get; set; }

 /// <summary>
 /// 123456789 results in "123_REMOVED_789". PreserveLength is ignored"
 /// </summary>
 [LogMasked(Text = "_REMOVED_", ShowFirst = 3, ShowLast = 3, PreserveLength = true)]
 public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; }
}

<sup><a href='/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L9-L170' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomizedMaskedLogs' title='Start of snippet'>anchor</a></sup>

7. Masking a string property with regular expressions

Apply the LogReplaced attribute on a string to apply a RegEx replacement during Logging.

This is applicable in scenarios when a string contains both Sensitive and Non-Sensitive information. An example of this could be a string such as "Sensitive|NonSensitive". Then apply the attribute like the following snippet:

[LogReplaced(@"([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)", "***|$2")]
public string Information { get; set; }

// Will log: "***|NonSensitive"

LogReplaced attribute is available with the following constructor:

LogReplaced(string pattern, string replacement)

Constructor arguments:

  • pattern: The pattern that should be applied on value.
  • replacement: The string that will be applied by RegEx.

Available properties:

  • Options: The RegexOptions that will be applied. Defaults to RegexOptions.None.
  • Timeout: A time-out interval to evaluate regular expression. Defaults to Regex.InfiniteMatchTimeout.

Note that replacement also works for properties of type IEnumerable<string> or derived from it, for example, string[] or List<string>.

Examples

<a id='snippet-WithRegex'></a>

public class WithRegex
{
 private const string REGEX_WITH_VERTICAL_BARS = @"([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)";

 /// <summary>
 /// 123|456|789 results in "***|456|789"
 /// </summary>
 [LogReplaced(REGEX_WITH_VERTICAL_BARS, "***|$2|$3")]
 public string? RegexReplaceFirst { get; set; }

 /// <summary>
 /// 123|456|789 results in "123|***|789"
 /// </summary>
 [LogReplaced(REGEX_WITH_VERTICAL_BARS, "$1|***|$3")]
 public string? RegexReplaceSecond { get; set; }
}

<sup><a href='/src/Destructurama.Attributed.Tests/Snippets.cs#L6-L25' title='Snippet source file'>snippet source</a> | <a href='#snippet-WithRegex' title='Start of snippet'>anchor</a></sup>

8. Working with MetadataTypeAttribute

You can apply MetadataTypeAttribute to your class providing another type with annotated properties. It may help you in the case when source type with its properties is defined in a code generated file so you don't want to put the attributes in there as they would get overwritten by the generator. Note that you have to set AttributedDestructuringPolicyOptions.RespectMetadataTypeAttribute to true.

var log = new LoggerConfiguration()
 .Destructure.UsingAttributes(x => x.RespectMetadataTypeAttribute = true)
 ...

9. Filter properties

You can define attribute derived from IPropertyFilterAttribute to filter out those properties that you do not want to be considered for destructuring. By default all properties that do not have any Destructurama attributes are just passed as is to the underlying serilog pipeline. In other words, they are "logged" somehow.

Destructurama provides AllowDestructuringOnlyExplicitlyMarkedPropertiesAttribute that allows a property to be considered for destructuring only if it is marked with one of Destructurama attributes. In the following example Age and Birthday properties will never be logged because they have no any attributes. All other properties will be destructured/logged according to their attributes.

This can be a convenient alternative for classes with dozens of properties where only a few need to be logged. In this case, there is no need to mark all other properties with [NotLogged].

[AllowDestructuringOnlyExplicitlyMarkedProperties]
public class Person
{
 [LogWithName("FullName")]
 public string? Name { get; set; }

 public int Age { get; set; }

 [NotLoggedIfNull]
 public string Username { get; set; }

 public DateTime Birthday { get; set; }

 [LogMasked(ShowFirst = 3)]
 public string? ShowFirstAndLastInvalidValues { get; set; }

 [LogReplaced(@"([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)", "***|$2")]
 public string Information { get; set; }
}

Benchmarks

The results are available here.

Product Versions Compatible and additional computed target framework versions.
.NET net5.0 net5.0 was computed.  net5.0-windows net5.0-windows was computed.  net6.0 net6.0 was computed.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 was computed.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 was computed.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
.NET Core netcoreapp2.0 netcoreapp2.0 was computed.  netcoreapp2.1 netcoreapp2.1 was computed.  netcoreapp2.2 netcoreapp2.2 was computed.  netcoreapp3.0 netcoreapp3.0 was computed.  netcoreapp3.1 netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 netstandard2.0 is compatible.  netstandard2.1 netstandard2.1 was computed. 
.NET Framework net461 net461 was computed.  net462 net462 was computed.  net463 net463 was computed.  net47 net47 was computed.  net471 net471 was computed.  net472 net472 was computed.  net48 net48 was computed.  net481 net481 was computed. 
MonoAndroid monoandroid monoandroid was computed. 
MonoMac monomac monomac was computed. 
MonoTouch monotouch monotouch was computed. 
Tizen tizen40 tizen40 was computed.  tizen60 tizen60 was computed. 
Xamarin.iOS xamarinios xamarinios was computed. 
Xamarin.Mac xamarinmac xamarinmac was computed. 
Xamarin.TVOS xamarintvos xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

NuGet packages (68)

Showing the top 5 NuGet packages that depend on Destructurama.Attributed:

Package Downloads
CG.SharedEntities

Package Description

Serilog.Extras.Attributed

Obsolete. Please install Destructurama.Attributed.

MyJetWallet.Sdk.Service

Package Description

Libplanet.Net

A peer-to-peer networking layer based on Libplanet.

MyJetWallet.Sdk.WalletApi

Package Description

GitHub repositories (4)

Showing the top 4 popular GitHub repositories that depend on Destructurama.Attributed:

Repository Stars
ErsatzTV/legacy
Open-source platform that transforms your personal media library into live, custom TV channels.
planetarium/libplanet
Blockchain in C#/.NET for on-chain, decentralized gaming
Texnomic/SecureDNS
Secure, Modern, Fully-Featured, All-In-One Cross-Architecture & Cross-Platform DNS Server Using .NET 10
GitTools/GitReleaseManager
Tool for creating and exporting releases for software applications hosted on GitHub
Version Downloads Last Updated
5.3.0 305,505 4/12/2026
5.2.0 651,076 1/11/2026
5.1.0 3,658,932 1/8/2025
5.0.1 157,182 1/3/2025
5.0.0 264,739 12/15/2024
4.1.0 411,528 12/15/2024
4.0.0 9,170,367 2/27/2024
3.2.0 1,374,241 1/24/2024
3.1.0 4,362,976 6/22/2023
3.1.0-dev-00177 324 6/23/2023
3.1.0-dev-00175 298 6/22/2023
3.1.0-dev-00173 318 6/22/2023
3.1.0-dev-00172 337 6/22/2023
3.0.0 10,231,010 9/27/2021
3.0.0-dev-00139 498 11/2/2021
3.0.0-dev-00138 561 10/30/2021
3.0.0-dev-00131 466 9/27/2021
3.0.0-dev-00129 596 9/15/2021
3.0.0-dev-00128 502 8/26/2021
3.0.0-dev-00127 454 8/25/2021
Loading failed