Search code examples
c#c#-4.0postsharpaop

How to exclude from OnMethodBoundaryAspect-based logging?


I have this logger:

[Serializable]
[AttributeUsage(AttributeTargets.All)]
public class MethodsInterceptAspect : OnMethodBoundaryAspect {
    public override void OnEntry(MethodExecutionArgs args) { Logger.LogMethodEntry(args.Method, DateTime.Now); }
    public override void OnExit(MethodExecutionArgs args) { Logger.LogMethodExit(args.Method, DateTime.Now); }
}

But there's an intensive function (many nested loops, performance-critical) that also bloats the log. How do I exclude it and all its subroutines ?


Solution

  • You can do this with the AttributeExclude=true property of the aspect. The exclusion can be applied at the assembly level

    [assembly: Trace("Business", AttributeTargetTypes="BusinessLayer.*", AttributePriority = 1)]
    [assembly: Trace(AttributeTargetMembers="Dispose", AttributeExclude = true, AttributePriority = 2)]
    

    or per method

    [assembly: Trace("Business", AttributeTargetTypes="BusinessLayer.*")]
    namespace BusinessLayer
    {
      public class Process : IDisposable
      {
       public Customer Create(string value) { ... }
       public void Delete(long id) { ... }
    
       [Trace(AttributeExclude=true)]
       public void Dispose() { ... }
      }
    }
    

    A more complete answer can be found here