6

I need to create a simple system to abstract logging platforms. The idea is that I'd like to be able to exchange or activate a logging platform (nlog, log4net, enterprise library) via some configuration (this shouldn't be a problem). I know that I would need a wrapper for each platform that I want to support that would load the necessary dll's and create the loggers.

I was thinking of creating an abstract class or an interface providing the most common methods like Log, Debug, Trace, etc.:

interface ILogger
{
    void Log(...);
    void Debug(...);
    // etc.
}

class NLogPlatform : ILogger
{
    // create nlog, load config etc.
}

class Log4NetPlatform : ILogger
{
    // create log4net, load config etc.
}

class LoggerFactory
{ 
    public static CreateLogger(...)
    {
        // read settings and create a logger.
    }
}

At first I thought of a log-provider but then I found that provider is not a pattern at all so I looked at .NET Design Patterns but nothing really seems to be suitable here. Did I miss something or is there just no pattern for it?

I haven't really started yet because this time I'd rather go in the right direction from the beginning then refactor several tools later.

Could you tell me what I should take into consideration when designing such a system and whether there is a pattern for this?

t3chb0t
  • 2,602

3 Answers3

7

Congratulations, you have (re-)discovered the Object Adapter Pattern. You have one target interface (here: ILogger), and various adapter classes NLogPlatform, Log4NetPlatform, …. Each adapter class conforms to the target interface, and proxies all calls to the adaptee object which it contains as a member.

If you want to make the use of this pattern clearer, I would call the adapter classes NLogAdapter etc..

The usage of a factory to inject the correct log adapter is entirely orthogonal to this, and is about the Dependency Inversion principle.

amon
  • 135,795
3
  1. This has nothing to do with design patterns...

  2. This is a solved problem, that has been solved over and over since .Net was invented.

  3. Just use Commons.Logging it's a small configurable liberality that does exactly what you asked: https://github.com/net-commons/common-logging

AK_
  • 744
0

This is nothing to do it design pattern, you can just use the build-in TraceSource to log anything you want.