Configure Serilog File sink to use one log file per application run

穿精又带淫゛_ 提交于 2020-05-14 19:12:24

问题


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 and rollOnFileSizeLimit 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!