Monitoring and Logging
Why monitoring matters
Section titled “Why monitoring matters”You cannot fix what you cannot see. Without monitoring, you learn about problems from angry users instead of from your dashboard. Application Insights and structured logging give you visibility into request performance, error rates, and application behavior so you can detect and resolve issues before they impact your audience.
Connect Application Insights
Section titled “Connect Application Insights”Optimizely Cloud environments come with Application Insights pre-configured. To use your own instance or customize the configuration:
Step 1: Get your instrumentation key
Section titled “Step 1: Get your instrumentation key”- In the Optimizely Cloud management portal, navigate to Monitoring
- Copy the Application Insights connection string
- Alternatively, use your own Application Insights resource from Azure
Step 2: Configure in your application
Section titled “Step 2: Configure in your application”Add the Application Insights SDK to your project:
dotnet add package Microsoft.ApplicationInsights.AspNetCoreConfigure in Program.cs:
builder.Services.AddApplicationInsightsTelemetry(options =>{ options.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];});Step 3: Set the connection string per environment
Section titled “Step 3: Set the connection string per environment”In the management portal, add the environment variable:
ApplicationInsights__ConnectionString = InstrumentationKey=your-key;IngestionEndpoint=...Configure structured logging
Section titled “Configure structured logging”Structured logging produces machine-readable log entries that you can query and filter in Application Insights.
Set up Serilog
Section titled “Set up Serilog”dotnet add package Serilog.AspNetCoredotnet add package Serilog.Sinks.ApplicationInsightsConfigure in Program.cs:
builder.Host.UseSerilog((context, config) =>{ config .ReadFrom.Configuration(context.Configuration) .WriteTo.ApplicationInsights( TelemetryConfiguration.Active, TelemetryConverter.Traces) .Enrich.WithProperty("Environment", context.HostingEnvironment.EnvironmentName);});Log level per environment
Section titled “Log level per environment”Set different verbosity per environment using environment variables:
| Environment | Log level | Rationale |
|---|---|---|
| Integration | Debug | Maximum detail for development |
| Preproduction | Information | Balanced detail for staging validation |
| Production | Warning | Minimize noise; capture problems |
Serilog__MinimumLevel__Default = WarningStream logs in real time
Section titled “Stream logs in real time”For live debugging, stream logs from your running application:
Via the management portal
Section titled “Via the management portal”- Navigate to your project > Logs
- Select the environment
- Click Live Stream
- Logs appear in real time as requests are processed
Via the CLI
Section titled “Via the CLI”opti logs stream --environment Integration --level WarningVia Application Insights
Section titled “Via Application Insights”- Open your Application Insights resource in the Azure portal
- Navigate to Live Metrics
- View request rates, failure rates, and live log entries
Set up health checks
Section titled “Set up health checks”Health checks let the platform and your monitoring tools verify that your application is functioning correctly.
Add health check endpoints
Section titled “Add health check endpoints”builder.Services.AddHealthChecks() .AddSqlServer( builder.Configuration.GetConnectionString("EPiServerDB"), name: "database") .AddUrlGroup( new Uri("https://search.example.com/health"), name: "search-service");
app.MapHealthChecks("/health", new HealthCheckOptions{ ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse});Health check endpoint responses
Section titled “Health check endpoint responses”| Status | HTTP code | Meaning |
|---|---|---|
| Healthy | 200 | All checks passed |
| Degraded | 200 | Some non-critical checks failed |
| Unhealthy | 503 | Critical checks failed |
The platform load balancer uses health checks to route traffic only to healthy instances.
Key metrics to monitor
Section titled “Key metrics to monitor”Request performance
Section titled “Request performance”- Average response time — Target under 500ms for page requests
- P95 response time — The slowest 5% of requests; target under 2 seconds
- Failed requests — Percentage of 5xx responses; target under 0.1%
Application health
Section titled “Application health”- CPU utilization — Sustained above 70% triggers auto-scaling
- Memory usage — Watch for memory leaks causing gradual increase
- Exception rate — Spike in exceptions indicates a code or dependency issue
Dependencies
Section titled “Dependencies”- Database query time — Slow queries drag down overall performance
- External API latency — Third-party services can become bottlenecks
- Cache hit ratio — Low hit ratio means excessive origin load
Set up alerts
Section titled “Set up alerts”Configure alerts in Application Insights to notify your team of problems:
- In Application Insights, go to Alerts > New Alert Rule
- Define the condition (e.g., failure rate > 5% over 5 minutes)
- Set the action group (email, SMS, webhook, PagerDuty)
- Name and save the alert
Recommended alerts:
- Server response time exceeds 3 seconds (5-minute average)
- Failed request percentage exceeds 5%
- Exception count exceeds 50 in 5 minutes
- Health check endpoint returns unhealthy