c# - Serilog - 如何自定义滚动文件名中的日期?

标签 c# serilog

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

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

这将每天以以下格式创建一个新的日志文件:
  • log-20200214.txt
  • log-20200215.txt
  • log-20200216.txt

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

    例如我希望文件名看起来像这样:
  • 2020-02-14-log.txt
  • 2020-02-15-log.txt
  • 2020-02-16-log.txt

  • 我希望更换 "log-.txt""{Date:yyyy-MM-dd}-log.txt"会起作用,但不会。

    最佳答案

    Serilog.Sinks.File 当前不支持此功能。下沉。如果你真的想要这个功能,你可以试试send a pull-request ,或 fork 存储库并使用您自己的自定义实现。

    您可能感兴趣的链接:

  • 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
  • 关于c# - Serilog - 如何自定义滚动文件名中的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60228026/

    相关文章:

    c# - .net c#一次加载XML文件的方法

    json - Serilog.Sinks.Postgresql : Read configuration from appsettings. json

    c# - Serilog 实现打破了其他配置

    Serilog Graylog无法登录Graylog

    C# 随机数不是 "random"

    c# - c# 中 Get 和 Set 方法的目的是什么?

    servicestack - 将自定义接收器与 ServiceStack.Logging.Serilog 一起使用?

    dependency-injection - Serilog Sink with MassTransit - IBus 服务解析导致无限循环

    c# - 有没有办法在 Selenium 的 C# 端口中使用 WebDriverWait 的 FindsBy 注释?

    c# - Database.BeginTransaction 与 Transactions.TransactionScope