wpf 暂时禁用Serilog中的时间戳

txu3uszq  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(201)

我正在使用软件包 Serilog. sink.File 我希望仅对少数消息禁用时间戳,而不创建LoggerConfiguration的另一个示例,例如-

Log.Logger = new LoggerConfiguration().WriteTo.File("logs/log.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}").CreateLogger();

输出:

2023-03-10 05:33:24 [INF] App started
2023-03-10 05:33:24 [INF] Some process1
2023-03-10 05:33:24 [INF] file1
2023-03-10 05:33:24 [INF] file2
2023-03-10 05:33:24 [INF] file3
2023-03-10 05:33:24 [INF] Operation completed.

我想要:

2023-03-10 05:33:24 [INF] App started
2023-03-10 05:33:24 [INF] Some process1
file1
file2
file3
2023-03-10 05:33:24 [INF] Operation completed.

如何做到这一点?

x8diyxa7

x8diyxa71#

找到方法了。我用shared参数true创建了两个logger对象。

var logFilePath = "logs/log.txt";
var loggerConfiguration = new LoggerConfiguration().WriteTo.File(logFilePath, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}", shared: true).CreateLogger();
Log.Logger = loggerConfiguration;
var FilesLogger = new LoggerConfiguration().WriteTo.File(logFilePath, outputTemplate: "{Message}{NewLine}", shared: true).CreateLogger();

相关问题