Code Monkey home page Code Monkey logo

attributed's Introduction

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 [LoggedAsScalar] 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; }
}

Masking a string property with Regular Expressions

Apply the LogReplaced attribute on a string, in which you want to apply a RegEx replacement during Logging.

This is applicable in scenarios which a string contains both Sensitive and Non-Sensitive information. An example of this could be a string such as "Sensitive|NonSensitive". Then you can apply the attibute 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 two constructors:

LogReplaced(string pattern, string replacement)

LogReplaced(string pattern, string replacement, RegexOptions regexOptions)
  • Pattern: The pattern that should be applied on value..
  • Replacement: The string that will be applied by RegEx.
  • RegexOptions: The RegexOptions that will be applied. Defaults to RegexOptions.None

Examples

public class CreditCard
{
  const string RegexWithVerticalBars = @"([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)";
  
  /// <summary>
  /// 123|456|789 results in "***|456|789"
  /// </summary>
  [LogReplaced(RegexWithVerticalBars, "***|$2|$3")]
  public string RegexReplaceFirst { get; set; }

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.