[英].NET Core Console App - not setting appropriate appsettings based env variable at runtime
I am working on .NET Core 5 Console App and I was trying load appsettings.[environment].json at runtime based on the "ASPNETCORE_ENVIRONMENT = Development" which I setup in debug under project properties.我正在开发 .NET Core 5 控制台应用程序,我正在尝试加载 appsettings.[environment].json 在运行时基于我在项目属性下调试中设置的“ASPNETCORE_ENVIRONMENT = Development”。
In the BuildConfig method I can see that在 BuildConfig 方法中,我可以看到
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
is returning the " Development " text correctly and also loading the 2 file (appsettings.json, appsettings.development.json).正在正确返回“ Development ”文本并加载 2 文件(appsettings.json,appsettings.development.json)。
but when I pass the config to different class via constructor and then inspecting config in that class I seeing the (appsettings.json, appsettings.production.json) file not the development file why?但是当我通过构造函数将配置传递给不同的 class 然后检查该 class 中的配置时,我看到的是(appsettings.json,appsettings.production file.json)文件而不是开发?
I don't have appsettings.production.json file in my project yet.我的项目中还没有 appsettings.production.json 文件。
static void Main(string[] args)
{
try
{
//setting for SeriLog
var builder = new ConfigurationBuilder();
BuildConfig(builder);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.ReadFrom.Configuration(builder.Build())
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File(@"Log\Log.txt")
.CreateLogger();
Log.Logger.Information("Application Starting");
var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddTransient<IRIUProcess, RIUProcess>();
})
.UseSerilog()
.Build();
var svc = ActivatorUtilities.CreateInstance<RIUProcess>(host.Services);
svc.Run(args);
}
catch (Exception ex)
{
Log.Logger.Error(ex.ToString());
}
}
static void BuildConfig(IConfigurationBuilder builder)
{
builder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
}
Configuration builder return self instance configurated like entityframework core配置生成器返回像实体框架核心一样配置的自我实例
//setting for SeriLog
var builder = new ConfigurationBuilder();
BuildConfig(builder);
should be应该
//setting for SeriLog
var builder = new ConfigurationBuilder();
builder = BuildConfig(builder);
Without that, your configuration builder remain unchanged.没有它,您的配置生成器将保持不变。 Adapt also the BuildConfig method for that.
也为此调整 BuildConfig 方法。
Then you can remove the CreateDefaultBuilder (with use default AspNetCore Configuration) and directly use your instance of configuration:然后您可以删除 CreateDefaultBuilder(使用默认 AspNetCore 配置)并直接使用您的配置实例:
new HostBuilder().ConfigureHostConfiguration(a => a.AddConfiguration(builder.Build()))
https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.hostbuilder.configurehostconfiguration?view=dotnet-plat-ext-5.0 https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.hostbuilder.configurehostconfiguration?view=dotnet-plat-ext-5.0
You will have then your host with the same configuration for SeriLog and your host然后,您的主机将具有与 SeriLog 和主机相同的配置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.