tags:

views:

69

answers:

2

Hi

I'm wondering about where I should place logging code in my C# application.

Let's say I want to log the program arguments (like myapp.exe -arg1).

The arguments are passed from the entry-point class with main() before it reaches a simple factory class that parses the arguments (actually there is only one argument) and creates an object based on the argument.

Should the factory that parses the arguments (the bottom of the hierarchy) be responsible for logging the arguments or should I log the arguments in main (the top of the hierarchy).

I feel the factory class would be the right place, but I'm often in doubt about where the right place for logging code is, and would really appreciate some good best practices I can reuse.

+2  A: 

I think you should log inside the factory.

If you migrate your code later to be used in another enviroment, the main() might not exist. And all main() is doing is passing on this information. So if you create an ASP application , and want to keep the same logging details, then the factory method would be usefull, since the factory makes the decision on what gets created based on the argument.

Heiko Hatzfeld
+1  A: 

As a custom rule I always try to log things as down the callstack as I can while still has sense to do the log (to avoid logging small methods that are called too often). If you loose some semantics because you are logging too down you can always add extended log information a bit upper.

SoMoS