What logging strategies would you use to debug issues in a REST
Short answer: PI? Structured logging with libraries like Serilog, NLog, or built-in ILogger. Log requests and responses, including headers and payloads (avoid sensitive info). Use correlation IDs to trace requests across services. Centralize logs using ELK Stack, Seq, or Azure Application Insights. Log different levels: Information, Warning, Error,… Critical. Example……… using ILogger in ASP.NET Core: private readonly…
Explain a bit more
ILogger<MyController> _logger; public MyController(ILogger<MyController> logger) { _logger = logger; } [HttpGet("{id}")] public IActionResult GetUser(int id) { _logger.LogInformation("Fetching user with id {UserId}", id); try { var user = dbContext.Users.Find(id); if (user == null) { _logger.LogWarning("User with id {UserId} not found", id); return NotFound(); } return Ok(user); } catch (Exception ex) { _logger.LogError(ex, "Error fetching user with id {UserId}", id); return StatusCode(500, "Internal Server Error"); } } This covers all core aspects of error handling and debugging for REST APIs. public MyController(ILogger<MyController> logger)
Example code
{
_logger = logger;
} [HttpGet("{id}")] public IActionResult GetUser(int id)
{ _logger.LogInformation("Fetching user with id {UserId}", id); try {
var user = dbContext.Users.Find(id);
if (user == null)
{ _logger.LogWarning("User with id {UserId} not found", id); return NotFound();
}
return Ok(user);
} catch (Exception ex) { _logger.LogError(ex, "Error fetching user with id {UserId}", id); return StatusCode(500, "Internal Server Error");
}
} This covers all core aspects of error handling and debugging for REST APIs.
Real-world example (ShopNest)
Creating an order is POST /api/orders → 201 with Location header. Fetching is GET /api/orders/{id} → 200 or 404.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png