Skip to content

Monitoring and Logging

⏱ 30 minutes intermediate
📜Corecms

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.


Optimizely Cloud environments come with Application Insights pre-configured. To use your own instance or customize the configuration:

  1. In the Optimizely Cloud management portal, navigate to Monitoring
  2. Copy the Application Insights connection string
  3. Alternatively, use your own Application Insights resource from Azure

Add the Application Insights SDK to your project:

Terminal window
dotnet add package Microsoft.ApplicationInsights.AspNetCore

Configure 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=...

Structured logging produces machine-readable log entries that you can query and filter in Application Insights.

Terminal window
dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Sinks.ApplicationInsights

Configure 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);
});

Set different verbosity per environment using environment variables:

EnvironmentLog levelRationale
IntegrationDebugMaximum detail for development
PreproductionInformationBalanced detail for staging validation
ProductionWarningMinimize noise; capture problems
Serilog__MinimumLevel__Default = Warning

For live debugging, stream logs from your running application:

  1. Navigate to your project > Logs
  2. Select the environment
  3. Click Live Stream
  4. Logs appear in real time as requests are processed
Terminal window
opti logs stream --environment Integration --level Warning
  1. Open your Application Insights resource in the Azure portal
  2. Navigate to Live Metrics
  3. View request rates, failure rates, and live log entries

Health checks let the platform and your monitoring tools verify that your application is functioning correctly.

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
});
StatusHTTP codeMeaning
Healthy200All checks passed
Degraded200Some non-critical checks failed
Unhealthy503Critical checks failed

The platform load balancer uses health checks to route traffic only to healthy instances.


  • 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%
  • 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
  • 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

Configure alerts in Application Insights to notify your team of problems:

  1. In Application Insights, go to Alerts > New Alert Rule
  2. Define the condition (e.g., failure rate > 5% over 5 minutes)
  3. Set the action group (email, SMS, webhook, PagerDuty)
  4. 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