How .NET Core Handles Logging and Monitoring
In ASP.NET Core / .NET Core, logging is built-in using the Microsoft.Extensions.Logging framework.
This allows you to log application behavior, errors, performance, and debugging information.
✅ Why Logging is Important
Logging helps you:
✅ Debug issues
✅ Track errors
✅ Monitor performance
✅ Audit user actions
✅ Production troubleshooting
✅ Built-in Logging Providers in .NET Core
.NET Core supports multiple logging providers:
| Provider | Purpose |
|---|---|
| Console | Development logging |
| Debug | Visual Studio debugging |
| EventSource | Windows event logs |
| Azure App Service | Cloud logging |
Example built-in providers:
builder.Logging.AddConsole();
builder.Logging.AddDebug();
✅ How to Use Logging in .NET Core
Step 1: Inject ILogger
public class UserService
{
private readonly ILogger<UserService> _logger;
public UserService(ILogger<UserService> logger)
{
_logger = logger;
}
}
✅ Logging Example
_logger.LogInformation("User created successfully");
_logger.LogWarning("User not found");
_logger.LogError("Something went wrong");
✅ Logging Levels
| Level | When to Use |
|---|---|
| Trace | Detailed debugging |
| Debug | Debugging |
| Information | Normal flow |
| Warning | Unexpected situation |
| Error | Errors |
| Critical | System crash |
Example:
_logger.LogInformation("Application started");
_logger.LogWarning("Memory usage high");
_logger.LogError("Database connection failed");
✅ Logging in Controller Example
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
private readonly ILogger<UsersController> _logger;
public UsersController(ILogger<UsersController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult Get()
{
_logger.LogInformation("Getting users");
return Ok();
}
}
✅ Configure Logging in appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"System": "Warning"
}
}
}
✅ Production Logging Tools (Real World)
Most companies use:
-
Serilog
-
NLog
-
log4net
Example Using Serilog (Most Popular)
Install packages:
Serilog.AspNetCore
Serilog.Sinks.File
Program.cs:
Log.Logger = new LoggerConfiguration()
.WriteTo.File("logs/log.txt")
.CreateLogger();
builder.Host.UseSerilog();
Monitoring in .NET Core
Monitoring tools help track:
-
Performance
-
Requests
-
Failures
-
Dependencies
Popular Monitoring Tools:
-
Application Insights
-
Prometheus
-
Grafana
Example Using Application Insights
Install:
Microsoft.ApplicationInsights.AspNetCore
Program.cs:
builder.Services.AddApplicationInsightsTelemetry();
Automatically tracks:
✅ Requests
✅ Exceptions
✅ Dependencies
✅ Performance
Interview Best Answer
If interviewer asks:
How does .NET Core handle logging?
You can answer:
".NET Core uses Microsoft.Extensions.Logging for built-in logging.
We inject ILogger into classes and log messages using different levels like Information, Warning, and Error.
For production, we use tools like Serilog and Application Insights."
Real-World Best Practice
✔ Use ILogger everywhere
✔ Log meaningful messages
✔ Use structured logging
✔ Use external monitoring tools
✔ Log exceptions globally
Example Best Practice
try
{
// code
}
catch(Exception ex)
{
_logger.LogError(ex, "Error occurred while processing request");
}
Summary
| Feature | .NET Core |
|---|---|
| Built-in logging | Yes |
| Multiple providers | Yes |
| Log levels | Yes |
| External tools | Yes |
| Monitoring support | Yes |
