With the help of AOP, I can remove the logging code from my business logic. But I think it can only be used to log simple things (i.e. logging method entry/exit and parameter values). However, what if I need to log something in my business logic? e.g.public void SomeDomainMethod(string id){   //Get user by Id   User user = Users.Get(id);   if (user == null)   {      Log.Warn(user is not existed);        //<----------------- Log A      throw new InvalidOperationException(user is not existed);   }   //Step 1    while(true)   {       //do something   }   Log.Info(Step 1 is completed);            //<----------------- Log B   //Step 2   while(true)   {       //do something   }   Log.Info(Step 2 is completed);            //<----------------- Log C}The above sample method may not be clear enough, what I want to show here is that the method should be treated as the smallest unit from the domain point of view. It shouldn't be divided into smaller pieces.Is it possible to move above 3 logging code out of the method? What's best practice for such situation?
Is it possible to keep logging code completely outside the business logic?
c#;logging;separation of concerns
Sure!But in my experience, there are two general types of useful logging:Everything logs: Logs built through profiling API's. Good for identifying performance issues and reporting exceptions. Very noisy.Business event logs: Logs invoked in business logic. Anything the business might care about. Minimal noise. Just notable, logical, business events. Good for auditing and KPI's...So, I would highly suggest two things. Firstly, do what other monitoring tools do, like New Relic, and use the .NET profiling API1. Secondly, log logical business events in your business logic. Keeping a record of certain events is business logic.And, I wouldn't normally suggest AOP for either sort of logging2. In my experience, you either want everything, which means you're using a profiler, or you you want logical/business events. And in the latter case, I think it's more straightforward to just invoke the logger in the business logic.1. But seriously, save yourself thousands of hours in effort and just use an existing profiler tool...2. Of course, this assumes that you share my opinion that an aspect isn't a great place to hide business rules!