locked
How to Set Application Logging (Blob) ON and Web Server Logging ON via PowerShell or ARM Template RRS feed

  • Question

  • Hi,

    I am using the Microsoft Azure PowerShell January 2016 Update (v 1.1.0).

    I have a Azure Web App created via Resource Manager mode.

    I want to use either Azure PowerShell cmdlets or ARM template definition to turn on both Application Logging (Blob) and Web Server Logging diagnostic log properties for my web app.

    Can anyone give me some hint or help?

    Thanks very much!

    Monday, January 18, 2016 2:05 PM

Answers

  • Hi Juv,

    You can do it by using the new config/logs provider. I just posted a sample here. For PowerShell, you can use https://resources.azure.com/ to get in the right direction. It will also show you all the extra properties that you can set, e.g. for azureBlobStorage (my ARM sample only does file system).

    thanks,
    David

    • Marked as answer by juvchan Tuesday, January 19, 2016 8:39 PM
    Tuesday, January 19, 2016 7:39 PM

All replies

  • Hi,

    I would suggest you to check this article Enable diagnostics logging for web apps in Azure App Service:

    https://azure.microsoft.com/en-in/documentation/articles/web-sites-enable-diagnostic-log/

    Regards,
    Azam khan

    Tuesday, January 19, 2016 12:04 PM
  • Hi Azam,

    I know this can be done in portal easily.

    However I need to automate it in PowerShell or ARM template for headless deployment.

    Is this possible for now?

    Thanks.

    Tuesday, January 19, 2016 2:12 PM
  • Hi Juv,

    You can do it by using the new config/logs provider. I just posted a sample here. For PowerShell, you can use https://resources.azure.com/ to get in the right direction. It will also show you all the extra properties that you can set, e.g. for azureBlobStorage (my ARM sample only does file system).

    thanks,
    David

    • Marked as answer by juvchan Tuesday, January 19, 2016 8:39 PM
    Tuesday, January 19, 2016 7:39 PM
  • Thanks very much David!
    Tuesday, January 19, 2016 8:39 PM
  • Just 1 more question, the name "logs" under "config" type does not seem to comply with the 2015-08-01 api-version schema for Microsoft.Web.json at here: 

    2015-08-01 Microsoft.Web.Json

    Just wonder will it get updated soon?

    It's because I always refer to the schema from the Microsoft Azure Resource Manager schemas on GitHub, thanks!

    Tuesday, January 19, 2016 8:46 PM
  • Yes, unfortunately the schema file is not up to date, and I'm not sure exactly when it will be updated. But even though it won't validate through the schema, everything should work at template deployment time.
    Wednesday, January 20, 2016 12:18 AM
  • Hi David, thanks very much for the clarifications!
    Wednesday, January 20, 2016 5:40 AM
  • Hi Juvchan,

    You can use below PowerShell script to enable Application and Web Server Logging programmatically. Assumption, you have web app and storage account already created in your resource group.

    <#
    .SYNOPSIS
        Configures web app to store application diagnostics.    
     
    .DESCRIPTION
        Creates a containers and table in a storage of given resource group, 
        and updates web app properties to store the application and http logs in the created storage containers and table.
     
    .NOTES
        File Name: ConfigureApplicationDiagnostics.ps1
        Author: Milind Patil
        Prerequisite: Powershell 3.0, Azure Module
     
    .PARAMETER resourceGroupName
        The string containing name of the resource group where web app and storage is created.
        This is a mandatory parameter.
     
    .PARAMETER appLogContainerName
        The string containing name to be given to a container to store application logs.
        This is a mandatory parameter.
        
    .PARAMETER httpLogContainerName
        The string containing name to be given to a container to store http logs.
        This is a mandatory parameter.
        
    .PARAMETER tableName
        The string containing name to be given to a table to application http logs.
        This is a mandatory parameter.
    
    .PARAMETER webAppName
        The string containing name of the web app to configure application diagnostics.
        This is a mandatory parameter.
    
    .PARAMETER storageAccountName
        The string containing name of the storage account where applocation diagnostic logs will get written.
        This is a mandatory parameter.
        
    .EXAMPLE
        .\ConfigureApplicationDiagnostics.ps1 -resourceGroupName "[resourceGroupName]" -appLogContainerName "[appLogContainerName]" -httpLogContainerName "[httpLogContainerName]" -tableName "[tableName]" -webAppName "[webAppName]" -storageAccountName "[storageAccountName]"
     
    .EXAMPLE
        .\ConfigureApplicationDiagnostics.ps1 -resourceGroupName "Test-ResourceGroup" -appLogContainerName "testapplogcontainer" -httpLogContainerName "testhttplogcontainer" -tableName "testportaltable" -webAppName "TestPortal" -storageAccountName "testlogstorage"
    #>
    
    Param(
    [parameter(Mandatory=$true)]
    $resourceGroupName,
    [parameter(Mandatory=$true)]
    $appLogContainerName,
    [parameter(Mandatory=$true)]
    $httpLogContainerName,
    [parameter(Mandatory=$true)]
    $tableName,
    [parameter(Mandatory=$true)]
    $webAppName,
    [parameter(Mandatory=$true)]
    $storageAccountName
    
    )
    
    
    function Get-PSscriptInformation {
     <#
      .SYNOPSIS
        Returns current script information.
    
      .DESCRIPTION
        Returns current script information.
                    
      .INPUTS
        -
          
      .OUTPUTS
        Script Information
      
      .EXAMPLE
        Get-PSscriptInformation
      #>
      $PSscript = Get-Item $MyInvocation.ScriptName
      Return $PSscript
    }
    
    #################################################################################################################################
    #                                                  Main Script                                                                  #
    #################################################################################################################################
    
        $PSscript = Get-PSscriptInformation
        
    
        Write-Host "***************************************************************************************************`n"
        Write-Host "Running script [$PSscript.Name]. Started processing at [$([DateTime]::Now)].`n"
        Write-Host "***************************************************************************************************`n"
        
        #Load Azure Module
    	$azureModule=Get-Module -ListAvailable -Name Azure -Refresh
    	$Message = "Executing script on Azure PowerShell Version: {0}.{1}.{2}" -f $azureModule.Version.Major, $azureModule.Version.Minor, $azureModule.Version.Build
    	Write-Host $Message
    
        #Get Azure Web App resource 
    	$webAppLogResource = Get-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $webAppName/logs -ApiVersion 2015-08-01
    	$propertyObject = $webAppLogResource.Properties
    	
        #Get property values in a variables
    	$appLogContainerSasUrl = $propertyObject.applicationLogs.azureBlobStorage.sasUrl
    	$tableSasUrl = $propertyObject.applicationLogs.azureTableStorage.sasUrl
    	$httpLogContainerSasUrl = $propertyObject.httpLogs.azureBlobStorage.sasUrl
    
        #Get Storage account
    	$storageAccount = (Get-AzureRmStorageAccount  -ResourceGroupName  $resourceGroupName -Name $storageAccountName)
        
        if($storageAccount -ne $null)
        {
            #Get Storage account primary key
    	    $storageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccount.StorageAccountName)[0].Value
    
            #Create storage context
    	    $context = New-AzureStorageContext  -StorageAccountName $storageAccount.StorageAccountName -StorageAccountKey $storageAccountKey
    
            #Get containers and table from storage with given name
    	    $appLogContainer = Get-AzureStorageContainer -Name $appLogContainerName -Context $context -ErrorAction SilentlyContinue
    	    $httpLogContainer = Get-AzureStorageContainer -Name $httpLogContainerName -Context $context -ErrorAction SilentlyContinue
    	    $storageTable = Get-AzureStorageTable -Name $tableName -Context $context -ErrorAction SilentlyContinue
    
            #Check if already exists, else create containers and table
    	    if($storageTable -eq $null)
    	    {
    		    $storageTable = New-AzureStorageTable -Name $tableName -Context $context
    	    }
    
    	    if($appLogContainer -eq $null)
    	    {
    		    $appLogContainer = New-AzureStorageContainer -Name $appLogContainerName -Context $context
    	    }
    
    	    if($httpLogContainer -eq $null)
    	    {
    		    $httpLogContainer = New-AzureStorageContainer -Name $httpLogContainerName  -Context $context
    	    }
    	    if($appLogContainerSasUrl.Length -eq 0)
    	    {
    		    $appLogContainerSasUrl = New-AzureStorageContainerSASToken -Name $appLogcontainer.Name -Context $context -Permission rwl -FullUri -ExpiryTime (Get-Date).AddDays(99999)   
    	    }
    	    if($tableSasUrl.Length -eq 0)
    	    {
    		    $tableSasUrl = New-AzureStorageTableSASToken -Name $storageTable.Name -Context $context -Permission rau -FullUri -ExpiryTime (Get-Date).AddDays(99999)
    	    }
    	    if($httpLogContainerSasUrl.Length -eq 0)
    	    {
    		    $httpLogContainerSasUrl = New-AzureStorageContainerSASToken -Name $httpLogContainer.Name -Context $context -Permission rwl -FullUri -ExpiryTime (Get-Date).AddDays(99999)
    	    }
    
            #Assign properties with values.
    	
    		    $propertyObject.applicationLogs.azureTableStorage.level = "Error"		
    		    $propertyObject.applicationLogs.azureTableStorage.sasUrl = "$tableSasUrl"
    		    $propertyObject.applicationLogs.azureBlobStorage.level = "Information"
    		    $propertyObject.applicationLogs.azureBlobStorage.sasUrl = "$appLogContainerSasUrl"
    		    $propertyObject.applicationLogs.azureBlobStorage.retentionInDays = 90
    		    $propertyObject.httpLogs.fileSystem.enabled = $false				
    		    $propertyObject.httpLogs.azureBlobStorage.sasUrl = "$httpLogContainerSasUrl"
    		    $propertyObject.httpLogs.azureBlobStorage.retentionInDays = 90
    		    $propertyObject.httpLogs.azureBlobStorage.enabled = $true
    		    $propertyObject.failedRequestsTracing.enabled = $true
    		    $propertyObject.detailedErrorMessages.enabled = $true
    	
    	    Set-AzureRmResource -PropertyObject $propertyObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config `
    								    -ResourceName $webAppName/logs -ApiVersion 2015-08-01 -Force
    	}
        else
        {
            $Message = "Storage account with name {0} does not exist in a resource group {1}" -f $storageAccountName , $resourceGroupName
            Write-Host $Message
        }
        Write-Host "`n***************************************************************************************************"
        Write-Host "`nFinished processing at [$([DateTime]::Now)]."
        Write-Host "`n***************************************************************************************************"
    
    #################################################################################################################################
    Hope this will help.

    Thanks.

    Thursday, April 6, 2017 9:41 AM