Seekatar.OptionToStringGenerator 0.1.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package Seekatar.OptionToStringGenerator --version 0.1.4                
NuGet\Install-Package Seekatar.OptionToStringGenerator -Version 0.1.4                
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="Seekatar.OptionToStringGenerator" Version="0.1.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Seekatar.OptionToStringGenerator --version 0.1.4                
#r "nuget: Seekatar.OptionToStringGenerator, 0.1.4"                
#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.
// Install Seekatar.OptionToStringGenerator as a Cake Addin
#addin nuget:?package=Seekatar.OptionToStringGenerator&version=0.1.4

// Install Seekatar.OptionToStringGenerator as a Cake Tool
#tool nuget:?package=Seekatar.OptionToStringGenerator&version=0.1.4                

OptionToString Incremental Source Generator

OptionToStringGenerator codecov

Problem: I have configuration class for use with IOptions and I want to safely log out its values at runtime.

Solution: Use an incremental source generator to generate an extension method to get a string with masked values for the properties.

This package generates an OptionToString extension method for your classes. By marking properties in the class you can control how the values are masked. It was created for dumping out objects used by IOptions or IConfiguration when the application starts.

Usage

  1. Add the OptionToString NuGet package to your project.
  2. Decorate a class with the OptionToStringAttribute attribute.
  3. Optionally decorate properties with how you want them to be you want to dump out. If you don't decorate a property, its full text is dumped out.

Example

Here's an example class of the various options with values set in the class for illustration purposes. The output follows.

namespace Test;
using Seekatar.OptionToStringGenerator;

[OptionsToString]
public class PublicOptions
{
    public class AClass
    {
        public string Name { get; set; } = "maybe this is secret";
        public override string ToString() => $"{nameof(AClass)}: {Name}";
    }

    public string PlainText { get; set; } = "hi mom";

    public int PlainNumber { get; set; } = 42;

    public DateTime PlainDateTime { get; set; } = new DateTime(2020, 1, 1);

    public string? NullItem { get; set; }

    public AClass AnObject { get; set; } = new();

    [OutputRegex(Regex = @"AClass\:\s+(.*)")]
    public AClass AMaskedObject { get; set; } = new();

    [OutputMask]
    public string FullyMasked { get; set; } = "thisisasecret";

    [OutputMask(PrefixLen=3)]
    public string FirstThreeNotMasked { get; set; } = "abc1233435667";

    [OutputMask(PrefixLen = 100)]
    public string NotMaskedSinceLongLength { get; set; } = "abc1233435667";

    [OutputLengthOnly]
    public string LengthOnly { get; set; } = "thisisasecretthatonlyshowsthelength";

    [OutputRegex(Regex="User Id=([^;]+).*Password=([^;]+)")]
    public string MaskUserAndPassword { get; set; } = "...;User Id=myUsername;Password=myPassword;";

    [OutputRegex(Regex="User Id=([^;]+).*Password=([^;]+)",IgnoreCase=true)]
    public string MaskUserAndPasswordIngoreCase { get; set; } = "...;user Id=myUsername;Password=myPassword;";

    [OutputRegex(Regex = "User Id=([^;]+).*Password=([^;]+)")]
    public string RegexNotMatched { get; set; } = "...;user Id=myUsername;Password=myPassword;";

    [OutputIgnore]
    public string IgnoreMe { get; set; } = "abc1233435667";
}
// usage
var options = new PublicOptions();
_logger.LogInformation(options.OptionToString());

The output has the class name followed by an indented list of all the properties' values masked as specified.

integration.PublicOptions:
  PlainText                     : "hi mom"
  PlainNumber                   : 42
  PlainDateTime                 : 1/1/2020 12:00:00 AM
  NullItem                      : null
  AnObject                      : AClass: maybe this is secret
  AMaskedObject                 : AClass: ***
  FullyMasked                   : "*************"
  FirstThreeNotMasked           : "abc**********"
  NotMaskedSinceLongLength      : "abc1233435667"
  LengthOnly                    : Len = 35
  MaskUserAndPassword           : ...;User Id=***;Password=***;
  MaskUserAndPasswordIngoreCase : ...;user Id=***;Password=***;
  RegexNotMatched               : ***!

Notes

  • All public properties are included by default and output as plain text.
  • Use the OutputIgnore attribute to exclude a property.
  • ToString() is called on the property's value, then the mask is applied. You can have a custom ToString() method on a class to format its output then it will be masked as the AClass example above.
  • Only one Output* attribute is allowed per property. If more than one is set, you'll get a compile warning, and the last attribute set will be used.
  • Regex strings with back slashes need to use a verbatim string or escape the back slashes (e.g. @"\s+" or "\\s+").
  • OutputRegex must have a Regex parameter, or you'll get a compile error.
  • If the regex doesn't match the value, the output will be ***! to indicate it didn't match.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.3.4 69 11/2/2024
0.3.3.82-prerelease 53 11/2/2024
0.3.3.77-prerelease 91 5/28/2024
0.3.3 5,279 5/28/2024
0.3.2.75-prerelease 86 5/27/2024
0.3.2 92 5/27/2024
0.3.1.72-prerelease 93 2/10/2024
0.3.1.71-prerelease 92 2/10/2024
0.3.1 202 2/10/2024
0.3.0.69-prerelease 97 1/31/2024
0.3.0.67-prerelease 97 1/13/2024
0.3.0 230 1/13/2024
0.3.0-prerelease 86 1/13/2024
0.2.3 164 1/1/2024
0.2.2 303 12/4/2023
0.2.1 27,321 11/14/2023
0.2.0 125 11/11/2023
0.1.4 309 10/12/2023
0.1.3 152 10/9/2023
0.1.2-prerelease 126 9/5/2023
0.1.1-prerelease 121 9/4/2023