This package makes it possible to manipulate how objects are logged to Serilog using attributes.
Install from NuGet:
Install-Package Destructurama.AttributedModify logger configuration:
varlog=newLoggerConfiguration().Destructure.UsingAttributes()
...Apply the NotLogged attribute:
publicclassLoginCommand{publicstringUsername{get;set;}[NotLogged]publicstringPassword{get;set;}}When the object is passed using {@...} syntax the attributes will be consulted.
varcommand=newLoginCommand{Username="logged",Password="not logged"};log.Information("Logging in {@Command}",command);To prevent destructuring of a type or property at all, apply the [LoggedAsScalar] attribute.
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
publicclassCreditcard{/// <summary>/// 123456789 results in "*********"/// </summary>[LogMasked]publicstringDefaultMasked{get;set;}/// <summary>/// 123456789 results in "REMOVED"/// </summary>[LogMasked(Text="REMOVED")]publicstringCustomMasked{get;set;}/// <summary>/// 123456789 results in "123***"/// </summary>[LogMasked(ShowFirst=3)]publicstringShowFirstThreeThenDefaultMasked{get;set;}/// <summary>/// 123456789 results in "123******"/// </summary>[LogMasked(ShowFirst=3,PreserveLength=true)]publicstringShowFirstThreeThenDefaultMaskedPreserveLength{get;set;}/// <summary>/// 123456789 results in "***789"/// </summary>[LogMasked(ShowLast=3)]publicstringShowLastThreeThenDefaultMasked{get;set;}/// <summary>/// 123456789 results in "******789"/// </summary>[LogMasked(ShowLast=3,PreserveLength=true)]publicstringShowLastThreeThenDefaultMaskedPreserveLength{get;set;}/// <summary>/// 123456789 results in "123REMOVED"/// </summary>[LogMasked(Text="REMOVED",ShowFirst=3)]publicstringShowFirstThreeThenCustomMask{get;set;}/// <summary>/// 123456789 results in "REMOVED789"/// </summary>[LogMasked(Text="REMOVED",ShowLast=3)]publicstringShowLastThreeThenCustomMask{get;set;}/// <summary>/// 123456789 results in "******789"/// </summary>[LogMasked(ShowLast=3,PreserveLength=true)]publicstringShowLastThreeThenCustomMaskPreserveLength{get;set;}/// <summary>/// 123456789 results in "123******"/// </summary>[LogMasked(ShowFirst=3,PreserveLength=true)]publicstringShowFirstThreeThenCustomMaskPreserveLength{get;set;}/// <summary>/// 123456789 results in "123***789"/// </summary>[LogMasked(ShowFirst=3,ShowLast=3)]publicstringShowFirstAndLastThreeAndDefaultMaskeInTheMiddle{get;set;}/// <summary>/// 123456789 results in "123REMOVED789"/// </summary>[LogMasked(Text="REMOVED",ShowFirst=3,ShowLast=3)]publicstringShowFirstAndLastThreeAndCustomMaskInTheMiddle{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)]publicstringShowFirstAndLastThreeAndCustomMaskInTheMiddle{get;set;}}