ASP.NET – Where to Place Logger in a Multi-Project Solution

asp.netloggingvisual studio

I have a Visual Studio solution with 4 projects, that closely follows the Nop-commerce structure. The following is the structure of my solution.

- Core
- Data
- Services
- Web

The Web project is an asp.net mvc application. Now I want to add server-side logging to my solution, using log4net. My question is which project is best candidate to have the reference of log4net in it, and why? As far as I have studied, logger should be in my Web project. But what if I wanted to log from one of the other three projects?

Best Answer

You will need to have a reference to your logging library (e.g. log4net) in all projects that contain any code that wants to log. In a lot of cases that will mean nearly all your projects have a reference to your logging library.

In addition you will likely need a reference in your "entry point" project (e.g. your web app) so that you can configure log sinks etc...

You could introduce abstractions to avoid referencing your logging library everywhere, e.g. raise events instead of logging, or introduce a logging interface in your Core project. In some cases this can be worth it (e.g. an open source project where users will likely have different logging libraries), however in most projects this is a bad idea and just adds complexity for little real-world gain.

Related Topic