c# - 未从 appsettings 文件应用 ASP.Net Core 2 中的 Serilog JsonFormatter

标签 c# .net-core asp.net-core-2.0 serilog

我有一个 ASP.Net Core 2 Web 应用程序,我正在尝试将 SeriLog 配置为以 JSON 格式输出,通过 appsettings.json 文件进行配置。我无法使用标准的 Serilog.Formatting.Json.JsonFormatter 或紧凑的 Serilog.Formatting.Compact.CompactJsonFormatter。

我看到有人问了类似的问题 here ,但使用 appsettings.json 似乎没有确凿的答案,并且该线程已经安静了一年多(而且我没有足够的声誉来评论以插入进一步的回应)。

我正在使用以下 SeriLog 包:

  • Serilog.AspNetCore - 2.1.0
  • Serilog.Enrichers.Environment - 2.1.2
  • Serilog.Enrichers.Thread - 3.0.0
  • Serilog.Formatting.Compact - 1.0.0
  • Serilog.Settings.Configuration - 2.4.0
  • Serilog.Sinks.RollingFile - 3.3.0

我的 appsettings.json 文件的 SeriLog 部分显示为:

"Serilog": {
  "Using": ["Serilog.Sinks.RollingFile"],
  "MinimumLevel": {
    "Default": "Verbose",
    "Override": {
      "Microsoft": "Warning",
      "System": "Warning"
    }
  },
  "WriteTo": [
    {
      "Name": "RollingFile",
      "Args": {
        "pathFormat": "C:\\Logs\\MLSBPortal-{Date}.txt",
        "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
        "restrictedToMinimumLevel": "Debug",
        "retainedFileCountLimit": "10",
        "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{ThreadId}] [{SourceContext}] [{SessionId}] [{UserName}]: {Message}{NewLine}{Exception}"
      }
    }
  ],

在使用默认 JSON 格式化程序进行测试时,将格式化程序行修改为以下内容:

"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",

正如其他多个地方所建议的那样。

我的输出写得很好,但格式化程序设置似乎对输出没有任何影响。

我是不是做错了什么,还是这个功能还没有通过读取的配置文件实现?

在此先感谢您的帮助。

最佳答案

事实证明,(目前)不可能同时指定格式化程序和输出模板(如 https://github.com/serilog/serilog-aspnetcore/issues/31 中所述)。

希望这种情况在未来可能有所改变......

关于c# - 未从 appsettings 文件应用 ASP.Net Core 2 中的 Serilog JsonFormatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48646420/

相关文章:

c# - 我可以使用 Microsoft Dotnet CLI 构建 ASP.NET Core Web 应用程序吗?

ASP.NET Core 2.0 EventLog 更改事件查看器中的应用程序名称

azure - 如何让 VSTS 使用 net Net Standard Azure Functions 查找我的 NetCore 单元测试?

c# - 视频文件流无法在 asp.NET Core 2.0 中的 Safari/iOS 设备上运行

c# - 从 Windows 8/WinRT 中的不同线程更新 UI 元素

c# - 在 Razor 中声明变量

c# - 将 MySQL .DMP 文件转换为 MS Sql Server 2005 的 .SQL 文件

c# - Linq to sql - 数据库不会更新

asp.net-core - Blazor 独立 WebAssembly 代表登录用户调用安全核心 API

asp.net-core - 当我在浏览器中刷新页面时,如何在 ASP.NET Core 2.2 中启用自动重建?