2

We use different loggers to break up and apply specific rules to different kinds of log data. We typically specify which logger to be used with something like this:

var logger = Logger.GetLogger("MyLogger");

What is a recommended strategy on loggers?

  • Functional: For example, "Sales", "Administration", "Orders", etc.
  • Non-functional: For example, "Performance", "Caching", "Authentication", etc.
  • Layered: For example, "DAL", "Models", "Infrastructure", "BLL", etc.
  • Classes: For example, "OrderRepository", "OrdersController", etc.

I have often see the use of class names, but surely this is too granular?

1

It depends on what that string you're passing in actually does. For this answer, I'm going to assume that string is a "scope prefix", i.e. a string that simply gets printed out at the beginning of every log message output via this logger to make it clear where it comes from, because that's how every logger I've ever worked with did things (if it does do anything fancier, magic string values probably aren't the best way to implement that fancier thing).

Logs are important because one day, that one bug will happen on production to that one client and none of you devs will know how to reproduce it. Pretty much anything to do with logging should be designed around maximizing the chances you can quickly get a useful hint out of whatever your program logged, because that might be all you have.

So your logger's "scope prefix" should be as fine-grained and explicit as possible, preferably specifying the file or class it's been created in, not just the layer or department that file belongs to. Where I work, each file's logger is required to use the full filepath as its prefix (relative to the top of the package), and this has been very helpful for quickly pinning down where log output comes from in the heat of debugging.

Of course, that's assuming you have no silly technical constraints on logging like "only 80 characters per line; the rest get dropped" or "the server explodes if we log more than 100 characters per second". If you do, then would I begrudgingly consider using a prefix as coarse and terse as "DAL" or "BLL".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.