ASP.NET Core Interview Question #151

What is logging in ASP.NET Core?

Logging, Error Handling & Background Services Mid-Level Intermediate

Quick Interview Answer

Logging is the framework-supported mechanism for recording diagnostic and operational information about an application. ASP.NET Core provides the ILogger abstraction so application code can write logs without being tightly coupled to a particular logging provider.

Detailed Explanation

Logging is the framework-supported mechanism for recording diagnostic and operational information about an application. ASP.NET Core provides the ILogger abstraction so application code can write logs without being tightly coupled to a particular logging provider.

Logs are commonly used to diagnose failures, understand request processing, monitor important application events, and support production troubleshooting. Logging should capture useful context without exposing passwords, tokens, personal data, or other sensitive information.

Code Example

public class OrderService
{
    private readonly ILogger<OrderService> _logger;

    public OrderService(ILogger<OrderService> logger)
    {
        _logger = logger;
    }

    public void Process(int orderId)
    {
        _logger.LogInformation(
            "Processing order {OrderId}", orderId);
    }
}