如何将Serilog日志文件分割成多个文件?

问题描述 投票:0回答:1

我正在尝试按 SourceContext 拆分日志。 花了几天时间阅读文档和很多很多帖子后,我有了这个配置,但没有解决方案:

"Serilog": {
  "Using": [ "Serilog.Sinks.Debug", "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Expressions" ],
  "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
  "MinimumLevel": {
    "Default": "Debug",
    "Override": {
      "Microsoft.AspNetCore": "Information",
      "Microsoft": "Warning",
      "System": "Warning"
    }
  },
  "WriteTo": [
    {
      "Name": "File",
      "Args": {
        "path": "Logs/log-.txt",
        "rollingInterval": "Day",
        "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}"
      }
    },
    {
      "Name": "Logger",
      "Args": {
        "confgureLogger": {
          "WriteTo": [
            {
              "Name": "File",
              "Args": {
                "path": "Logs/log-HTTP-.txt",
                "rollingInterval": "Day",
                "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {SourceContext} {Namespace} - {Message:lj}{NewLine}{Exception}"
              }
            }
          ],
          "Filter": [
            {
              "Name": "ByIncludingOnly",
              "Args": {
                "expression": "StartsWith(SourceContext, 'Microsoft.AspNetCore')"
              }
            }
          ]
        }
      }
    },
    {
      "Name": "Logger",
      "Args": {
        "confgureLogger": {
          "WriteTo": [
            {
              "Name": "File",
              "Args": {
                "path": "Logs/log-API-.txt",
                "rollingInterval": "Day",
                "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {SourceContext} {Namespace} - {Message:lj}{NewLine}{Exception}"
              }
            }
          ],
          "Filter": [
            {
              "Name": "ByIncludingOnly",
              "Args": {
                "expression": "StartsWith(SourceContext, 'My.Application')"
              }
            }
          ]
        }
      }
    },

    {
      "Name": "Console",
      "Args": {
        "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {SourceContext} {NameSpace} - {Message:lj}{NewLine}{Exception}"
      }
    }
  ]
}

我尝试过“subLogs”和多个根日志。

对于子日志,我只有一个文件。 使用 rootlogs,我在两个文件中都有相同的日志。 除了更改记录器并切换到 NLog 之外,我不再知道该怎么办。

有可行的解决方案吗?

asp.net-web-api serilog serilog-sinks-file
1个回答
0
投票

一种简单的方法是使用 Serilog 表达式。使用此工具,您可以拥有带有单独过滤器的不同水槽。 SeriLog 表达式 Github

另外,Stackoverflow 上也有类似的问题 将 Serilog 日志过滤到不同的接收器

© www.soinside.com 2019 - 2024. All rights reserved.