Seekatar.OptionToStringGenerator 0.1.2-prerelease

This is a prerelease version of Seekatar.OptionToStringGenerator.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Seekatar.OptionToStringGenerator --version 0.1.2-prerelease
                    
NuGet\Install-Package Seekatar.OptionToStringGenerator -Version 0.1.2-prerelease
                    
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.2-prerelease" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Seekatar.OptionToStringGenerator" Version="0.1.2-prerelease" />
                    
Directory.Packages.props
<PackageReference Include="Seekatar.OptionToStringGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Seekatar.OptionToStringGenerator --version 0.1.2-prerelease
                    
#r "nuget: Seekatar.OptionToStringGenerator, 0.1.2-prerelease"
                    
#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.
#:package Seekatar.OptionToStringGenerator@0.1.2-prerelease
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Seekatar.OptionToStringGenerator&version=0.1.2-prerelease&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Seekatar.OptionToStringGenerator&version=0.1.2-prerelease&prerelease
                    
Install as a Cake Tool

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 integration;
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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.5.89-prerelease 93 12/4/2024
0.3.5.88-prerelease 91 12/3/2024
0.3.5 14,760 12/5/2024
0.3.4 1,177 11/2/2024
0.3.3.82-prerelease 100 11/2/2024
0.3.3.77-prerelease 109 5/28/2024
0.3.3 31,495 5/28/2024
0.3.2.75-prerelease 113 5/27/2024
0.3.2 125 5/27/2024
0.3.1.72-prerelease 118 2/10/2024
0.3.1.71-prerelease 126 2/10/2024
0.3.1 247 2/10/2024
0.3.0.69-prerelease 130 1/31/2024
0.3.0.67-prerelease 118 1/13/2024
0.3.0 279 1/13/2024
0.3.0-prerelease 104 1/13/2024
0.2.3 194 1/1/2024
0.2.2 348 12/4/2023
0.2.1 36,983 11/14/2023
0.2.0 152 11/11/2023
0.1.4 336 10/12/2023
0.1.3 184 10/9/2023
0.1.2-prerelease 165 9/5/2023
0.1.1-prerelease 153 9/4/2023