.Net 6 how to register ILogger into IServiceCollection with implementation

Occasional Visitor

Hi
I am working on a .Net to .Net 6 migration application and want to register ILogger in IService Collection with implementation.


In the existing .Net application, the ILogger is resolved as follows using structure map

ForRequestedType<ILogger>().AsSingletons().TheDefault.Is.ConstructedBy(() =>
GetLoggerFactory().CreateLogger("InstanceName"));

 

private ILoggerFactory GetLoggerFactory()
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
.CreateLogger();
return new LoggerFactory()
.AddSerilog();
}

Now I want to register for ILogger on .Net 6. When I tried to register below, it didn't allow to register.

 

services.AddTransient<ILogger,GetLoggerFactory().CreateLogger("InstanceName")>();

It gave me an error as 'ILogger' is a type,which is not valid in the given context.

 

So, anyone could you please help us to register for ILogger ?

1 Reply

Hi @Uday_Mahajan,

Thanks for posting your issue here.

However this platform is used for how-to discussions and sharing best practices for building any app with .NET.Since your issue is a technical question, welcome to post it in Microsoft Q&A forum, the support team and communities on Microsoft Q&A will help you for any technical questions.
Besides, it will be appreciated if you can share it here once you post this technical question Microsoft Q&A.
Best Regards,
Lan Huang