There are no technical reasons why you shouldn't be able to pass the logger to the library class. For example, this is possible:
public class LoginLogoutServices
{
private ILogger<LoginLogoutServices> Logger { get; }
public LoginLogoutServices(ILogger<LoginLogoutServices> logger)
{
Logger = logger;
}
}
The only requirement would be for your services project to have a direct or indirect reference to the Microsoft.Extensions.Logging.Abstractions
NuGet package, where the definition of the ILogger<T>
interface lives.
With this in place, you should also be able to register the LoginLogoutServices
class in the IoC container in the Program.cs
class:
builder.Services.AddScoped<LoginLogoutServices>();
With this also in place, you should stop instantiating your LoginLogoutServices
class by "hand", and instead have it requested via the controller's constructor:
public class MyController : ApiController
{
private LoginLogoutServices LoginLogoutServices { get; }
public MyController(LoginLogoutServices loginLogoutServices)
{
LoginLogoutServices = loginLogoutServices;
}
}
Now you just use the LoginLogoutServices
property instead of local variables like the _loginService
you show in your original post.