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?
I am using Log4Net.
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?
I am using Log4Net.
Functions
To expand on Jared's answer, avoiding code repetition and including options for arguments:
Usage
Output
In the console, this would be the output if
DoSomething
were run with "blah" asarg1
The Decorator Pattern may help.
You could use a post-compiler like Postsharp. The sample from the website talks about setting up a tracer for entering/exiting a method, which is very similar to what you want.
I'm not sure what your actual needs are, but here's a low-rent option. It's not exactly "automatic", but you could use StackTrace to peel off the information you're looking for in a manner that wouldn't demand passing arguments - similar to ckramer's suggestion regarding interception:
You could combine something like the above example with inheritance, using a non-virtual public member in the base type to signify the action method, then calling a virtual member to actually do the work:
Again - there's not much "automatic" about this, but it would work on a limited basis if you didn't require instrumentation on every single method invocation.
Hope this helps.
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.
Have a look at:
How do I intercept a method call in C#? PostSharp - il weaving - thoughts
Also search SO for 'AOP' or 'Aspect Oriented Programming' and PostSharp...you get some interesting results.