本文介绍了替代(免费)到 AOP PostSharp 进行方法跟踪(和异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在类中添加跟踪每个方法的进入和离开.并在发生异常时跟踪参数.使用 PostSharp AOP,我不需要将它放在每个方法中,而只需应用属性.但我买不起 PostSharp.

I like to add tracing entering and leaving of every methods in classes. and also trace the arguments when an exception occurs. With PostSharp AOP, I don't need to put it in every method but just apply attributes. But I can't afford PostSharp.

有没有其他方法可以做到这一点?Unity3D 能做到吗?

Is there an alternate way to do this? Can Unity3D do that?

我正在使用 log4net 来记录消息.

I am using log4net to log the message.

推荐答案

是的,您可以使用 Unity 进行面向方面的编程和拦截.

Yes you can do Aspect Oriented Programming and Interception using Unity.

Unity 是一个轻量级、可扩展的依赖注入容器,具有支持实例和类型拦截.

Dino Esposito 有一篇很棒的文章:面向切面的编程、拦截和统一 2.0

There is a great article from Dino Esposito: Aspect-Oriented Programming, Interception and Unity 2.0

Unity 2.0 中的拦截

Unity 中拦截的核心思想是使开发人员能够自定义所需的调用链调用对象的方法.换句话说,统一拦截机制捕获对配置对象的调用并通过添加一些额外的东西来自定义目标对象的行为在方法的常规执行之前、之后或周围的代码.拦截本质上是一种非常灵活的方法来添加新的在运行时对对象的行为而不触及其源代码和不影响同一继承中类的行为路径.

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.

还有一篇关于 使用 Unity 进行拦截.文章中的示例代码使用拦截执行日志记录.

And also there is a great Patterns and Practices article about Interception using Unity.The example code at article performs logging using interception.

这篇关于替代(免费)到 AOP PostSharp 进行方法跟踪(和异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 00:32