Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of NLog LogManager.GetLogger(String, Type) overload

Tags:

c#

nlog

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?

like image 411
Ilya Avatar asked Apr 07 '14 19:04

Ilya


1 Answers

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.

like image 116
wageoghe Avatar answered Nov 14 '22 22:11

wageoghe