Deploying Web Apps with Azure App Services: A Complete Beginner's Guide

3.48K 0 0 0 0

📘 Chapter 5: Monitoring, Logging, and Cost Management

🔍 Overview

Managing a web app on Azure goes beyond just deployment. Once your app is live, it's crucial to track its performance, diagnose issues, understand usage trends, and control costs. Azure App Services offers a comprehensive toolset for real-time monitoring, robust logging, and detailed cost management.

In this chapter, we explore how to:

  • Enable and analyze logs
  • Use Application Insights
  • Set up alerts and diagnostics
  • Track billing and resource usage
  • Optimize app performance and budget

🧠 1. Overview of Monitoring Tools in Azure App Services

Azure offers a suite of tools for observability:

Tool/Service

Purpose

Application Insights

End-to-end app monitoring and performance alerts

Azure Monitor

Infrastructure-level metrics and logs

Diagnostic Logs

HTTP logs, error logs, request tracing

Log Analytics

Centralized querying and visualization

Cost Management + Billing

Budgeting, usage tracking, forecasts


🔧 2. Enabling Diagnostic Logging

Diagnostic logs provide real-time insight into app behavior.

Enable Application Logs

bash

 

az webapp log config \

  --name mywebapp \

  --resource-group MyResourceGroup \

  --application-logging true \

  --level information

Enable Web Server Logs (Filesystem)

bash

 

az webapp log config \

  --name mywebapp \

  --resource-group MyResourceGroup \

  --web-server-logging filesystem \

  --retention 7


📁 Log Types

Log Type

Description

Application Logs

stdout/stderr logs from your app

Web Server Logs

HTTP requests (IIS/Apache/Nginx equivalent)

Detailed Error Logs

Error-specific traces (500, 404, etc.)

Failed Request Tracing

Low-level diagnostics for failed HTTP requests


📈 3. Integrating Application Insights

Application Insights offers deep telemetry, real-time alerts, and distributed tracing.

Step 1: Create Application Insights

bash

 

az monitor app-insights component create \

  --app MyInsights \

  --location eastus \

  --resource-group MyResourceGroup \

  --application-type web

Step 2: Link with App

bash

 

az webapp config appsettings set \

  --resource-group MyResourceGroup \

  --name mywebapp \

  --settings APPINSIGHTS_INSTRUMENTATIONKEY=<YOUR-KEY>

Common Metrics Tracked

Metric Name

Use Case

Request count

App load trend analysis

Response time

Performance benchmarking

Failed requests

Error diagnostics

Dependency failures

External API/db issues

Live metrics

Real-time monitoring of traffic/usage


🔍 Query with Kusto (Log Analytics)

kusto

 

requests

| where timestamp > ago(1h)

| summarize count() by resultCode


🔔 4. Setting Up Alerts

Set alerts when your app exceeds thresholds or fails:

Example: Alert on high CPU

  1. Go to Azure Portal → Monitor → Alerts
  2. Create a rule
  3. Target your App Service
  4. Set condition: CPU Usage > 80% for 5 minutes
  5. Choose action: Email, Webhook, Azure Function

📋 Alert Use Cases

Scenario

Recommended Alert

App goes down

Ping test with action group

CPU consistently high

CPU % > 80 for 5 mins

Too many failed HTTP requests

Requests where resultCode >= 500

App Insights exceptions spiking

Custom metric alert


🔍 5. Log Streaming and Real-Time Debugging

For live log visibility:

bash

 

az webapp log tail \

  --name mywebapp \

  --resource-group MyResourceGroup

This command streams logs to your terminal in real time.


📊 6. Cost Management and Optimization

View Cost Breakdown

Navigate to:
Azure Portal → Cost Management + Billing → Cost Analysis

Track:

  • Per-app cost
  • Per-resource group
  • Forecasts & anomalies

CLI Cost Breakdown via Cost Management APIs

bash

 

az consumption usage list \

  --start-date 2024-04-01 \

  --end-date 2024-04-22


📋 Common Cost Triggers in App Services

Cost Driver

How to Reduce

Premium SKU

Scale down if over-provisioned

Autoscale overrun

Set instance limits and schedules

Excess logging

Shorten retention / disable verbose

App Insights sampling

Enable adaptive sampling


🧠 Budget Alerts

Set budgets with alert thresholds:

bash

 

az consumption budget create \

  --amount 50 \

  --name MyAppBudget \

  --resource-group MyResourceGroup \

  --time-grain monthly \

  --time-period start=2024-04-01 end=2024-12-31 \

  --notification-key email \

  --threshold 0.8 \

  --contact-emails user@example.com


🧪 7. Optimization Best Practices

  • Enable GZIP Compression
  • Turn on Always On for background tasks
  • Use deployment slots to separate staging/prod
  • Minimize cold starts with appropriate plan tier
  • Reduce logs verbosity in production

🧩 Summary Table – Monitoring & Cost Management Tools


Tool

Function

Application Insights

App performance + logs + exceptions

Diagnostic Logs

Request/response logs + error tracing

Azure Monitor Alerts

Notifications for app or resource states

Log Stream (CLI/Portal)

Real-time debugging

Azure Cost Analysis

Cost breakdowns + forecasts

Budget Alerts

Notify on cost thresholds

Back

FAQs


❓1. What is Azure App Service?

Answer:
Azure App Service is a fully managed Platform as a Service (PaaS) from Microsoft that allows you to host web applications, RESTful APIs, and mobile backends. It supports various languages like .NET, Node.js, Python, Java, and PHP.

❓2. What types of applications can I deploy on Azure App Service?

Answer:
You can deploy web apps (e.g., React, Angular, .NET MVC), APIs (Node.js, Flask, Express), static sites, background jobs, and containerized applications. Azure App Service supports both Linux and Windows environments.

❓3. Does Azure App Service support custom domains and SSL?

Answer:
Yes. You can map a custom domain to your web app and enable HTTPS using either App Service-managed SSL certificates or your own custom certificates.

❓4. How do I deploy my application to Azure App Service?

Answer:
You can deploy using:

  • Visual Studio or VS Code
  • Azure CLI (az webapp deploy)
  • GitHub Actions or Azure DevOps
  • FTP/Zip deploy
  • Docker and Azure Container Registry

❓5. Can Azure App Service scale automatically?

Answer:
Yes. App Services can scale vertically (increase compute resources) or horizontally (add instances). Autoscaling rules can be based on CPU usage, memory, or HTTP queue length.

❓6. What is the difference between App Service Plan and App Service?

Answer:
An App Service Plan defines the region, OS, pricing tier, and resource allocation (CPU/RAM) for one or more web apps. The App Service is the actual web application hosted within that plan.

❓7. How does deployment slot swapping work?

Answer:
Deployment slots (e.g., staging, production) allow you to deploy your app to a staging environment, test it, and then swap it into production without downtime.

❓8. What pricing options are available for App Services?

Answer:
Azure App Services offer:

  • Free Tier: for learning and testing
  • Shared and Basic: for small workloads
  • Standard and Premium: for production apps with scaling, staging, and high availability
    Pricing depends on compute size, number of instances, and features.



❓9. Is Azure App Service secure?

Answer:
Yes. It offers built-in security features such as HTTPS, DDoS protection, Azure Active Directory authentication, integration with Azure Key Vault, and compatibility with Azure Defender.

❓10. Can I use Azure App Service for CI/CD?

Answer:
Absolutely. Azure App Service integrates with GitHub, Bitbucket, and Azure DevOps for automated deployments and pipelines. It also supports custom scripts and Docker builds.