I was initially using one log file for Serilog, which I accomplished by doing this
var slc = new SerilogSubLoggerConfiguration();
configuration.GetSection("Serilog:SubLogger").Bind(slc);
and then configuring the SubLogger in the Main method of Program.cs
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.WriteTo.Logger(logger => logger.Filter.
ByIncludingOnly(lvl => lvl.Level == slc.Level).WriteTo.RollingFile(slc..PathFormat))
.CreateLogger();
I have moved to using three separate logs now defined in the appsettings file. This creates the class, with one property, a list of Serilog configurations
public class SerilogSubLoggerConfigurations
{
public List<SerilogSubLoggerConfiguration> SubLoggers { get; set; }
}
var slcs = configuration.GetSection("Serilog").Get<SerilogSubLoggerConfigurations>();
Now that I have my list of SubLogger configs, I need to create the logger, and add all these sub loggers. Each SubLogger will need its own
.WriteTo.Logger(logger => logger.Filter.
ByIncludingOnly(lvl => lvl.Level == slc.Level).WriteTo.RollingFile(slc..PathFormat))
line in the Log.Logger call, therefore I need to iterate over the SubLoggers. My intention was to write a method to do so.
public static LoggerConfiguration GetLoggerConfiguration(IConfiguration config, SerilogSubLoggerConfigurations slcs)
{
var lc = new LoggerConfiguration();
lc.ReadFrom.Configuration(config);
foreach (var cfg in slcs.SubLoggers)
{
lc.WriteTo.Logger(logger => logger.Filter
.ByIncludingOnly(lvl => lvl.Level == cfg.Level).WriteTo
.RollingFile(cfg.PathFormat));
}
lc.CreateLogger();
return lc;
}
and then just use this in the Main method
Log.Logger = GetLoggerConfiguration(configuration, slcs);
But this errs,
Cannot implicitly convert type 'Serilog.LoggerConfiguration' to 'Serilog.ILogger'
I tried changing the method to an ILogger and returning an ILogger, but ILogger doesn't contain the WriteTo and ReadFrom methods, so that was a no go. I tried casting the returned LoggerConfiguration to an ILogger, but that didn't work either and a number of other iterations that also failed.
There must be a way to accomplish this. Any help appreciated.
Update:
public static int Main(string[] args)
{
var currentEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{currentEnv}.json", optional: true)
.AddEnvironmentVariables()
.Build();
var slcs = configuration.GetSection("Serilog").Get<SerilogSubLoggerConfigurations>();
Log.Logger = BuildLogger(configuration, slcs);
Log.Information(Appname != null ? $"{Appname}{LogDelimeter}Logger created." : "Logger created.");
try
{
Log.Information(Appname != null ? $"{Appname}{LogDelimeter}Starting web host" : "Starting web host.");
BuildWebHost(args).Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex,
Appname != null
? $"{Appname}{LogDelimeter}Web Host terminated unexpectedly"
: "Web Host terminated unexpectedly");
return 1;
}
finally
{
Log.Information(Appname != null ? $"{Appname}{LogDelimeter}Flushing log and closing it." : "Flushing log and closing it.");
Log.CloseAndFlush();
}
}