3

I used Fody Nuget package as follows

  1. Install package

    PM> Install-Package MethodDecorator.Fody
    
  2. Decorate the method

    public class BusinessLayerClass
    {
        [LogMethod] 
        public string BusinessLayerMethod()
        {
            DataLayerClass dataLayerClass = new DataLayerClass();
           return  dataLayerClass.DataLayerMethod();
        }
    }
    
  3. Write the interceptor

    using System;
    using System.Reflection;
    
    [module: LogMethod] // Attribute should be "registered" by adding as module or assembly custom attribute
    
    // Any attribute which provides OnEntry/OnExit/OnException with proper args
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Assembly | AttributeTargets.Module)]
    public class LogMethodAttribute : Attribute, IMethodDecorator
    {
        private MethodBase _method;
    
        // instance, method and args can be captured here and stored in attribute instance fields
        // for future usage in OnEntry/OnExit/OnException
    
        public void Init(object instance, MethodBase method, object[] args)
        {
            _method = method;
        }
    
        public void OnEntry()
        {
            NLogging.Trace("Entering into {0}", _method.Name);
        }
    
        public void OnExit()
        {
            NLogging.Trace("Exiting into {0}", _method.Name);
        }
    
        public void OnException(Exception exception)
        {
            NLogging.Trace(exception, "Exception {0}", _method.Name);
        }
    }
    

This works fine within the same project but when I use the decorator [LogMethod] in another method in another project, this OnEntry(), OnExit(), OnException(Exception exception) methods do not fire.

For instance:

[LogMethod]
public void Another_Method_In_Seperate_Project()

I added a reference to the project where [LogMethod] is defined.

Could anyone please send me a way of using the same implementation in other projects without doing the implementation of LogMethodAttribute.cs (where this [LogMethod] is defined) in each and every project.

Matze
  • 5,100
  • 6
  • 46
  • 69
Tom
  • 175
  • 1
  • 14

2 Answers2

3

You need to install MethodDecorator.Fody nuget package in the other project as well (along with dependencies). You will also need to add another FodyWeavers.xml in that project.

H.D.
  • 455
  • 5
  • 19
1

In addition to the answer by H.D. I recommend looking at this answer, from another post: https://stackoverflow.com/a/61503942/13081132

Edit: The answer I have linked to simply reminds the author to "register" the attribute inside the file where the attributes is being used.

Example:

[module: LogMethod] // In my case this was nessesary for all files using the attribute

public class BusinessLayerClass
{
    [LogMethod] 
    public string BusinessLayerMethod()
    {
        DataLayerClass dataLayerClass = new DataLayerClass();
       return  dataLayerClass.DataLayerMethod();
    }
}
CarRonne
  • 51
  • 4