简体   繁体   中英

Serilog - how to customize date in rolling file name?

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?

eg 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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM