IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| IServiceCollection and IApplicationBuilder | DI Service Lifetimes | |
Centralized logging and monitoring in .NET Core |
To implement centralized logging and monitoring in a .NET Core application, you will typically use a structured logging framework like Serilog, an observability standard like OpenTelemetry, and a centralized platform for log aggregation and visualization, such as Seq or the Elastic Stack.
The general architecture for centralized logging and monitoring in .NET Core involves three main parts:
ASP.NET Core has a built-in ILogger<T> abstraction that works with multiple providers.
This allows you to plug in different backends (console, files, Elasticsearch, Application Insights, etc.) without changing your code.
public class WeatherController : ControllerBase
{
private readonly ILogger<WeatherController> _logger;
public WeatherController(ILogger<WeatherController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult GetWeather()
{
_logger.LogInformation("Weather endpoint hit at {time}", DateTime.UtcNow);
return Ok("Sunny");
}
}
Instead of plain text logs, use structured logging for better querying and filtering.
dotnet add package Serilog.AspNetCore dotnet add package Serilog.Sinks.Console dotnet add package Serilog.Sinks.Elasticsearch
using Serilog;
var builder = WebApplication.CreateBuilder(args);
// Configure Serilog
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
{
AutoRegisterTemplate = true,
})
.CreateLogger();
builder.Host.UseSerilog();
var app = builder.Build();
app.MapControllers();
app.Run();
Now logs go both to the console and to Elasticsearch, which you can visualize using Kibana.
Common stacks for centralization:
Logs alone aren’t enough—you also need metrics and tracing.
Use OpenTelemetry: OpenTelemetry provides distributed tracing, metrics, and logs.
dotnet add package OpenTelemetry.Extensions.Hosting dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol dotnet add package OpenTelemetry.Instrumentation.AspNetCore dotnet add package OpenTelemetry.Instrumentation.Http
using OpenTelemetry.Trace;
using OpenTelemetry.Metrics;
builder.Services.AddOpenTelemetry()
.WithTracing(tracerProvider => tracerProvider
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter(opt =>
{
opt.Endpoint = new Uri("http://otel-collector:4317");
}))
.WithMetrics(metricsProvider => metricsProvider
.AddAspNetCoreInstrumentation()
.AddRuntimeInstrumentation()
.AddOtlpExporter(opt =>
{
opt.Endpoint = new Uri("http://otel-collector:4317");
}));
Export data to Prometheus, Jaeger, or Grafana Tempo for visualization.
Use a dedicated error-tracking service:
These provide alerting, dashboards, and user-impact analysis.
| IServiceCollection and IApplicationBuilder | DI Service Lifetimes | |