这是我扩展 IMethodDecorator 接口的属性类的代码
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Field)]
public class LogAttribute : Attribute, IMethodDecorator
{
ILogger log = Logger.Factory.GetLogger<Logger>();
String methodName;
public LogAttribute() {
}
public void Init(object instance, MethodBase method, object[] args)
{
methodName = method.Name;
}
public void OnEntry()
{
Console.WriteLine(methodName);
log.Debug(methodName);
}
public void OnExit()
{
Console.WriteLine("Exiting Method");
}
public void OnException(Exception exception)
{
Console.WriteLine("Exception was thrown");
}
}
我希望能够将其用作
[log("some logmessage")]
void method()
{
// some code
}
有任何想法吗 ?我正在使用方法装饰器 Fody 包。