Close

How to enable Logger in TypeConverters ?

eliharel
4 years ago
#9851 Quote
Avatar
  • 3
Hello

for example 

How can I enable logging in the class : CustomerContactTypeConverter ? 

I want to be able to do : Logger.Error("Message"); but Logger isn't familier there.

Thank you for any help

iliyan.tanev
4 years ago
#9853 Quote
Avatar
  • Moderator
  • 347
Hi,

You need to resolve it.

There are two possible approaches here. 

1. You can resolve through the constructor of the class - like this:

private readonly ILogger _logger;
public CustomerContactTypeConverter(ILogger logger)
{
     _logger = logger;
}


2. You can resolve it through the EngineContext - like this:

var logger = EngineContext.Current.Resolve<ILogger>();


I think it will be more clean to do it using the constructor.

Hope this helps!
Regards,
Iliyan Tanev
Nop-Templates Dev Team
eliharel
4 years ago
#9862 Quote
Avatar
  • 3
Thank you very much ! Helped