Mid GoF Patterns

Handler Interface (Logger):?

  • The Logger class is the handler interface that defines the contract for

handling log messages. It includes a reference to the next logger in the chain

(NextLogger) and a method (LogMessage) to process a message. If the

current handler cannot handle the message, it passes the request along to

the next handler in the chain.

public abstract class Logger

protected Logger NextLogger;

public void SetNext(Logger nextLogger) => NextLogger =

nextLogger;

public void LogMessage(string message, LogLevel level)

if (CanHandle(level))

Handle(message);

else

NextLogger?.LogMessage(message, level);

protected abstract bool CanHandle(LogLevel level);

protected abstract void Handle(string message);

More from Design Patterns in C#

All questions for this course