aspnetcore: When I set builder.Logging.ClearProviders();, dotnet run stuck in building

I create an Asp.Net Core Web Api 7.0 project and use Serilog. After I setting builder.Logging.ClearProviders(); in Program.cs, I can run my project with Visual Studio, but when I use CLI: dotnet run or dotnet watch run, my project stuck in Building. When I remove it, I can use dotnet run normally.

Below is my Program.cs:

var logger = new LoggerConfiguration().ReadFrom
.Configuration(builder.Configuration)
.Enrich
.FromLogContext()
.CreateLogger();

builder.Logging.ClearProviders();
builder.Logging.AddSerilog(logger);

My appsetting.json:

"Serilog": {
    "Using": [ "Serilog.Sinks.File" ],
    "MinimumLevel": {
      "Default": "Information"
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "../logs/webapi-.log",
          "rollingInterval": "Day",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {CorrelationId} {Level:u3} {Username} {Message:lj}{Exception}{NewLine}"
        }
      }
    ]
  }

And Nuget Packages:

<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />

When I run dotnet run: image

Has anyone encountered this kind of problem and can explain it?

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 25 (10 by maintainers)

Most upvoted comments

I don’t believe there’s a hang, it just that nothing is logged.

This is the output with logging on:

PS C:\Users\davifowl\source\repos\WebApplication2\WebApplication2> dotnet run
Building...
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5236
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Users\davifowl\source\repos\WebApplication2\WebApplication2
info: Microsoft.Hosting.Lifetime[0]
      Application is shutting down...

This is what happens when you clear the loggers:

PS C:\Users\davifowl\source\repos\WebApplication2\WebApplication2> dotnet run
Building...

There’s no “Build succeeded” log in either case, the application just runs. You removed the console logger so nothing gets logged to the console from the application.