Enabling Azure Diagnostics extension through Powershell

Azure SDK 2.5 uses the extension model for the diagnostics. The configuration and the connection string to the diagnostic storage are no longer part of the deployment package and cscfg. All the diagnostics configuration is contained within the wadcfgx which is attached as sample. The advantage we get is that diagnostics agent and settings are decoupled from the project and can be dynamically enabled and updated even after your application is deployed. 

 The MSDN article on configuring diagnostics in Visual Studio on Azure is explained in the msdn article https://msdn.microsoft.com/en-us/library/azure/dn186185.aspx#BK_Migration.

The PowerShell cmdlets for managing the diagnostics extensions on a Cloud Service are -

Steps to configure diagnostics through powershell commands :

1. The Diagnostics.wadcfgx file that got generated from Visual Studio needs to be modified. The changes that we need to do are as follows:  

a. Remove the line : <DiagnosticsConfiguration xmlns="https://schemas.microsoft.com/ServiceHosting/2010/10/DiagnosticsConfiguration">. Remove its closing tag also.

b. Delete the PrivateConfig section as shown below:

  <PrivateConfig xmlns="https://schemas.microsoft.com/ServiceHosting/2010/10/DiagnosticsConfiguration">

     <StorageAccount name=" " endpoint="https://core.windows.net/" />

   </PrivateConfig>

   <IsEnabled>false</IsEnabled>

c. <?xml version="1.0" encoding="utf-8"?> should be the first line of the file.

 

You can also create your own file with the extension .wadcfgx or .xml. The schema should be as below.

 <?xml version="1.0" encoding="utf-8"?>

  <PublicConfig xmlns="https://schemas.microsoft.com/ServiceHosting/2010/10/DiagnosticsConfiguration">
 <WadCfg>
 <DiagnosticMonitorConfiguration overallQuotaInMB="4096">
 <WindowsEventLog>
 <DataSource name="Application!*" />
 </WindowsEventLog>
 <CrashDumps dumpType="Full">
 <CrashDumpConfiguration processName="WaAppAgent.exe" />
 <CrashDumpConfiguration processName="WaIISHost.exe" />
 <CrashDumpConfiguration processName="WindowsAzureGuestAgent.exe" />
 <CrashDumpConfiguration processName="WaWorkerHost.exe" />
 <CrashDumpConfiguration processName="DiagnosticsAgent.exe" />
 <CrashDumpConfiguration processName="w3wp.exe" />
 </CrashDumps>
 </DiagnosticMonitorConfiguration>
 </WadCfg>
 <StorageAccount>samplename</StorageAccount>
 </PublicConfig> 

Now execute the below commands in PowerShell : 

 $storage_name=" < storage account name where the diagnostics will be captured > "
$key=" < key of the Storage Account > "
$service_name=" < cloud service name > "
$public_config=" < location path of the diagnostics.wadcfgx file > "
$azureSubscriptionName=" < azure subscription id > " 
$thumbprint=" < server certificate's thumbprint > " 
$subscriptionId=" < azure subscription id > " 
  
 $mycert = Get-Item cert:\\CurrentUser\My\$thumbprint
Set-AzureSubscription -SubscriptionName $azureSubscriptionName -SubscriptionId $subscriptionId -Certificate $mycert
Select-AzureSubscription $azureSubscriptionName

$storageContext = New-AzureStorageContext –StorageAccountName $storage_name –StorageAccountKey $key

Set-AzureServiceDiagnosticsExtension -StorageContext $storageContext -DiagnosticsConfigurationPath $public_config –ServiceName $service_name -Slot ‘Production’ -Role $role_name


To verify that the Diagnostics is successfully configured on cloud, use the following command:

   Get-AzureServiceDiagnosticsExtension -ServiceName “ < cloud service name> ”

  
Please Note: 

To get the values of subscription id and azure subscription name, you can use the following command :

Get-AzurePublishSettingsFile

It will automatically download a .publishsettings files which will contain all the details of your subscriptions of Azure.

I have attached the powershell script as well.
 

AzureDiagnostics.ps1