Logging entry and exit of methods along with parameters automagically?

前端 未结 7 586
遇见更好的自我
遇见更好的自我 2021-01-02 04:31

Is there a way for me to add logging so that entering and exiting methods gets logged along with parameters automatically somehow for tracing purposes? How would I do so?

7条回答
  •  天涯浪人
    2021-01-02 05:00

    The best way to achieve this sort of thing is by using interception, There are a couple of ways to do this, though they all tend to be somewhat invasive. One would be to derive all your objects from ContextBoundObject. Here is an example of using this sort of approach. The other approach would be to use one of the existing AOP libraries to achieve this. Something like DynamicProxy from the Castle Project is at the core of many of these. Here are a few links: Spring.Net PostSharp Cecil

    There are probably several others, and I know Castle Windsor, and Ninject both provide AOP capabilities on top of the IoC functionality.

    Once AOP is in place you would simply write an interceptor class that would write the information about the method calls out to log4net.

    I actually wouldn't be surprised if one of the AOP frameworks would give you that sort of functionality out of the box.

提交回复
热议问题