【问题标题】:Providing an Azure web app with complex settings configuration提供具有复杂设置配置的 Azure Web 应用
【发布时间】:2020-03-04 16:02:44
【问题描述】:

我在我的 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 配置在那里无效。那么如何提供任何类型的高级配置呢?

【问题讨论】:

    标签: c# azure asp.net-core


    【解决方案1】:

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

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

    另见:Import or export configuration data

    【讨论】:

    • 很遗憾,Azure 应用门户不允许在键名中使用冒号。
    • 嗯,确实如此。但是,您可以改用__ - 这也有效...
    【解决方案2】:

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

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

    Serilog:MinimumLevel:默认

    我一直在使用以下 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:执行后仔细检查设置/值...

    【讨论】:

    • 不幸的是,Azure 应用程序门户不允许在键名中使用冒号。
    • 您是否尝试过在 Azure 应用程序门户中创建 Web 应用程序并通过 UI 创建配置设置?那篇文章是 2016 年的,也许他们从那以后就引入了限制。这是截图:i.stack.imgur.com/Ey2Ny.png
    • 其实我就是这么做的。这是打印屏幕:postimg.cc/Y4DC3Pdz
    • 好的,看看this discussion 我已经明白了。这是因为 Web 应用程序托管在 Linux 上,它不允许使用冒号(这就是我讨厌使用 env 变量的原因;它们因平台而异!)在 Linux 主机上,您必须使用双下划线 (__)。
    猜你喜欢
    • 2019-12-19
    • 2020-01-07
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多