☁️ Introduction
Full stack .NET applications, with a backend (ASP.NET Core), frontend (React, Angular, or Blazor), and cloud services, require robust monitoring and logging for:
Detecting errors and exceptions
Tracking performance and latency
Auditing user activity
Proactively alerting on issues
Cloud environments provide scalable tools to collect, analyze, and visualize logs and metrics.
๐งฉ 1. Logging in ASP.NET Core
.NET Core has a built-in logging abstraction: ILogger. It supports multiple providers like Console, Debug, File, and Cloud providers.
Example: Configure Logging in Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddDebug();
builder.Logging.AddAzureWebAppDiagnostics(); // Logs to Azure if deployed
var app = builder.Build();
app.MapGet("/", (ILogger<Program> logger) =>
{
logger.LogInformation("Request received at {Time}", DateTime.UtcNow);
return "Hello World!";
});
app.Run();
✅ Key Points:
Use structured logging ({Placeholder}) instead of string concatenation
Use different log levels: Trace, Debug, Information, Warning, Error, Critical
Log enough context (user ID, request ID, correlation ID)
๐ฆ 2. Centralized Logging with Cloud Services
Azure Options:
Azure Monitor / Application Insights:
Automatically collects requests, dependencies, exceptions, and performance metrics
Supports distributed tracing for full stack apps
Log Analytics: Query logs using Kusto Query Language (KQL)
Example: Add Application Insights
dotnet add package Microsoft.ApplicationInsights.AspNetCore
In Program.cs:
builder.Services.AddApplicationInsightsTelemetry(builder.Configuration["ApplicationInsights:InstrumentationKey"]);
Then track custom events or exceptions:
var telemetry = app.Services.GetRequiredService<TelemetryClient>();
telemetry.TrackEvent("CustomEvent");
telemetry.TrackException(new Exception("Test exception"));
๐ก Benefits:
Correlates backend API calls with frontend requests
Detects performance bottlenecks
Provides alerts and dashboards
๐ 3. Logging from the Frontend
Frontend logs are also critical, especially for SPAs:
Use console logging during development
Send client-side logs to backend using a logging API
Use Application Insights JavaScript SDK for monitoring:
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
const appInsights = new ApplicationInsights({
config: {
instrumentationKey: "<YOUR_KEY>"
}
});
appInsights.loadAppInsights();
appInsights.trackPageView();
appInsights.trackException({ error: new Error("Frontend error") });
✅ This allows you to correlate frontend errors with backend logs.
๐ 4. Monitoring Performance Metrics
Monitoring is as important as logging:
Key Metrics to Track:
Backend API:
Request count & duration
HTTP status codes (4xx, 5xx)
Database query performance
Memory and CPU usage
Frontend:
Page load times
Resource load failures
SPA routing performance
Database / Cloud Services:
Connection pool usage
Query latency
Storage capacity
Tools:
Azure Monitor for full-stack metrics
Application Insights dashboards
Prometheus + Grafana (for Kubernetes deployments)
๐จ 5. Setting Up Alerts
Alerts help you proactively detect issues:
Define thresholds, e.g., Server response time > 1s or HTTP 500 > 5/min
Use Azure Monitor alerts or Application Insights alert rules
Integrate with Teams, Slack, or email for notifications
Example: Alert when 500 Internal Server Error rate exceeds 1%
๐ง 6. Distributed Tracing
Full stack apps often involve multiple services. Distributed tracing links requests across services:
Backend: Use Activity and OpenTelemetry
Frontend: Correlate with backend via request-id headers
Azure Application Insights automatically handles correlation if configured
builder.Services.AddOpenTelemetryTracing(tracerProviderBuilder =>
{
tracerProviderBuilder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddAzureMonitorTraceExporter(options =>
{
options.ConnectionString = "<YOUR_CONNECTION_STRING>";
});
});
๐ก Best Practices
Practice Recommendation
Structured logs Use JSON format for easier analysis
Centralized logging Avoid logs scattered across VMs or containers
Log levels Use Information for routine, Error for failures
Correlation IDs Track requests across frontend, backend, and database
Sensitive data Never log passwords, tokens, or PII
Monitoring dashboards Set up dashboards for KPIs and SLA metrics
Alerts Always have actionable alerts for errors and performance degradation
๐ฌ Summary
Monitoring and logging in cloud-hosted full stack .NET applications involve structured logging, centralized collection, performance metrics, distributed tracing, and alerts. Using tools like Application Insights, Azure Monitor, and OpenTelemetry, you can gain full visibility into both backend and frontend, allowing proactive maintenance and faster issue resolution.
Learn Dot Net Course in Hyderabad
Read More
Setting Up a CI/CD Pipeline for Full Stack .NET on Azure DevOps
Managing Secrets and Configuration in .NET Core
How to Scale Your Full Stack .NET Application on the Cloud
Introduction to Kubernetes with .NET Core
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments