views:

97

answers:

1

What is the best logging pattern for asp.net mvc 2 using log4net? When should I initialize logger how should I access logger instance?

+1  A: 

We follow the same pattern that we use for all apps:

Create a logger inside each class, setting the class type as the logger name and log everything that need to be logged inside the class to the local logger. Like this (for example... there are other ways to declare the logger):

private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

If you're so inclined you could try to weave a logging aspects throught your class, but sometimes you need finer grained control, which is why we go through the process of putting them in by hand.

TskTsk
thanks for the hint, what about creating a static LoggingService as pointed out on this blog: http://piotrwalat.net/?p=49 ?
adrin
If you do it that way, it all gets logged under one logger, so you have no idea in which part of the code the message got generated. For example, instead of knowing whether the message got logged in the action, the repository or the DAL, all you get is whatever message you added. It's a PITA to figure out where in the code the message got generated, because it's all under one logger. IMHO I would say this is the one way NOT to do it.
TskTsk