[英]Serilog with Asp.Net Core 2.1 not writing from appsettings.json
I am trying to configure Serilog
to read my settings from the appsettings.json
file in my ASP.NET Core 2 WebApi. 我正在尝试将Serilog
配置为从ASP.NET Core 2 WebApi中的appsettings.json
文件读取我的设置。
I have the following in the root of the appsetting.json
file; 我在appsetting.json
文件的根目录中有以下内容;
"Serilog": {
"Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Async", "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "File",
"Args": { "pathFormat": "Logs\\Serilog\\log.txt" },
"RollingInterval": "Day"
},
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "myconnectionstring",
"schemaName": "Schema",
"tableName": "Logs",
"batchPostingLimit": 1000,
"period": 30
}
}
]
}
}
]
}
In my Startup.cs
I have the following; 在我的Startup.cs
我有以下内容;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public async void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var log = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
loggerFactory.AddSerilog(log);
If I run the following then the output perfectly so I know its down to the move to appsettings; 如果我运行以下命令,那么输出将是完美的,因此我知道将其转移到appsettings的过程。
var log = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.MSSqlServer("connectionString", "Logs", schemaName:"Schema")
.WriteTo.File("Logs\\Serilog\\log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
And then in my Program.cs
I have; 然后在我的Program.cs
中;
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config =>
config.AddJsonFile(
x =>
{
x.Path = "appsettings.json";
x.Optional = true;
x.ReloadOnChange = true;
})
.AddJsonFile(
x =>
{
x.Path = "appsettings.{currentEnv}.json";
x.Optional = true;
x.ReloadOnChange = true;
})
)
.UseStartup<Startup>()
.Build();
To note, both appsettings.json and the environment specific currently contain the same data. 注意,appsettings.json和特定于环境的当前都包含相同的数据。
I have the following versions of Serilog installed; 我安装了以下版本的Serilog;
Serilog 2.7.1 Serilog.spNetCore 2.1.1 Serilog.Enrichers.Environment 2.1.2 Serilog.Settings.Configuration 3.0.1 Serilog.Sinks.Async 1.3.0 Serilog.Sinks.File 4.0.0 Serilog.Sinks.MSSqlServer 5.1.2 Serilog 2.7.1 Serilog.spNetCore 2.1.1 Serilog.Enrichers.Environment 2.1.2 Serilog.Settings.Configuration 3.0.1 Serilog.Sinks.Async 1.3.0 Serilog.Sinks.File 4.0.0 Serilog.Sinks.MSSqlServer 5.1.2
Try this in your Program.cs file: 在Program.cs文件中尝试以下操作:
UseStartup<Startup>() .UseSerilog() .Build();
That will tell the HostBuilder to use the Serilog package. 这将告诉HostBuilder使用Serilog软件包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.