1

I am using Fody to log every method "OnExit". It looks like this:

[CustomLog(AttributeTargetMemberAttributes = MulticastAttributes.Public)]
public class Driver
{

    public static void Close()
    public static void Quit()
    public static void DeleteCookies()
    public static void Navigate(string url)

}

The class for the CustomLog looks like this:

public sealed class CustomLogAttribute : OnMethodBoundaryAspect

I want to exclude the method DeleteCookies from logging in the Driver class. Eventually, I want to have the decision to exclude any other method. I worked before with PostSharp and they have this code:

[CustomLog(AttributeExclude=true)]

But I have not been able to find something similar in Fody. I appreciate any help.

Update: I was able to find a solution thanks to Kirk Woll. Now I am able to read the attributes from methods but not properties.

public class DoNotLog : Attribute
{
    public DoNotLog()
    {

    }
}
[DoNotLog]
public static IWebDriver Current {get;}
public override void OnExit(MethodExecutionArgs args)
{
    // Avoid logging the methods marked as [DoNotLog]
    IEnumerable<CustomAttributeData> attributes = args.Method.CustomAttributes;
    var result = args.Method.GetCustomAttribute(typeof(DoNotLog));
            
}
  • I'm confused as your `OnMethodBoundaryAspect` is apparently a PostSharp class, which you said you weren't using. Did you roll your own version? – Kirk Woll Apr 16 '22 at 14:28
  • @KirkWoll , Fody implements the same class name. – charlie.mtp Apr 16 '22 at 18:18
  • If that's true, the class name doesn't appear in their [repository](https://github.com/Fody/Fody/search?q=OnMethodBoundaryAspect). – Kirk Woll Apr 16 '22 at 18:48
  • My bad, this is part of Fody but I am using the weaver [MethodBoundaryAspect.Fody](https://github.com/vescon/MethodBoundaryAspect.Fody) – charlie.mtp Apr 18 '22 at 12:48
  • 1
    It looks like the `OnEnter` and other methods you override in `CustomLog` provides a `MethodExecutionArgs` class which contains a property `Method` that you can use to discriminate whether or not to exclude the logging. You could extend that by creating your own attribute that you can decorate on methods and interrorage that `Method` property for the presence of tha attribute. – Kirk Woll Apr 18 '22 at 14:53
  • 1
    You are right @KirkWoll. That solves my problem! Thank you – charlie.mtp Apr 18 '22 at 18:32
  • Glad I could help. Feel free to self-answer your question. – Kirk Woll Apr 18 '22 at 19:39
  • One more challenge ... I am able to read the attribute from methods, but not from the Properties. Any idea? Example code: `[DoNotLog] public static IWebElement Current` This code does not recognize the attributes of the properties but it works for methods: `IEnumerable attributes = args.Method.CustomAttributes; var result = args.Method.GetCustomAttribute(typeof(DoNotLog));` – charlie.mtp Apr 18 '22 at 19:56
  • 1
    The problem there is that the `get;` and `set;` in properties are actual methods and you're (understandably) applying the attribute to the enclosing `PropertyInfo`. You have two options: apply the attribute directly to the setter (such as `IWebElement Current { get; [DoNotLog] set; }` or figure out how to map the get/set methods back to the `PropertyInfo` yourself. The answer [here](https://stackoverflow.com/a/520155/189950) helped me out in a similar situation. – Kirk Woll Apr 18 '22 at 20:18
  • 1
    That worked like a charm. Thank you @KirkWoll . Adding the answer! – charlie.mtp Apr 18 '22 at 20:26

1 Answers1

1

Like @KirkWoll suggested, the solution is first to use MethodExecutionArgs to filter based on the property that I should create.

public override void OnExit(MethodExecutionArgs args)
{
    // Avoid logging the methods marked as [DoNotLog]
    IEnumerable<CustomAttributeData> attributes = args.Method.CustomAttributes;
    var result = args.Method.GetCustomAttribute(typeof(DoNotLog));            
}

And for the properties, the attribute should be added to the GET or SET and not on the Property