问题
I want to configure Serilog to create one log file per app run.
File name should be based on current date/time:
1st run: log_20180413_1020.txt
2nd run: log_20180413_1033.txt
I didn't find how to do it in any File sink (Rolling, Alternative..)
Any clue?
回答1:
Configure the Serilog.Sinks.File sink without any rolling period or size limit, and add the timestamp to the log filename when you configure logging at startup:
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmm");
var log = new LoggerConfiguration()
.WriteTo.File($"log{timestamp}.txt")
.CreateLogger();
Per the Rolling Policies section of the README:
Specifying both
rollingInterval
androllOnFileSizeLimit
will cause both policies to be applied, while specifying neither will result in all events being written to a single file.
来源:https://stackoverflow.com/questions/49822263/configure-serilog-file-sink-to-use-one-log-file-per-application-run