Destructurama.Attributed 5.1.0

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:

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

snippet source | anchor

2. Ignoring a property

Apply the NotLogged attribute:

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

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

snippet source | anchor

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

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

snippet source | anchor

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

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; }
}

snippet source | anchor

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 property 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

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; }
}

snippet source | anchor

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)
  ...

Benchmarks

The results are available here.

No packages depend on Destructurama.Attributed.

.NET Standard 2.0

Version Downloads Last updated
5.1.0 27 01/09/2025
5.0.1 23 01/07/2025
5.0.0 26 12/15/2024
4.1.0 27 12/16/2024
4.0.0 44 03/04/2024
3.2.0 44 02/01/2024
3.1.0 51 07/08/2023
3.1.0-dev-00177 49 08/11/2023
3.1.0-dev-00175 43 08/09/2023
3.1.0-dev-00173 50 08/11/2023
3.1.0-dev-00172 50 08/17/2023
3.0.0 51 06/15/2022
3.0.0-dev-00139 45 03/06/2023
3.0.0-dev-00138 47 06/29/2023
3.0.0-dev-00131 49 10/29/2022
3.0.0-dev-00129 47 07/06/2023
3.0.0-dev-00128 45 07/04/2023
3.0.0-dev-00127 63 01/09/2023
3.0.0-dev-00060 43 07/01/2023
3.0.0-dev-00048 49 06/28/2023
2.0.1-dev-00037 51 05/08/2023
2.0.0 50 05/08/2023
2.0.0-dev-00033 54 06/30/2023
2.0.0-dev-00029 47 06/29/2023
2.0.0-dev-00028 46 06/29/2023
2.0.0-dev-00026 47 06/30/2023
1.2.0 53 11/23/2022
1.2.0-dev-00021 46 07/01/2023
1.2.0-dev-00019 51 10/19/2022
1.2.0-dev-00016 55 05/08/2023
1.2.0-dev-00013 50 05/08/2023
1.2.0-dev-00011 52 09/18/2022
1.1.0 45 05/31/2023
1.1.0-dev-00010 48 05/08/2023
1.1.0-dev-00007 42 11/02/2022
1.1.0-dev-00004 52 06/28/2023
1.1.0-dev-00001 40 05/08/2023
1.0.7 53 09/29/2022
1.0.6 42 05/08/2023
1.0.5 47 06/29/2023
1.0.4 45 06/27/2023
1.0.3 47 05/08/2023
1.0.2 40 05/08/2023