【问题标题】:Serilog does not take console config from jsonSerilog 不从 json 获取控制台配置
【发布时间】:2021-05-26 19:11:53
【问题描述】:

我的 asp.net core (5) 应用程序中有以下内容:

var config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(config)
    .CreateLogger();

appsettings.json下面

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "MyApp": "Information",
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithProcessId",
      "WithThreadId"
    ],
    "WriteTo": [
      {
        "Name": "Console",
        "outputTemplate": "{Timestamp:G}[{Level:u3}] ** {Message} ** ({SourceContext}) {NewLine:1}{Exception:1}"
      },
      {
        "Name": "File",
        "Args": {
          "path": "C:\\logs\\MyApp\\log.txt",
          "outputTemplate": "{Timestamp:G}[{Level:u3}] ** {Message} *** ({SourceContext}) {NewLine:1}{Exception:1}"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "C:\\logs\\MyApp\\log.json",
          "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
        }
      },
      {
        "Name": "Seq",
        "Args": {
          "_serverUrl": "http://localhost:5341",
          "serverUrl": "http://localhost:8081"
        }
      }
    ]
  },

  "AllowedHosts": "*"
}

当我在 txt 文件中有 **(我将其用于测试目的)时,console 输出似乎是 默认 之一 - 任何更改在config 文件中似乎没有影响它...

我的项目中安装了以下软件包:

<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />

<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />

【问题讨论】:

    标签: .net asp.net-core serilog asp.net-core-5.0 asp.net-core-configuration


    【解决方案1】:

    根据serilog console sink documentation,您需要在Args属性内指定格式

    {
      "Serilog": {
        "WriteTo": [
          {
              "Name": "Console",
              "Args": {
                "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
                "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
              }
          }
        ]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 2015-02-28
      • 1970-01-01
      • 2021-04-17
      相关资源
      最近更新 更多