⚙️ C# / .NET Intermediate

What is the ILogger interface in ASP.NET Core?

Answer

ILogger<T> is ASP.NET Core's built-in structured logging abstraction. Injected via DI: public class UserService(ILogger<UserService> logger) { }. Log levels (ascending severity): Trace, Debug, Information, Warning, Error, Critical. Log calls: logger.LogInformation("User {UserId} logged in at {Time}", userId, DateTime.UtcNow);. Structured logging: named placeholders {UserId} are captured as separate properties (not just string-formatted), enabling filtering and analysis in log aggregation systems. Configurable via appsettings.json: "Logging": { "LogLevel": { "Default": "Information" } }. Built-in providers: Console, Debug, EventSource. Third-party providers: Serilog, NLog, Application Insights — plug in via ILoggingBuilder. LoggerMessage: source-generated high-performance logging to avoid allocation on hot paths. Avoid string interpolation in log calls (logger.LogInformation($"User {id}")) — use structured logging placeholders.