如何使用SeriLog每天创建新的日志文件夹

huangapple go评论55阅读模式
英文:

How to create new log folder for each day using SeriLog

问题

我正在我的.NET 6应用程序中使用SeriLog。

我正在尝试创建一个SeriLog JSON配置,以帮助我每天创建一个新的日志文件夹。然后,当天的日志将创建在相应的文件夹中。

例如:

  • 20221205/Log_20221205_152358.log
  • 20221206/Log_20221206_051633.log
  • 20221207/Log_20221207_084812.log

我的当前配置如下:

{
  "Serilog": {
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "Logs\\log.log",
          "rollingInterval": "Minute",
          "fileSizeLimitBytes": 1000000,
          "rollOnFileSizeLimit": true,
          "retainedFileCountLimit": 30,
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:t4}] {Message:j}{NewLine}"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithExceptionDetails" ],
    "Properties": {
      "ApplicationName": "SampleApp",
      "Environment": "Int"
    }
  }
}

使用当前配置,我无法创建具有日期的文件夹。请帮助我解决这个问题。

英文:

I am using SeriLog in my .Net6 application.

I am trying to create a serilog json configuration that will help me create a new log folder each day.
Then the logs for that day will be created in the corresponding folder

> Eg:
> * 20221205/Log_20221205_152358.log
> * 20221206/Log_20221206_051633.log
> * 20221207/Log_20221207_084812.log

My Current config is as below

  "Serilog": {
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "Logs\\log.log",
          "rollingInterval": "Minute",
          "fileSizeLimitBytes": 1000000,
          "rollOnFileSizeLimit": true,
          "retainedFileCountLimit": 30,
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:t4}] {Message:j}{NewLine}"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithExceptionDetails" ],
    "Properties": {
      "ApplicationName": "SampleApp",
      "Environment": "Int"
    }
  }
}

By using the current configuration, I am not able to create folders with dates. Kindly help me to resolve the issue

答案1

得分: 1

你可以将 Serilog.Sinks.Map 添加到你的日志配置中,并将时间戳映射到File sink使用的路径:

return new LoggerConfiguration()
	  .WriteTo.Map(le => new DateTime(le.Timestamp.Year, le.Timestamp.Month, le.Timestamp.Day),
		(day, wt) => wt.File($"./{day:yyyyMMdd}/Log_.log",
							 rollingInterval: RollingInterval.Minute,
							 fileSizeLimitBytes: 10,
							 rollOnFileSizeLimit: true,
							 retainedFileCountLimit: 30,
							 outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:t4}] {Message:j}{NewLine}"),
		sinkMapCountLimit: 1)
	 .CreateLogger();

这个 sink 需要从代码中配置,因为它使用一个映射函数,所以无法使用XML或JSON进行配置。

英文:

You can add Serilog.Sinks.Map to your logger configuration, and map the timestamp to the path used by the File sink:

return new LoggerConfiguration()
	  .WriteTo.Map(le => new DateTime(le.Timestamp.Year, le.Timestamp.Month, le.Timestamp.Day),
		(day, wt) => wt.File($"./{day:yyyyMMdd}/Log_.log",
							 rollingInterval: RollingInterval.Minute,
							 fileSizeLimitBytes: 10,
							 rollOnFileSizeLimit: true,
							 retainedFileCountLimit: 30,
							 outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:t4}] {Message:j}{NewLine}"),
		sinkMapCountLimit: 1)
	 .CreateLogger();

This sink needs to be configured from code because it uses a mapping function, so you can't configure it using XML or JSON.

huangapple
  • 本文由 发表于 2023年3月7日 20:18:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定