Serilog - 如何自定义滚动文件名中的日期? [英] Serilog - how to customize date in rolling file name?

查看:345
本文介绍了Serilog - 如何自定义滚动文件名中的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Serilog 中,您可以轻松启用滚动日志文件:

In Serilog, you can easily enable rolling log files:

Log.Logger = new LoggerConfiguration()
    .WriteTo.File("log-.txt", rollingInterval: RollingInterval.Day)
    .CreateLogger();

这将每天创建一个新的日志文件,格式如下:

This will create a new log file every day in the following format:

  • log-20200214.txt
  • log-20200215.txt
  • log-20200216.txt

我的问题:是否可以自定义日期在文件名中的位置,以及自定义日期的格式?

My question: is it possible to customize where the date is placed in the file name, and to customize how the date is formatted?

例如我希望文件名如下所示:

e.g. I want the file names to look like this:

  • 2020-02-14-log.txt
  • 2020-02-15-log.txt
  • 2020-02-16-log.txt

我希望用 "{Date:yyyy-MM-dd}-log.txt" 替换 "log-.txt" 会起作用,但它没有'

I was hoping replacing "log-.txt" with "{Date:yyyy-MM-dd}-log.txt" would work, but it doesn't.

推荐答案

Serilog.Sinks.File 接收器.如果你真的想要这个功能,你可以尝试发送pull-request,或 fork 代码库并使用您自己的自定义实现.

This is not currently supported by the Serilog.Sinks.File sink. If you really want this feature, you can try to send a pull-request, or fork the repository and use your own custom implementation.

您可能感兴趣的链接:

这篇关于Serilog - 如何自定义滚动文件名中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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