简体   繁体   English

.NET 核心控制台应用程序 - 未在运行时设置适当的基于 appsettings 的环境变量

[英].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.

相关问题 在 .NET 核心控制台应用程序中使用 appsettings.ENV.json - Using appsettings.ENV.json in .NET Core Console App .Net Core 3 控制台应用程序没有启动类或 Appsettings? - .Net Core 3 console app no Startup class or Appsettings? 发布的控制台应用程序上没有.NET Core 2.0应用程序设置 - No .NET Core 2.0 appsettings on a published console app 阅读AppSettings数据控制台App .net Core 2.0 - Read AppSettings Data Console App .net Core 2.0 appsettings.json 中的哨兵配置与 .Net Core 3 控制台应用程序中的 Serilog - Sentry configuration in appsettings.json with Serilog in .Net Core 3 Console App 使用 ENV 变量在 appsettings.json 中设置键 - ASP.NET Core 3.1 Docker - Setting keys in appsettings.json with ENV variables - ASP.NET Core 3.1 Docker 如何在 docker-compose 命令中将 appsettings.json 值替换为 .env 变量以启动 ASP.NET 核心 MVC 应用程序? - How do I replace appsettings.json values with .env variable in docker-compose command to launch an ASP.NET Core MVC app? 阅读asp net core console应用程序中的appsettings - Read appsettings in asp net core console application C# Do.net 核心控制台 appsettings.json 运行时重新加载 - C# Dotnet core Console appsettings.json runtime reload 基于 .Net Core 中的 appSettings 使用 Cors - Use Cors based on an appSettings in .Net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM