简体   繁体   English

如何在 .Net WPF 应用程序中使用 Serilog 自动拖尾(删除)旧日志?

[英]How do I automatically tail (delete) older logs using Serilog in a .Net WPF application?

I'm using Serliog in a .Net WPF application.我在 .Net WPF 应用程序中使用 Serliog。

Is there a way that I can "tail" (delete) the log files automatically when they are over N days old?有没有办法在日志文件超过 N 天时自动“拖尾”(删除)它们?

According to https://github.com/serilog/serilog-sinks-file/blob/dev/README.md the default value of retainedFileCountLimit is 31 so only the most recent 31 files are kept by default.根据https://github.com/serilog/serilog-sinks-file/blob/dev/README.md,retainedFileCountLimit的默认值为 31,因此默认情况下只retainedFileCountLimit最近的 31 个文件。

To change the amount of files kept in code:要更改代码中保留的文件数量:

var log = new LoggerConfiguration()
    .WriteTo.File("log.txt", retainedFileCountLimit:= 42)
    .CreateLogger();

pass null to remove the limit.传递null以删除限制。

In XML <appSettings> configuration:在 XML <appSettings>配置中:

<appSettings>
  <add key="serilog:using:File" value="Serilog.Sinks.File" />
  <add key="serilog:write-to:File.path" value="log.txt" />
  <add key="serilog:write-to:File.retainedFileCountLimit" value="42"/>
</appSettings>

and pass an empty string to remove the limit.并传递一个空字符串以删除限制。

In JSON appsettings.json configuration在 JSON appsettings.json配置中

{
  "Serilog": {
    "WriteTo": [
      { "Name": "File", "Args": { "path": "log.txt", "retainedFileCountLimit": "42" } }
    ]
  }
}

and pass an empty string to remove the limit.并传递一个空字符串以删除限制。 Note that I have not tested the JSON configuration.请注意,我尚未测试 JSON 配置。

https://github.com/serilog/serilog-sinks-rollingfile/blob/dev/README.md Look there. https://github.com/serilog/serilog-sinks-rollingfile/blob/dev/README.md看那里。 You can configure autocreation of a new log file every day and also you can set how many of them you want to be kept您可以配置每天自动创建一个新的日志文件,也可以设置要保留的日志文件数量

Now you can also specify a property retainedFileTimeLimit : https://github.com/serilog/serilog-sinks-file/pull/90现在您还可以指定一个属性retainedFileTimeLimithttps : //github.com/serilog/serilog-sinks-file/pull/90

By the way, don't forget to specify retainedFileCountLimit: null if you want limitation only by the date.顺便说一句,如果您只想按日期进行限制,请不要忘记指定retainedFileCountLimit: null With the current implementation default value of retainedFileCountLimit is 31. Therefore, if you leave the parameter out, this filter will also be applied在当前实现中,retainedFileCountLimit 的默认值是 31。因此,如果您不使用该参数,也会应用此过滤器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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