-
-
Notifications
You must be signed in to change notification settings - Fork 158
Can't use Serilog bootstrap logger with JADNC #1082
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
Comments
Hi @Lazyboy1, thanks for reporting this and providing such a great repro. I've always expected that our intermediate service provider would bite us someday... The reason we're doing that is two-fold. The first reason is that we're logging some warnings during initialization. This can be fixed by enqueuing those messages and flush them to the logger at a later time. The second reason is more problematic, unfortunately. We need a The most reliable/flexible way to obtain a What I found using your repro, is that EF Core obtains a logger from the service container, which triggers the exception in Serilog. After eliminating the first reason mentioned above, combined with preventing EF Core to ask for a logger, the exception disappears, and "Hello World!" appears in the browser. services.AddDbContext<MyDbContext>(builder =>
{
builder.UseInMemoryDatabase("db");
builder.UseLoggerFactory(NullLoggerFactory.Instance);
}); That's the best I can offer, short-term. But this means you won't have any EF Core logging. To fix that, instead of passing a null logger, you may be able to implement a custom factory that somehow waits for Serilog to have initialized before flushing its queue of messages. Or not use the bootstrap logger at all. I hope that helps a bit. I'm open to suggestions, so if you have any other ideas of how to solve this, please let me know. |
@Lazyboy1 Did you get a chance to try the above? |
Unfortunately not, and it's OK that we close the issue for now. I get that it's not an easy problem to solve. The easy way around it for us is to not use Serilog's It's also possible to try to get Serilog to be more tolerant in this case. The reason I opened the issue in the first place is because I think Serilog is so widely used, we would want JADNC to be compatible with its best practice, but maybe they are the ones who should be more careful not to break the ecosystem :) |
Thanks for understanding and for reporting this. I'm closing this issue, we can revisit if things change. Users should still be able to find this using an issue search. |
…s considered an anti-pattern, leading to occasional compatibility issues. This unblocks use in [Aspire](https://learn.microsoft.com/en-us/dotnet/aspire/) (more specifically, its usage of `AddNpgsqlDataSource`, which throws an `ObjectDisposedException`) and fixes #1082.
…s considered an anti-pattern, leading to occasional compatibility issues. This unblocks use in [Aspire](https://learn.microsoft.com/en-us/dotnet/aspire/) (more specifically, its usage of `AddNpgsqlDataSource`, which throws an `ObjectDisposedException`) and fixes #1082.
…s considered an anti-pattern, leading to occasional compatibility issues. This unblocks use in [Aspire](https://learn.microsoft.com/en-us/dotnet/aspire/) (more specifically, its usage of `AddNpgsqlDataSource`, which throws an `ObjectDisposedException`) and fixes #1082.
…s considered an anti-pattern, leading to occasional compatibility issues. (#1431) This unblocks use in [Aspire](https://learn.microsoft.com/en-us/dotnet/aspire/) (more specifically, its usage of `AddNpgsqlDataSource`, which throws an `ObjectDisposedException`) and fixes #1082.
DESCRIPTION
When trying to implement two-stage logger initialization with Serilog, an exception is thrown:
Exception is not thrown when removing
services.AddJsonApi()
call. It seems this is happening because the callback toUseSerilog
is invoked twice: once when building the ASP.NET host, and once when building an intermediateServiceProvider
inJsonApiApplicationBuilder
ctor. as far as I understand, it is recommended to avoid buildingServiceProvider
during DI services registration.STEPS TO REPRODUCE
I created a repro: https://github.com/lazyboy1/JsonApiSerilogBootstrapLoggerRepro. Run the project to see the exception.
EXPECTED BEHAVIOR
Exception should not be thrown, and app should start with both the recommended Serilog configuration and JADNC.
ACTUAL BEHAVIOR
Exception is thrown.
VERSIONS USED
The text was updated successfully, but these errors were encountered: