Serilog 记录器 - 未找到请求的值 'True'
Serilog Logger - Requested value 'True' was not found
调用 Serilog.Log.Logger()
:
时出现此错误
这是 .NET Core 2.1
System.ArgumentException: Requested value 'True' was not found.
我的Program.cs
:
public class Program
{
private static IConfiguration Configuration => new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile(
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
true, true)
.AddEnvironmentVariables()
.Build();
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration).CreateLogger();
try
{
Log.Information("Web hosting is starting up.");
CreateWebHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "A fatal exception has forced unexpected host termination.");
throw;
}
finally
{
Log.Information("Web hosting is shutting down.");
Log.CloseAndFlush();
}
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft.EntityFrameworkCore": "Warning",
"Microsoft.AspNetCore.StaticFiles": "Warning"
}
},
"WriteTo": [
{
"Name": "Elasticsearch",
"Args": {
"nodeUris": "https://XXXXXXXXXXXXXX.com",
"indexFormat": "log-XXXXXXXXXX-{0:yyyy.MM.dd}",
"autoRegisterTemplate": true,
"autoRegisterTemplateVersion": true
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithEnvironmentUserName"
],
"Properties": {
"Application": "{myappname}"
}
}
这里是这个项目中安装的包(请原谅截图)
C:\repos\myapp\myapp>dotnet list package
Project 'myapp' has the following package references
[netcoreapp2.1]:
Top-level Package Requested Resolved
> BuildBundlerMinifier 3.2.435 3.2.435
> Microsoft.AspNetCore.App (A) [2.1.1, ) 2.1.1
> Microsoft.AspNetCore.Razor.Design 2.1.2 2.1.2
> Microsoft.NETCore.App (A) [2.1.0, ) 2.1.0
> Serilog.AspNetCore 3.2.0 3.2.0
> Serilog.Enrichers.Environment 2.1.3 2.1.3
> Serilog.Sinks.Elasticsearch 8.0.1 8.0.1
"autoRegisterTemplateVersion": true
问题与 appsettings.json 文件中的 autoRegisterTemplateVersion
参数有关。
从Serilog.Sinks.Elasticsearch JSON appsettings.json configuration可以看出autoRegisterTemplateVersion
参数应该是字符串类型:
将 autoRegisterTemplateVersion
参数值从 true
更改为 "ESv2"
后,问题消失。
调用 Serilog.Log.Logger()
:
这是 .NET Core 2.1
System.ArgumentException: Requested value 'True' was not found.
我的Program.cs
:
public class Program
{
private static IConfiguration Configuration => new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile(
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
true, true)
.AddEnvironmentVariables()
.Build();
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration).CreateLogger();
try
{
Log.Information("Web hosting is starting up.");
CreateWebHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "A fatal exception has forced unexpected host termination.");
throw;
}
finally
{
Log.Information("Web hosting is shutting down.");
Log.CloseAndFlush();
}
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft.EntityFrameworkCore": "Warning",
"Microsoft.AspNetCore.StaticFiles": "Warning"
}
},
"WriteTo": [
{
"Name": "Elasticsearch",
"Args": {
"nodeUris": "https://XXXXXXXXXXXXXX.com",
"indexFormat": "log-XXXXXXXXXX-{0:yyyy.MM.dd}",
"autoRegisterTemplate": true,
"autoRegisterTemplateVersion": true
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithEnvironmentUserName"
],
"Properties": {
"Application": "{myappname}"
}
}
这里是这个项目中安装的包(请原谅截图)
C:\repos\myapp\myapp>dotnet list package
Project 'myapp' has the following package references
[netcoreapp2.1]:
Top-level Package Requested Resolved
> BuildBundlerMinifier 3.2.435 3.2.435
> Microsoft.AspNetCore.App (A) [2.1.1, ) 2.1.1
> Microsoft.AspNetCore.Razor.Design 2.1.2 2.1.2
> Microsoft.NETCore.App (A) [2.1.0, ) 2.1.0
> Serilog.AspNetCore 3.2.0 3.2.0
> Serilog.Enrichers.Environment 2.1.3 2.1.3
> Serilog.Sinks.Elasticsearch 8.0.1 8.0.1
"autoRegisterTemplateVersion": true
问题与 appsettings.json 文件中的 autoRegisterTemplateVersion
参数有关。
从Serilog.Sinks.Elasticsearch JSON appsettings.json configuration可以看出autoRegisterTemplateVersion
参数应该是字符串类型:
将 autoRegisterTemplateVersion
参数值从 true
更改为 "ESv2"
后,问题消失。