How to exclude some methods in Fody when using MethodBoundaryAspect

515 views Asked by At

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));
            
}
1

There are 1 answers

0
charlie.mtp On

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