我喜欢添加跟踪类中每个方法的进入和离开。并在发生异常时跟踪参数。使用 PostSharp AOP,我不需要将它放在每个方法中,只需应用属性即可。但我买不起 PostSharp。
是否有其他方法可以做到这一点? Unity3D 能做到吗?
我正在使用 log4net 来记录消息。
最佳答案
是的,您可以使用 Unity 进行面向方面的编程和拦截。
Unity is a lightweight, extensible dependency injection container with support for instance and type interception.
Dino Esposito 有一篇很棒的文章:Aspect-Oriented Programming, Interception and Unity 2.0
Interception in Unity 2.0
The core idea of interception in Unity is enabling developers to customize the chain of calls that it takes to invoke a method on an object. In other words, the Unity interception mechanism captures calls being made to configured objects and customizes the behavior of the target objects by adding some extra code before, after or around the regular execution of methods. Interception is essentially an extremely flexible approach to add new behavior to an object at run time without touching its source code and without affecting the behavior of classes in the same inheritance path.
还有一篇关于 Interception using Unity 的很棒的模式和实践文章. 文章中的示例代码使用拦截执行日志记录。
关于c# - 替代(免费)AOP PostSharp 用于方法跟踪(和异常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32150733/