GetRequiredService throw "The logger is already frozen"

See original GitHub issue

Description in the project using Swashbuckle.AspNetCore, I try to use CreateBootstrapLogger to build a logger for Web API, but it cannot startup and throw the exception The logger is already frozen.

Reproduction image

image

Expected behavior Expected not throw exception

Relevant package, tooling and runtime versions netcoreapp3.1 Serilog.AspNetCore 4.1.0 Swashbuckle.AspNetCore 6.1.4

Additional context no

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
sanderaernoutscommented, Feb 13, 2023

I know this is an older issue but passing preserveStaticLogger: true to .UseSerilog(..) solved the issue for me. I have not found a way to get rid of my BuildServiceProvider() call, so this seems to be a way around the System.InvalidOperationException: The logger is already frozen

Host.CreateDefaultBuilder(args)
  .UseSerilog(
      (context, services, configuration) => configuration
          .ReadFrom.Configuration(context.Configuration)
          .ReadFrom.Services(services)
          .Enrich.FromLogContext()
          .WriteTo.Console(),
      true
  )
7reactions
skomis-mmcommented, Jun 6, 2021

Hi @benlinx1018 ,

Building IServiceProvider from the IServiceCollection is not a recommended pattern because it can result in multiple constructions and undesired side effects. Use this one instead

services.AddOptions<SwaggerGenOptions>()
    .Configure<IApiVersionDescriptionProvider>((options, provider) =>
    {
        foreach (var avd in provider.ApiVersionDescriptions)
        {
            options.SwaggerDoc(avd.GroupName, CreateMetaInfoApiVersion(avd));
        }
    });

services.AddSwaggerGen();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Serilog logger freezes on every endpoint call
An unhandled exception has occurred while executing the request. System.InvalidOperationException: The logger is already frozen. at Serilog.
Read more >
Supporting integration tests with WebApplicationFactory in ...
This will block the test code until one of three things happen: The web application throws an exception, which invokes the EntryPointCompleted() ...
Read more >
What's new in .NET 8
MakeReadOnly() gives you explicit control over when a JsonSerializerOptions instance is frozen. (You can also check it with the IsReadOnly ...
Read more >
NET 6 Web API with SQLite Database - Sean Spaniel
We need to write a unit test that verifies UserManager will actually log any exception thrown by IUserRepository. For this test, we'll need...
Read more >
C# (CSharp) ServiceEndpoint Examples
MinimumRequiredOpenIdVersion) { throw new OpenIdException(string.Format(CultureInfo. ... null) { if (!Args.ContainsKey(Protocol.openidnp.ns)) { Logger.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found