azure - 为什么无法通过新的 Azure 门户配置 Azure 诊断以使用 Azure 表存储?

标签 azure logging asp.net-web-api azure-storage azure-diagnostics

我正在开发一个将托管在 Azure 中的 Web API。我想使用 Azure 诊断将错误记录到 Azure 表存储中。 在经典门户中,我可以将日志配置为转到 Azure 表存储。

Classic Portal Diagnostic Settings

但是,在新的 Azure 门户中,我唯一的存储选项是使用 Blob 存储:

New Azure Portal Settings

看来,如果我要使用 Web 角色,我可以配置用于诊断的数据存储,但由于我正在开发 Web api,我不想为每个 api 创建单独的 Web 角色我可以登录到 azure 的表。

是否有一种方法可以以编程方式配置 azure 诊断以将日志消息传播到特定数据存储,而无需使用 Web 角色?是否有任何原因导致新的 Azure 门户仅具有 Blob 存储而不是表存储的诊断设置?

我目前可以通过使用经典门户来解决该问题,但我担心用于诊断的表存储最终将被弃用,因为它尚未包含在新门户的诊断设置中。

最佳答案

(我将对这个问题做一些死灵术,因为这是我在寻找解决方案时发现的最相关的 StackOverflow 问题,因为不再可能通过经典门户来做到这一点)

免责声明:微软似乎已经删除了对登录到 Azure 门户中的表的支持,因此我不知道这是否已被弃用或即将被弃用,但我有一个现在可以使用的解决方案(2017 年 3 月 31 日):

有一些特定的设置决定日志记录,我首先从 Azure Powershell github 中的一个问题中找到了这方面的信息:https://github.com/Azure/azure-powershell/issues/317

我们需要的具体设置是(来自github):

AzureTableTraceEnabled = True, & AppSettings has: DIAGNOSTICS_AZURETABLESASURL

在(GUI 导航)下使用优秀的资源浏览器 ( https://resources.azure.com ):

/subscriptions/{subscriptionName}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{siteName}/config/logs

我能够在属性中找到设置 AzureTableTraceEnabled。

属性 AzureTableTraceEnabled 具有 Level 和 sasURL。根据我的经验,更新这两个值 (Level="Verbose",sasUrl="someSASurl") 将会起作用,因为更新 sasURL 在 appsettings 中设置 DIAGNOSTICS_AZURETABLESASURL。

我们如何改变这个?我是在 Powershell 中完成的。我第一次尝试了 cmdlet Get-AzureRmWebApp,但找不到我想要的东西 - 旧的 Get-AzureWebSite 确实显示 AzureTableTraceEnabled,但我无法更新它(也许具有更多 powershell\azure 经验的人可以输入有关如何使用 ASM cmdlet 来执行此操作)。

对我有用的解决方案是通过 Set-AzureRmResource 命令设置属性,并进行以下设置:

Set-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName "$ResourceGroupName" -ResourceType Microsoft.Web/sites/config -ResourceName "$ResourceName/logs" -ApiVersion 2015-08-01 -Force

$PropertiesObject 看起来像这样:

$PropertiesObject = @{applicationLogs=@{azureTableStorage=@{level="$Level";sasUrl="$SASUrl"}}}

级别对应于“错误”、“警告”、“信息”、“详细”和“关闭”。

也可以在 ARM 模板中执行此操作(重要位位于站点日志资源的属性中):

        {
            "apiVersion": "2015-08-01",
            "name": "[variables('webSiteName')]",
            "type": "Microsoft.Web/sites",
            "location": "[resourceGroup().location]",
            "tags": {
                "displayName": "WebApp"
            },
            "dependsOn": [
                "[resourceId('Microsoft.Web/serverfarms/', variables('hostingPlanName'))]"
            ],
            "properties": {
                "name": "[variables('webSiteName')]",
                "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]"
            },
            "resources": [
            {
                "name": "logs",
                "type": "config",
                "apiVersion": "2015-08-01",
                "dependsOn": [
                    "[resourceId('Microsoft.Web/sites/', variables('webSiteName'))]"
                ],
                "tags": {
                    "displayName": "LogSettings"
                },
                "properties": {
                    "azureTableStorage": {
                        "level": "Verbose",
                        "sasUrl": "SASURL"
                    }
                }
            }
        }

在 ARM 中执行此操作的问题是,我还没有找到生成正确 SAS 的方法,可以提取 Azure 存储帐户 key (来自: ARM - How can I get the access key from a storage account to use in AppSettings later in the template? ) :

"properties": {
    "type": "AzureStorage",
        "typeProperties": {
            "connectionString": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value)]"
    }
}

还有一些巧妙的方法可以使用链接模板生成它们(来自: http://wp.sjkp.dk/service-bus-arm-templates/ )。

我当前寻求的解决方案(时间限制)是一个自定义的 Powershell 脚本,如下所示:

...
$SASUrl = New-AzureStorageTableSASToken -Name $LogTable -Permission $Permissions -Context $StorageContext -StartTime $StartTime -ExpiryTime $ExpiryTime -FullUri
$PropertiesObject = @{applicationLogs=@{azureTableStorage=@{level="$Level";sasUrl="$SASUrl"}}}
Set-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName "$ResourceGroupName" -ResourceType Microsoft.Web/sites/config -ResourceName "$ResourceName/logs" -ApiVersion 2015-08-01 -Force
...

这是一个相当丑陋的解决方案,因为除了 ARM 模板之外,您还需要维护一些额外的东西 - 但它很简单,快速,并且在我们等待 ARM 模板更新时它可以工作(或者对于比我来启发我们)。

关于azure - 为什么无法通过新的 Azure 门户配置 Azure 诊断以使用 Azure 表存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36006676/

相关文章:

c# - Azure SDK 已安装,但 Visual Studio 2012 看不到它

azure - 将数据从 SQL Azure 迁移到 Azure Table 的最佳设计解决方案

azure - 如何使用 Microsoft OAuth 获取另一个资源的访问 token

c# - 部署到 prod 后没有 MediaTypeFormatter 可供读取

asp.net-mvc - 如何创建自己的 webapi 操作并使用 jquery 调用它?

Azure 应用服务 - WEBSITE_HEALTHCHECK_MAXPINGFAILURES 和负载平衡时间

postgresql - 什么是默认的 PostgreSQL 日志格式

MongoDB 日志文件

ruby-on-rails - 如何从 Rails 事件记录中返回查询,以便我可以将它们传回客户端日志控制台

c# - Asp.net 核心 webapi 获取从 Angular4 应用程序发布的空值