Skip to content

Use attributes to control how complex types are logged to Serilog.

License

Notifications You must be signed in to change notification settings

sturlath/attributed

 
 

Repository files navigation

Destructurama.Attributed Build status

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

Enabling the module:

Install from NuGet:

Install-Package Destructurama.Attributed

Modify logger configuration:

var log = new LoggerConfiguration()
  .Destructure.UsingAttributes()
  ...

Ignoring a property

Apply the NotLogged attribute:

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

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

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

Treating types and properties as scalars

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

Masking a string property

Apply the LogMasked attribute with various settings:

  • Text: If set the properties 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.

Examples

public class Creditcard
{
  /// <summary>
  /// 123456789 results in "*********"
  /// </summary>
  [LogMasked]
  public string DefaultMasked { get; set; }
  
  /// <summary>
  ///  123456789 results in "REMOVED"
  /// </summary>
  [LogMasked(Text: "REMOVED")]
  public string CustomMasked { get; set; }
  
  /// <summary>
  ///  123456789 results in "123***"
  /// </summary>
  [LogMasked(ShowFirst: 3)]
  public string ShowFirstThreeThenDefaultMasked { get; set; }

  /// <summary>
  ///  123456789 results in "123******"
  /// </summary>
  [LogMasked(ShowFirst: 3, PreserveLength = true)]
  public string ShowFirstThreeThenDefaultMaskedPreserveLength { 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 ShowLastThreeThenDefaultMaskedPreserveLength  { get; set; }

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

  /// <summary>
  ///  123456789 results in "REMOVED789"
  /// </summary>
  [LogMasked(Text: "REMOVED", ShowLast: 3)]
  public string ShowLastThreeThenCustomMask { get; set; }
  
  /// <summary>
  ///  123456789 results in "******789"
  /// </summary>
  [LogMasked(ShowLast: 3, PreserveLength = true)]
  public string ShowLastThreeThenCustomMaskPreserveLength { get; set; }

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

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

  /// <summary>
  ///  123456789 results in "123REMOVED789"
  /// </summary>
  [LogMasked(Text: "REMOVED", ShowFirst: 3, ShowLast: 3)]
  public string ShowFirstAndLastThreeAndCustomMaskInTheMiddle { get; set; }

  /// <summary>
  ///  NOTE PreserveLength = true is ignored in this case
  ///  123456789 results in "123REMOVED789"
  /// </summary>
  [LogMasked(Text: "REMOVED", ShowFirst: 3, ShowLast: 3, PreserveLength = true)]
  public string ShowFirstAndLastThreeAndCustomMaskInTheMiddle { get; set; }
}

About

Use attributes to control how complex types are logged to Serilog.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 96.5%
  • PowerShell 3.5%