Logging is the process of recording application events so you can:
- Debug issues
- Monitor behavior
- Trace requests
- Audit actions in production
Without proper logging:
- Bugs are hard to reproduce
- Production failures are invisible
- Root cause analysis becomes guesswork
- Console
- Debug
- EventSource
- Application Insights (Azure)
How To Use ILogger?
public class OrderController : ControllerBase { private readonly ILogger<OrderController> _logger; public OrderController(ILogger<OrderController> logger) { _logger = logger; } }
_logger.LogInformation("Order creation started"); _logger.LogWarning("Order {OrderId} has no items", orderId); _logger.LogError(exception, "Failed to create order {OrderId}", orderId);
catch (Exception ex) { _logger.LogError(ex, "Error while processing order {OrderId}", orderId); throw; }
public async Task InvokeAsync(HttpContext context) { _logger.LogInformation( "Request {Method} {Path}", context.Request.Method, context.Request.Path); await _next(context); _logger.LogInformation( "Response {StatusCode}", context.Response.StatusCode); }
- Rich structured logging
- Multiple sinks (File, DB, Seq, Elastic)
- Better performance & flexibility
dotnet add package Serilog.AspNetCore dotnet add package Serilog.Sinks.Console dotnet add package Serilog.Sinks.File
using Serilog; Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .WriteTo.Console() .WriteTo.File("Logs/app-.log", rollingInterval: RollingInterval.Day) .CreateLogger(); builder.Host.UseSerilog();
_logger.LogInformation("Invoice {InvoiceId} generated", invoiceId);
ILoggerSerilog works behind the scenes.Interview Answer: In ASP.NET Core, I use ILogger for application logging and Serilog for structured, production-grade logging. I follow proper log levels, structured messages, correlation IDs, and centralized log storage to ensure observability and easy debugging.










