I am using postsharps OnMethodBoundaryAspect. Overriding on entry in this example so I can log the parameters for tracing. I have a requirement to mask potentially personally identifiable information.
Using the code below, I can do that by scanning those properties I have marked with "PII" attribute. However, the problem is for some reason, I can't create a copy of the Arguments object. I've even tried using the "Clone" method of Arguments but it still gets changed whenever I use the SetValue to update the property.
Is there something I'm missing here?
public override void OnEntry(MethodExecutionArgs args){
int count = args.Arguments.Count();
object[] argsCopy = new object[count];
args.Arguments.CopyTo(argsCopy, 0);
foreach(var arg in argsCopy)
{
MaskPiiDecorated(arg);
}
WriteEvent((int)EventId.Info, argsCopy.ToArray());
}
private object MaskPiiDecorated(object arg)
{
//Gets all properties
IEnumerable<PropertyInfo> topProperties = arg.GetType().GetProperties();
//Gets sub properties that could have properties
IEnumerable<PropertyInfo> nonValueTypeSubProperties = topProperties.Where(
prop => !prop.PropertyType.IsValueType
&& prop.PropertyType != typeof(string)
&& prop.PropertyType.BaseType != typeof(Array));
//Recursion for sub properties
nonValueTypeSubProperties.ToList().ForEach(
subProperty => { MaskPiiDecorated(subProperty.GetValue(arg)); });
topProperties.Where(prop => prop.IsDefined(typeof(PiiAttribute), false))
.ToList().ForEach(decor => { decor.SetValue(arg, "***REMOVED****"); });
return arg;
}