c# - 替代(免费)AOP PostSharp 用于方法跟踪(和异常)

标签 c# trace

我喜欢添加跟踪类中每个方法的进入和离开。并在发生异常时跟踪参数。使用 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/

相关文章:

android - 如何在 Android 上使用 startMethodTracing API?

python - 跟踪python解释器的执行路径

c - 跟踪 Mhash 文件(散列)

c# - 如何利用 C# 属性和反射在标记对象上注入(inject)/强制后期绑定(bind)?

c# - Razor View 中不包含 'AsEnumerable' 和 'cannot convert from methodgroup' 的定义

c# - 如何从 ASP.NET Core 中的 .json 文件读取 AppSettings 值

c# - 将 Outlook 电子邮件的所有附件转换为 PDF

.net - 使用 Ent-Lib 拨号跟踪的意外后果,没有错误记录

delphi - 如何跟踪服务 Firebird

c# - 根据某些符号的子字符串