1

I just want to know what would be best practice/ widely used, I currently do my logging in the domain service layer, however anything that happens inside my web application layer is not logged.

I would like one centralized and simple location to do all my create/delete/update loggging...

I have heard of Elmah, not sure how useful it is for domain service layer logging only...

I currently do not use any framework for logging at all, I just string builder my log message or get the exception and flush it into the database... Is this the best way to go about it?

If it matters... I need to use Ninject to inject in my ILoggingService

NOTE: I am not talking about Logging User Activity... that will definetly reside only inside my domain service layer...

2 Answers 2

2

Haroon,

Leverage Ninject to create and manage the lifetime of an ILoggingService. The implementation of that service should be built directly on top of a well tested logging library like NLog or log4net.

Once you have an instance of the service, you can easily inject it into either you MVC controller or your domain layer. All logging should happen against that instance, not a static logging class.

This will allow you to have the unified logging you are looking for, with a clean separation of concerns.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you feel it is better to log inside my asp.net mvc application or domain service?
imho different layers log different things. The MVC (user layer) might log all user interactions while the domain layer log logic errors and such.
0

imho logging should not be injected. The reason is that most of your services (if not all) will use logging.

If you look at most logging frameworks (like nlog), they are using a singleton/facade and abstract factories to provide logging.

Something like:

public static class LogFactory
{
    private static ILogFactory _instance;

    public void Assign(ILoggingFactory factory)
    {
         _instance = factory;
    }

    public ILogger CreateFor<T>()
    {
        return _instance.CreateFor<T>();
    }
}

The design makes your services only dependent of one class and one interface. Thus it's still extremely easy to switch logging implementations.

In your class use the code like:

public class ServiceImp : IService
{
      private ILogger _logger = LogFactory.CreateFor<IService>();

      public void SomeMethod()
      {
           _logger.Warning("Something went wrong, but we can handle it. Hence only a warning");
      }
}

2 Comments

If I was/ was not injecting my logging instance into my application=> I personally feel it would be better to use a base class, which contains a reference to my logging for all my services, do you feel the same?
No. I would not create a base class just to add support for logging (I strongly recommend against swiss army knife base classes, they break so many design principles). If you already have a base class, then it's fine to add a logger property to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.