-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Logging from additional containers #15
Comments
I found solution for Serilog, but I can't solve this problem for your library. |
Hi there! This is a DI configuration problem, not an issue with the library. When you build two containers (service providers) from the same When Having multiple instances of singleton services may lead to unexpected behavior. Not only in the case of file logger but other services, so I strongly advise against this. If you inevitably need multiple containers, I suggest that you use a DI implementation which supports the concept of nested scopes/registrations (like Autofac or DryIoc) and register your singleton services in the root scope. For the sake of completeness it's worth mentioning that you can workaround this particular issue using MS DI. It works but feels a bit hacky to me: var services = new ServiceCollection();
services.AddLogging(builder =>
{
builder.AddConfiguration(_configuration.GetSection("Logging"));
builder.AddFile(o => o.RootPath = AppContext.BaseDirectory);
});
// add your other services...
var provider1 = services.BuildServiceProvider();
// replace the ILoggerProvider registration for FileLoggerProvider with the instance acquired from the first container (provider1),
// this way the second container (provider2) will reuse that instance instead of creating another one;
// just one thing to keep in mind: the second container must be disposed before the first one to avoid lifetime issues
var index = FindServiceIndex<ILoggerProvider, FileLoggerProvider>(services);
var fileLoggerProvider = provider1.GetRequiredService<IEnumerable<ILoggerProvider>>().OfType<FileLoggerProvider>().FirstOrDefault();
services[index] = ServiceDescriptor.Singleton<ILoggerProvider>(fileLoggerProvider);
var provider2 = services.BuildServiceProvider();
static int FindServiceIndex<TService, TImplementation>(ServiceCollection services)
where TService : class
where TImplementation : class, TService
{
for (var i = services.Count - 1; i >= 0; i--)
{
var service = services[i];
if (service.ServiceType == typeof(TService) &&
(service.ImplementationType == typeof(TImplementation) ||
service.ImplementationInstance?.GetType() == typeof(TImplementation) ||
service.ImplementationFactory?.Method.ReturnType == typeof(TImplementation)))
{
return i;
}
}
return -1;
} |
Hello. In my app i need to create additional containers for creating two instances of Rebus.
When I use the same in ConfigureServices((hostContext, services) it works great, but when I use it in ServiceCollection it doesn't write to file. Do you have any ideas? Where did I make mistake?
There is my config file:
The text was updated successfully, but these errors were encountered: