gpt4 book ai didi

.Net Core appsettings.json 最佳实践 - 覆盖开发设置(反之亦然)?

转载 作者:行者123 更新时间:2023-12-03 14:36:13 26 4
gpt4 key购买 nike

在 .Net Core 中寻找构建 appsettings.json 文件的明智方法。

是否应该将基本的“appsettings.json”文件配置为在开发环境中运行,然后基于环境的覆盖(例如 appsettings.production.json)覆盖生产的特定 key ?

或者 appsettings.json 应该只包含在所有环境中共享的配置,然后使用特定的 appsettings.development/staging.json 文件为这些环境显式设置 key ?

我担心的是 - 假设应用程序部署到实时服务器,但存储在环境变量(例如覆盖连接字符串)中的 key 丢失或拼写错误等。在这种情况下,应用程序将回退到基础 appsettings.json连接字符串,这对于实时环境来说是不正确的数据库。像这样的场景听起来非常灾难性,特别是因为这很容易被忽视?

所以真的问题归结为 - 基本 appsettings.json 文件的内容是否应该是默认的“开发”值(例如开发数据库、沙箱 API),并被生产数据覆盖,反之亦然?

最佳答案

我认为这个答案很无聊;这取决于。但我最喜欢的方法是这样的:

appsetting.json (base settings)
appsettings.development.json (dev with no secrets)
appsettings.production.json (production with no secrets)

Appsettings 中的 secret 值仅存在于基本设置中,而其他值则写入相应的 appsettings.[env].json 中。因此,示例数据库连接键仅存在于本地数据库的基本设置中。替换它是环境的工作

数据库连接和日志记录示例

appsettings.json
{
"ConnectionStrings": {
“dbConnection: “data source=localhost” <—— only here
},
“environment”: “local”,
"Logging": {
"LogLevel": {
"Default": “Verbose”
}
},
}

appsettings.development.json
{
“environment”: “development”,
"Logging": {
"LogLevel": {
"Default": “Warning”
}
},
}

appsettings.production.json
{
“environment”: “production”,
"Logging": {
"LogLevel": {
"Default": “Information”
}
},
}

My concern is - say an app is deployed to a live server, but a key stored in an environment variable (e.g. to override connection string) is missing or spelt wrong etc. In this case the app would fall back to the base appsettings.json connection string which would be the incorrect DB for the live environment. A scenario like this sounds pretty disasterous, particularly as this could easily go unnoticed?



你总是可以这样做。但是一些健全性测试应该可以做到。如果您的基础设施/部署管道允许,请在您 ping 数据库的位置进行简单的健康检查。

关于.Net Core appsettings.json 最佳实践 - 覆盖开发设置(反之亦然)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62087066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com