[英]Start logging before webhost in ASP.NET Core 2
I want to start logging before webhost, so startup errors are logged. 我想在webhost之前开始记录日志,因此记录了启动错误。 So I followed Serilog's recommended init order : 1) configuration, 2) logging, 3) webhost.
因此,我遵循了Serilog推荐的初始化顺序 :1)配置,2)日志记录,3)Webhost。 I'm not using
CreateDefaultBuilder()
. 我没有使用
CreateDefaultBuilder()
。
So my Program.cs
has: 所以我的
Program.cs
有:
// setup config
var envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional:false, reloadOnChange:true)
.AddJsonFile($"appsettings.{envName}.json", optional:true, reloadOnChange:true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
// setup Serilog logging
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration) // <<< uses config from above
.CreateLogger()
// setup webhost
new WebHostBuilder()
.UseConfiguration(configuration) // <<< uses config from above
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseSerilog()
.Build()
.Run();
This works, but changes in appsettings.json
are not detected even though I specified reloadOnChange:true
. 这
appsettings.json
,但是即使我指定了reloadOnChange:true
也未检测到appsettings.json
中的更改。
However when I use ConfigureAppConfiguration()
then changes are detected: 但是,当我使用
ConfigureAppConfiguration()
会检测到更改:
new WebHostBuilder()
.ConfigureAppConfiguration((context, config) => {
// .. duplicate config here
})
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseSerilog()
.Build()
.Run();
But that means duplication and possibly unforseen problems - ie this is a hack. 但这意味着重复,甚至可能是无法预料的问题-即这是黑客。 How do I solve this?
我该如何解决?
UPDATE 更新
As per @StephenZeng's answer, UseConfiguration
won't help me here, so I suppose I need to use ConfigureAppConfiguration
. 按照@StephenZeng的回答,
UseConfiguration
在这里UseConfiguration
,所以我想我需要使用ConfigureAppConfiguration
。 But the problem remains - how do I init my config first, then use it multiple times without redefining it? 但是问题仍然存在-如何首先初始化配置,然后多次使用它而不重新定义它?
From the document it is by design: 根据文档,这是设计使然:
"UseConfiguration only copies keys from the provided IConfiguration to the host builder configuration. Therefore, setting reloadOnChange: true for JSON, INI, and XML settings files has no effect." “ UseConfiguration仅将密钥从提供的IConfiguration复制到主机构建器配置。因此,对于JSON,INI和XML设置文件,设置reloadOnChange:true无效。”
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-2.1&tabs=aspnetcore2x https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/host/web-host?view=aspnetcore-2.1&tabs=aspnetcore2x
I've run into the same issue, and I created a method with the responsibility to configure the builder. 我遇到了同样的问题,并创建了一个方法来配置构建器。 I then call the method from both
Main
and when I build the web host. 然后,我在建立Web主机时从
Main
调用该方法。 Something like this: 像这样:
public static class Program
{
public static int Main()
{
var configuration = new ConfigurationBuilder()
.AddConfiguration()
.Build();
// setup Serilog logging
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
// Code removed for brevity...
}
private static IWebHost BuildWebHost() =>
WebHost.CreateDefaultBuilder()
.UseSerilog()
.UseStartup<Startup>()
.ConfigureAppConfiguration((hostingContext, config) =>
{
// Clear default configuration
config.Sources.Clear();
// Replace with our own configuration
config.AddConfiguration();
})
.Build();
private static IConfigurationBuilder AddConfiguration(this IConfigurationBuilder self) =>
self
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional:false, reloadOnChange:true)
.AddJsonFile($"appsettings.{envName}.json", optional:true, reloadOnChange:true)
.AddEnvironmentVariables();
}
The solution is to avoid UseConfiguration
and use ConfigureAppConfiguration
instead: 解决方案是避免使用
UseConfiguration
而改用ConfigureAppConfiguration
:
// setup config
// ..as before
// ..this is done only once
// setup Serilog logging
// ...as before
// setup webhost
new WebHostBuilder()
.ConfigureAppConfiguration((context, config) => {
config.AddConfiguration(configuration); // <<< uses config from above
})
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseSerilog()
.Build()
.Run();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.