destructurama / attributed

Use attributes to control how complex types are logged to Serilog.
Apache License 2.0
269 stars 33 forks source link

Add support for strings of listed properties to exclude or mask #29

Closed daiplusplus closed 9 months ago

daiplusplus commented 5 years ago

Related to https://github.com/destructurama/attributed/issues/28, I'd like it if I could manually list the types and members that I want excluded or masked - this also helps when consuming types from other libraries that handle or contain sensitive data but which cannot be annotated with Destructurama's attributes.

One example would be to allow the use of nameof() to specify members to ignore or mask. This could be done by accepting a fully-qualified type and member path, e.g.:

var log = new LoggerConfiguration()
    .Destructure.UsingMemberPaths( o => {
        o.NotLogged( "FooNamespace.BarTypeName.PropertyName" );
        o.NotLogged( "FooNamespace.BarTypeName+NestedType.PropertyName" );
    } );

Programmer ergonomics and type-safety could be improved by using generics to specify the type and using nameof() with a dummy instance lambda to get the property name:

void NotLogged<T>(Func<T,String> getPropertyName)
{
    String typeName = typeof(T).FullName;
    String propertyName = getPropertyName( default(T) );
    this.NotLogged( typeName + "." + propertyName );
}

var log = new LoggerConfiguration()
    .Destructure.UsingMemberPaths( o => {
        o.NotLogged<FooNamespace.BarTypeName>( i => nameof(i.PropertyName) )
    } );

Other function parameters (with default values or overloads to reduce noise) could be used to specify LogMasked options:

var log = new LoggerConfiguration()
    .Destructure.UsingMemberPaths( o => {
        o.NotLogged<UserEditViewModel>( i => nameof(i.Password), showFirst: 1, preserveLength; true )
        o.NotLogged<UserEditViewModel>( i => nameof(i.ConfirmPassword), showLast: 1 )
    } );
github-actions[bot] commented 9 months ago

This issue was marked as stale since it has not been active for a long time

sungam3r commented 9 months ago

@daiplusplus I understand your point. What you are talking about is a generalized approach to obtaining information on how to destructure objects. The problem is that the whole project is intended specifically for working with attributes and to transmit settings through attributes. Look - you use UsingMemberPaths method. There is no any notice about any attributes - just generic settings provided via some fluent APIs. This is not bad in itself. Just please understand me correctly - this will turn out a completely different project.