[英]Serilog does not take console config from json
我的 asp.net 核心 (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
文件中有**
(我將其用於測試目的)時,控制台output 似乎是默認控制台 - 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" />
根據serilog 控制台接收器文檔,您需要在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}"
}
}
]
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.