serilog / serilog-settings-configuration

A Serilog configuration provider that reads from Microsoft.Extensions.Configuration
Apache License 2.0
446 stars 129 forks source link

Can't load appconfig.json when publishing single file #336

Closed bsdayo closed 1 year ago

bsdayo commented 1 year ago

I'm trying to load serilog configuation from my appconfig.json in ASP.NET Core, but if I publish the application with PublishSingleFile, the application can't load the configuation.

My Program.cs:

var builder = WebApplication.CreateBuilder(args);

// Load Serilog configuation
builder.Host.UseSerilog((context, config) => config
        .ReadFrom.Configuration(context.Configuration)
        .WriteTo.Console());

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.AddDatabases().AddServices();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseHttpLogging();
    app.UseSwagger();
    app.UseSwaggerUI();
    app.UseDeveloperExceptionPage();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

My appsettings.json:

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft.AspNetCore": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "./logs/log-.log",
          "rollingInterval": "Day"
        }
      }
    ]
  }
}

The problem is that, even if I set write to file in the config, the application don't create any log file. But when I disabled publishing single file ( -p:PublishSingleFile=false ), it can load the config file, and create log file as well.

I tried to print the config section:

Console.WriteLine(builder.Configuration.GetSection("Serilog:MinimumLevel:Default").Value);

It can print Information as well, so I think this problem is probably not related to ASP.NET Core.

Sorry for my bad English, and I'll be appreciate if anyone can help me!

sungam3r commented 1 year ago

https://github.com/serilog/serilog-settings-configuration#net-50-onwards-single-file-applications

bsdayo commented 1 year ago

I apologize for not reading the document carefully. Thanks for your help.