如何在标有属性的目标上记录方法调用?

v00*_*d00 6 c#

是否可以将Loggin行为注入标记的类或/和方法,如下所示:

Log("Method {0} started",GetMethodNameTroughReflection)
Call method body
Log("Method {0} Finished",GetMethodNameTroughReflection)
Run Code Online (Sandbox Code Playgroud)

我想创建自己的Attribute类,它将实现方法调用的记录行为.

我想描述app.config文件中的登录行为,可以通过config中的设置禁用它.

怎么做对了?也许为这样的任务创建了解决方案?

Dir*_*mar 3

这可以使用面向方面编程(AOP)来完成。看看PostSharp。请参阅此处的跟踪示例:

非侵入式追踪和记录