【问题标题】:Serilog different log file per levelSerilog 每个级别的不同日志文件
【发布时间】:2019-09-02 03:22:29
【问题描述】:

我希望我的日志根据其级别保存在不同的文件夹中。我希望这个配置完全通过配置文件而不是hard-coded configs,因为它将来可能会改变。在NLogconfiguration file 中这样做很容易,但我没有找到如何在Serilog 中进行高级日志配置。有什么建议吗?

目前我有这个:

"Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "MinimumLevel": "Information",
    "Override": {
      "Microsoft": "Warning",
      "System": "Warning"
    },
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "File",
        "Args": {
          "path": "logs\\AppLogs_.txt",
          "rollingInterval": "Hour",
          "fileSizeLimitBytes": 15000000,
          "rollOnFileSizeLimit": true,
          "shared": true,
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}]) {Message} {NewLine} {Exception}"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]


  }

【问题讨论】:

  • 您可以根据documentation在每个接收器级别上覆盖它
  • @SimplyGed,这是一个代码更改,正如我所说,我想在配置中按级别配置它,我没有找到每个级别配置的示例配置文件
  • 您是否尝试将"restrictedLevelMinimum": "Debug" 添加到配置Args
  • 另外,正如该文档所说,您不能在每个接收器的基础上更改配置级别:来自文档the logger-level configuration controls which logging statements will result in the creation of events, while the sink-level configuration only filters these. To create a single logger with a more verbose level, use a separate LoggerConfiguration.
  • restrictedLevelMinimum 没用

标签: logging .net-core serilog


【解决方案1】:

这是一个非常古老的帖子,但对于任何遇到此问题的人来说,这是我的解决方案。

您需要为要分离的每个级别创建一个配置文件。我有:

Serilog.Debug.json
Serilog.Information.json
Serilog.Error.json
Serilog.Warning.json

在这4个文件中你需要有如下配置:

{
  "Serilog:WriteTo:3:Args:configureLogger": { //notice this key
     "WriteTo": [
     {
         "Name": "File",
         "Args": {
         "path": "some path containing the log level"
          }
     }
     ],
"Filter": [
  {
    "Name": "ByIncludingOnly",
    "Args": {
      "expression": "@l = 'Debug'"
     }
   }
   ]
}
}

然后你可以有另一个文件为 Fatal Serilog.json 和以下内容:

{
"Serilog": {
"Enrich": [
  "FromLogContext",
  "WithMachineName"
],
"restrictedToMinimumLevel": null,
"WriteTo": [
  {
    "Name": "File",
    "Args": {
      "path": "Logs/Fatal_.txt",
      "restrictedToMinimumLevel": "Fatal"
    }
  },
  {
    "Name": "Logger",
    "Args": {
      "configureLogger": {} // leave this empty: Information
    }
  },
  {
    "Name": "Logger",
    "Args": {
      "configureLogger": {} // leave this empty: Warning
    }
  },
  {
    "Name": "Logger",
    "Args": {
      "configureLogger": {} // leave this empty: Debug
    }
  },
  {
    "Name": "Logger",
    "Args": {
      "configureLogger": {} // leave this empty: Error
    }
  }
]
}
}  

为了使所有这些工作,您至少需要以下软件包:

Serilog.Sinks.File
Serilog.Expressions
Serilog.Expressions.Logging

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-02
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    相关资源
    最近更新 更多