[go: up one dir, main page]

0% found this document useful (0 votes)
0 views3 pages

Monitoring a .NET Core Application

Uploaded by

Vijay Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Monitoring a .NET Core Application

Uploaded by

Vijay Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Monitoring a .

NET Core application involves collecting telemetry data such as logs,


metrics, traces, and exceptions to ensure health, performance, and availability.
Here's a **step-by-step guide** covering common tools and practices:

### ✅ 1. **Enable Built-in Logging and Monitoring**

.NET Core comes with built-in support for structured logging and diagnostics:

* **Use ILogger<T>**
* Configure logging providers in `Program.cs`:

builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddDebug();
// Add others like EventLog, ApplicationInsights, Serilog etc.

### ✅ 2. **Structured Logging (with Serilog / NLog)**

**Serilog Example:**

Log.Logger = new LoggerConfiguration()


.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();

builder.Host.UseSerilog();

### ✅ 3. **Health Checks**

Add NuGet: `Microsoft.Extensions.Diagnostics.HealthChecks`

**Configure in Program.cs:**

builder.Services.AddHealthChecks()
.AddSqlServer("YourConnectionString") // Add checks as needed
.AddCheck<CustomHealthCheck>("custom_check");

app.MapHealthChecks("/health");

### ✅ 4. **Application Insights (Azure)**

* Add NuGet: `Microsoft.ApplicationInsights.AspNetCore`


builder.Services.AddApplicationInsightsTelemetry("your-instrumentation-key");
* Tracks: Request telemetry, dependencies, exceptions, custom events.
### ✅ 5. **Distributed Tracing (OpenTelemetry + Jaeger/Zipkin)**

* Add NuGet:

* `OpenTelemetry.Extensions.Hosting`
* `OpenTelemetry.Exporter.Jaeger`

builder.Services.AddOpenTelemetry()
.WithTracing(builder => builder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddJaegerExporter());
### ✅ 6. **Prometheus + Grafana (for Metrics)**

* Expose `/metrics` using **prometheus-net.AspNetCore**

builder.Services.AddMetricServer();
app.UseMetricServer();
app.UseHttpMetrics();

* Monitor .NET GC, requests, custom metrics.


### ✅ 7. **Exception Tracking**

Use global exception middleware:


app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
context.Response.StatusCode = 500;
// log error
await context.Response.WriteAsync("An unexpected error occurred.");
});
});

### ✅ 8. **Custom Middleware for Correlation IDs and Logging**

public class CorrelationIdMiddleware


{
private readonly RequestDelegate _next;
public CorrelationIdMiddleware(RequestDelegate next) => _next = next;
public async Task InvokeAsync(HttpContext context)
{
var correlationId = context.Request.Headers["X-Correlation-
ID"].FirstOrDefault() ?? Guid.NewGuid().ToString();
context.Response.Headers["X-Correlation-ID"] = correlationId;

using (LogContext.PushProperty("CorrelationId", correlationId))


{
await _next(context);
}
}
}

### ✅ 9. **Monitoring Tools Integration**

| Tool | Purpose |
| | - |
| **Azure Monitor** | Logs, metrics, Application Insights |
| **ELK Stack** | Centralized log analysis (Elastic, Logstash, Kibana) |
| **Seq** | Structured log viewer for Serilog |
| **Grafana + Prometheus** | Real-time metrics dashboard |
| **Jaeger/Zipkin** | Distributed tracing visualization |

### Summary:

To monitor a .NET Core application effectively:


* Use **ILogger** and structured logging (e.g., Serilog).
* Configure **Health Checks**.
* Integrate **Application Insights** or **OpenTelemetry**.
* Track **custom metrics** via Prometheus.
* Implement **centralized exception handling**.
* Use **external dashboards** like Grafana, Kibana, or Azure Monitor.

Would you like an end-to-end example using any specific tools like Serilog + Seq or
Application Insights?

You might also like