I used Fody Nuget package as follows
Install package
PM> Install-Package MethodDecorator.Fody
Decorate the method
public class BusinessLayerClass{ [LogMethod] public string BusinessLayerMethod() { DataLayerClass dataLayerClass = new DataLayerClass(); return dataLayerClass.DataLayerMethod(); }}
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.