将Serilog文件接收器配置为在每个应用程序运行中使用一个日志文件 [英] Configure Serilog File sink to use one log file per application run

查看:508
本文介绍了将Serilog文件接收器配置为在每个应用程序运行中使用一个日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Serilog配置为在每次应用运行时创建一个日志文件.
文件名应基于当前日期/时间:
第一次运行::log_20180413_1020.txt
第二次运行:log_20180413_1033.txt

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?

推荐答案

配置 Serilog.Sinks.File 不加任何滚动周期或大小限制的接收器,并在启动时配置日志记录时将时间戳记添加到日志文件名:

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:

同时指定rollingIntervalrollOnFileSizeLimit会同时应用这两个策略,而 同时指定这两个策略将不会将所有事件都写入单个文件.

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.

这篇关于将Serilog文件接收器配置为在每个应用程序运行中使用一个日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆