I need to be able to switch between Serilog and another logger, MyLog. Currently I am switching by comment/uncomment code as the following:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
//// === to switch between Serilog and MyLogger comment/uncomment code below ===
//.UseSerilog((hostContext, loggerConfig) =>
// loggerConfig.ReadFrom.Configuration(hostContext.Configuration));
.ConfigureLogging((hostBuilderContext, logging) =>
{
logging.AddMyLogger(options =>
{
hostBuilderContext.Configuration.GetSection(MyLoggerOptions.LogOptions).Bind(options);
});
});
}
This works, but I prefer to make it controlled by a appsettings.json setting, say, Logger. So here is my appsettings.json file:
{
"Logger": "Serilog", // Use Serilog or MyLog
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Grpc": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
},
"MyLog": {
"Options": {
"FolderPath": "logs",
"FilePath": "Svr_{date}.log"
},
"LogLevel": {
"Default": "Information",
"Microsoft": "Error",
"Microsoft.Hosting.Lifetime": "Error"
}
}
},
"Serilog": {
"MinimumLevel": "Verbose",
"Override": {
"Microsoft.AspNetCore": "Warning"
},
"WriteTo": [
{
"Name": "Console "
},
{
"Name": "File",
"Args": {
"path": "Logs\\Serilog_Server-.log",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"rollingInterval": "Day"
}
}
]
}
}
and here is the code where I try to set log depending on the Loger setting:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureLogging((hostBuilderContext, logging) =>
{
string loger = hostBuilderContext.Configuration.GetValue<String>("Logger");
if (loger == "MyLog")
{
logging.AddMyLogger(options =>
{
hostBuilderContext.Configuration.GetSection(MyLoggerOptions.LogOptions).Bind(options);
});
}
else if (loger == "Serilog")
{
logging.AddSerilog(); // here is the problem. Serilog doesn't write.
}
});
The problem of this code is the Serilog (MyLog is fine) - it doesn't write at all. My guess is that, since I don't have a way to pass in the configuration object, it might not get configured properly. But how do I fix it? or is there a better way to do this?
UPDATE
Tried what Camilo suggested as the following with an error. How do I get the configuration object from here?