-
Notifications
You must be signed in to change notification settings - Fork 131
Closed
Labels
Description
Description
ReadFrom.Configuration is not able to use configuration parameters from the appsettings.json file. It does not write the expected format on the console, it does not generate the expected log file.
this is my appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
},
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": "Debug",
"Override": {
"Microsoft.AspNetCore": "Warning",
"Microsoft": "Warning",
"System": "Warning"
},
"Enrich": [ "FromLogContext" ],
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": {
// `type` (or $type) is optional, must be specified for abstract declared parameter types
"type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
"template": "[{@t:HH:mm:ss} {@l:u3} ---- {Coalesce(SourceContext, '<none>')}] {@m}\n{@x}"
}
}
},
{
"Name": "File",
"Args": {
"path": "\\logs\\log.txt"
}
}
]
}
}
the appsettings class:
public class AppSettings
{
private static AppSettings? _instance;
private static IConfiguration _configuration;
public static AppSettings Instance
{
get
{
if (_instance == null)
{
_instance = new AppSettings();
}
return _instance;
}
}
public IConfiguration Configuration
{
get
{
return _configuration;
}
}
public AppSettings()
{
string OS = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "Linux" : "Windows";
_configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile($"appsettings.{OS}.json")
.AddEnvironmentVariables()
.Build();
}
public static void Reload()
{
_instance = new AppSettings();
}
}`
the main:
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddConfiguration(AppSettings.Instance.Configuration);
builder.Host.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.Console()
);
// Add services to the container.
ConfigureServices(builder.Services);
var app = builder.Build();
//app.UseSerilogRequestLogging();
MigrateDatabase(app);
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
Log.Debug("######################################################################################################");
}
Reproduction
add nuget package:
dotnet add package Serilog (3.1.1)
dotnet add package Serilog.AspNetCore (8.0.1)
dotnet add package Serilog.Sinks.Console (5.0.1)
dotnet add package Serilog.Sinks.File (5.0.0)
Expected behavior
See the expected format on the console and the file in the expected folder
Relevant package, tooling and runtime versions
N/A
Additional context
Framework: Net Core 8
Project type: API