Skip to content

04 - Logging & Monitoring (Premium)

Configure Application Insights and structured logging for your Premium (EP1) PowerShell app.

Enable Application Insights

az monitor app-insights component create \
  --app func-ps-demo-ai \
  --location $LOCATION \
  --resource-group $RG

az functionapp config appsettings set \
  --name $APP_NAME \
  --resource-group $RG \
  --settings "APPLICATIONINSIGHTS_CONNECTION_STRING=<connection-string>"
| Command/Parameter | Purpose | | --- | --- | | az monitor app-insights component create | Create the Application Insights component for telemetry. | | --app | Name of the Application Insights component. | | --location | Azure region for the component. | | --resource-group | Resource group that contains the component. | | az functionapp config appsettings set | Point the function app at Application Insights. | | --name | Name of the function app. | | --resource-group | Resource group that contains the function app. | | --settings | Set the Application Insights connection string setting. |

Logging in PowerShell

Use the standard streams — they flow to Application Insights automatically:

param($Request, $TriggerMetadata)

Write-Information "Processing request for $($Request.Query.Name)"
Write-Warning "Name was empty; using default"
Write-Error "Downstream call failed"
Cmdlet Severity
Write-Information / Write-Host Information
Write-Warning Warning
Write-Error Error

Stream Live Logs

az functionapp log tail --name $APP_NAME --resource-group $RG
| Command/Parameter | Purpose | | --- | --- | | az functionapp log tail | Stream live log output from the function app. | | --name | Name of the function app. | | --resource-group | Resource group that contains the function app. |

Verification

Invoke the function, then query Application Insights:

traces
| where timestamp > ago(15m)
| order by timestamp desc

You should see your Write-Information messages.

Next Steps

Codify the whole environment as Infrastructure as Code.

Next: 05 - Infrastructure as Code

See Also

Sources