merken / Prise

A .NET Plugin Framework.
https://merken.github.io/Prise
MIT License
361 stars 39 forks source link

How to inject ILogger<T> #49

Closed kapilbhavsar closed 2 years ago

kapilbhavsar commented 3 years ago

How can I inject type ILogger<MyService> in MyService class

merken commented 3 years ago

Hi,

I assume you're talking about injecting ILogger into a plugin or a service inside a plugin ? I would advise not to use ILogger, but to provide an abstraction that is defined in your Contract

Something like:

public interface IPluginLogger
{
void LogInformation(string message);
void LogError(string message);
}

In the Host you must provide the implementation like an Adapter pattern, meaning you wrap the ILogger inside the IPluginLogger (or MyPluginLogger)

Just remember to use ILogger instead of ILogger, you will lose some metadata, but this way, backwards compatibility is guaranteed.

There's an example of this in the docs at Host Services https://merken.github.io/Prise/#/?id=🎩-host-services