Serilog - how to customize date in rolling file name?

点点圈 提交于 2021-01-29 17:11:34

问题


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

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


回答1:


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.

Links you might be interested in:

  • Support {Date} for log path
  • Serilog {Date} format when using RollingFile
  • Ability to use DateTime format strings in Rolling File Sinks file names
  • Support for path by date


来源:https://stackoverflow.com/questions/60228026/serilog-how-to-customize-date-in-rolling-file-name

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