I wrote a custom logger using the ILoggingBuilder
and the ILoggerProvider
pattern as describe in the .net documentation
My logger needs a service build in the app, I was able to pass the service using the ServiceProvider
but unfortunetly the instance is duplicated (even if the service is declared as a singleton)
Here is a sample of the code :
return Host.CreateDefaultBuilder(args)
.ConfigureHostConfiguration(configurationBuilder =>
{
if (args is not null)
{
_ = configurationBuilder.AddCommandLine(args);
}
})
.ConfigureAppConfiguration((context, builder) =>
builder.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: true))
.ConfigureServices((hostContext, services) =>
{
services.AddServices();
serviceProvider = services.BuildServiceProvider();
}
)
.ConfigureLogging((hostContext, loggingFactory) =>
{
loggingFactory
.AddConsole()
.AddEventLog()
.AddFile("Logs/worker-{Date}.txt")
.AddCustomFormatter(serviceProvider.GetService<IMycustomService>());
});
I've tried many approches but I was not able to use the same instance in my servicesColletion and in my custom logger
Any hep would be greatly appreciated