Quantcast
Channel: Active questions tagged nuget-package - Stack Overflow
Viewing all articles
Browse latest Browse all 3067

How to use MethodDecorator.Fody Decorator in another project

$
0
0

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.


Viewing all articles
Browse latest Browse all 3067

Trending Articles