繁体   English   中英

提供具有复杂设置配置的 Azure Web 应用

[英]Providing an Azure web app with complex settings configuration

我在 ASP.NET Core Web 应用程序中使用appsettings.json为我的应用程序提供配置值。 有些配置比“名称/值对”更复杂,例如 Serilog 配置:

"Serilog": {
    "MinimumLevel": {
        "Default": "Information",
        "Override": {
            "System": "Warning",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information",
            "Microsoft.AspNetCore.Authentication": "Information"
        }
    },
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "WriteTo": [
        { "Name": "Console" },
        {
            "Name": "File",
            "Args": {
                "path": "C:/Development/logs/log-auth.txt",
                "fileSizeLimitBytes": 1000000,
                "rollOnFileSizeLimit": true,
                "shared": true,
                "flushToDiskInterval": 1,
                "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext} | {Message:lj}{NewLine}{Exception}"
            }
        }
    ]
}

我想知道的是,如何为我的 Azure Web 应用提供这种配置? 我知道我可以创建一个appsettings.Production.json文件并在我发布时推送它,但是通过发布部署设置有什么意义? 它们也可能是硬编码的。 设置文件的重点是您应该能够更改设置并重新启动应用程序,更改其行为,而无需重新部署代码。

我唯一能看到 Azure 提供的是它在门户中的“设置 | 配置”部分,但这些设置只允许是简单的名称/值对。 我的 Serilog 配置在那里无效。 那么我怎样才能提供任何类型的高级配置呢?

正如蒂亚戈已经说过的,您可以使用:添加更多简单的键/值配置。 但是,如果您必须处理更大的应用程序配置,我建议使用Azure App Configuration

在 Linux 托管的 Web 应用程序上,Azure 不允许您在配置设置中插入冒号,大概是因为 Linux 环境变量名称中不允许这样做。 使用双下划线 ( __ ) 代替冒号。

另请参阅: 导入或导出配置数据

在发布过程中会忽略 appsettings.Production.json 文件。 您需要使用 powershell 或 CI/CD(例如使用 Azure Devops)来定义设置。

关于键/值对,您可以使用“:”作为分隔符来指定复杂类型,例如:

串行日志:最低级别:默认

我一直在使用以下 powershell 脚本:

$appname = "name of your function app"
$rg = "name of resource group"
$webApp = Get-AzureRmwebApp -ResourceGroupName $rg -Name $appname
$webAppSettings = $webApp.SiteConfig.AppSettings
$hash = @{}
foreach ($setting in $webAppSettings) {
    $hash[$setting.Name] = $setting.Value
}
$hash['Serilog:MinimumLevel:Default'] = 'Information'
$hash['Serilog:MinimumLevel:Override:System'] = 'Warning'
$hash['Serilog:MinimumLevel:Override:Microsoft'] = 'Warning'
$hash['Serilog:MinimumLevel:Override:Microsoft.Hosting.Lifetime'] = 'Information'
$hash['Serilog:MinimumLevel:Override:Microsoft.AspNetCore.Authentication'] = 'Information'

$hash['Serilog:Enrich:0'] = "FromLogContext"
$hash['Serilog:Enrich:1'] = "WithMachineName"
$hash['Serilog:Enrich:2'] = "WithThreadId"

$hash['Serilog:WriteTo:0:Name'] = "Console"
$hash['Serilog:WriteTo:1:Name'] = "File"
$hash['Serilog:WriteTo:1:Args:path'] = "C:/Development/logs/log-auth.txt"
$hash['Serilog:WriteTo:1:Args:fileSizeLimitBytes'] = "1000000"
$hash['Serilog:WriteTo:1:Args:rollOnFileSizeLimit'] = "true"
$hash['Serilog:WriteTo:1:Args:shared'] = "true"
$hash['Serilog:WriteTo:1:Args:flushToDiskInterval'] = "1"
$hash['Serilog:WriteTo:1:Args:outputTemplate'] = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext} | {Message:lj}{NewLine}{Exception}"


Set-AzureRMWebAppSlot -ResourceGroupName $rg -Name $appname -AppSettings $hash -Slot production

PS:执行后仔细检查设置/值...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM