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?
Best Answer
Congratulations, you have (re-)discovered the Object Adapter Pattern. You have one target interface (here:
ILogger
), and various adapter classesNLogPlatform
,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.