📝 Console.WriteLine is Not for Production
ILogger is built-in. Structured logging, log levels, multiple providers. No more rolling your own logging.
📝 Basic Logging
public class MyService
{
private readonly ILogger _logger;
public MyService(ILogger logger)
{
_logger = logger;
}
public void Process(int id)
{
_logger.LogInformation("Processing order {OrderId}", id);
try
{
// do work
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to process order {OrderId}", id);
throw;
}
}
}
🎯 Log Levels
// Trace: Most detailed (debugging only)
_logger.LogTrace("Entering method");
// Debug: Development debugging
_logger.LogDebug("Variable value: {Value}", value);
// Information: Normal application flow
_logger.LogInformation("User {UserId} logged in", userId);
// Warning: Unexpected but not fatal
_logger.LogWarning("API response slow: {Duration}ms", duration);
// Error: Exception or failure
_logger.LogError(ex, "Database connection failed");
// Critical: Application crash
_logger.LogCritical("Application shutting down unexpectedly");
💡 Providers
- Console (default)
- Debug (Output window)
- EventLog (Windows)
- Application Insights (Azure)
- Seq, Elasticsearch, Splunk (via Serilog)
“Used to write logs to text files manually. ILogger changed everything. Structured logging, log levels, easy filtering. Production debugging is now possible.”
