11

LogManager class has two methods: GetLogger and GetCurrentClassLogger, with an overload taking parameter Type loggerType

public static Logger GetLogger(string name, Type loggerType)
public static Logger GetCurrentClassLogger(Type loggerType)

Documentation states that loggerType is 'the type of the logger to create. The type must inherit from NLog.Logger.'

What is the purpose of such overloads? Why may I need to create loggers of inherited types?

Ilya
  • 21,871
  • 8
  • 73
  • 92

1 Answers1

10

You might create your own Logger as a subclass of the NLog Logger if you want to add some specific behavior to your Logger.

If you look at NLog's github repository here:

https://github.com/NLog/NLog/tree/master/examples/ExtendingLoggers/InheritFromLogger

You can see an example of how to extend NLog by subclassing Logger. In the case of the example, the new subclassed Logger (LoggerWithEventID) makes it easy to tag every logging statement with an "event ID". There are other ways to tag each statement with an event ID that don't involve subclassing, but this just shows that it is possible to implement such a thing.

These overloads allow a developer to develop his/her own custom Logger implementation and then have NLog create and dispense those custom Loggers without going through a lot of effort.

wageoghe
  • 27,390
  • 13
  • 88
  • 116