Log levels
Learn LogDebug, LogInformation, LogWarning and LogError levels.
Log levels
Each log message carries a level. The four common levels in .NET are: LogDebug (verbose, for developers only), LogInformation (normal flow), LogWarning (noteworthy but not critical), LogError (failure).
The level filters the logger output. In production Debug is off, Warning and Error are on. This cuts noise and keeps important events visible. The choice of level depends on the severity of the message, not its volume.
Choosing a level
Picking the right level takes practice. Rule of thumb: a message the user must see is LogError; a recoverable but noteworthy situation is LogWarning; expected normal behavior is LogInformation; developer-only detail is LogDebug.
Common mistake: writing everything as LogInformation. This adds production noise. The opposite extreme, marking everything LogError, hides real failures. Choose the level by the event's severity.
Exercise Log at four levels
Define an ILogger<T> interface with LogDebug, LogInformation, LogWarning, and LogError methods (each taking a string message). ConsoleLogger<T> writes them to console with the prefixes [DBG], [INF], [WRN], and [ERR] respectively. In Main, create a ConsoleLogger<Program> and call the four methods with "Ayrıntılı bilgi", "Normal akış", "Dikkat " + "gerektiren durum", "Hata oluştu".
Show example solution
Define interface ILogger<T> { void LogDebug(string m); void LogInformation(string m); void LogWarning(string m); void LogError(string m); }. Write class ConsoleLogger<T> : ILogger<T> { public void LogDebug(string m) => Console.WriteLine("[DBG] " + m); public void LogInformation(string m) => Console.WriteLine("[INF] " + m); public void LogWarning(string m) => Console.WriteLine("[WRN] " + m); public void LogError(string m) => Console.WriteLine("[ERR] " + m); }. In Main: var l = new ConsoleLogger<Program>(); l.LogDebug("Ayrıntılı bilgi"); l.LogInformation("Normal akış"); l.LogWarning("Dikkat gerektiren durum"); l.LogError("Hata oluştu");
Example solution:
using System;
public interface ILogger<T>
{
void LogDebug(string message);
void LogInformation(string message);
void LogWarning(string message);
void LogError(string message);
}
public class ConsoleLogger<T> : ILogger<T>
{
public void LogDebug(string message) =>
Console.WriteLine("[DBG] " + message);
public void LogInformation(string message) =>
Console.WriteLine("[INF] " + message);
public void LogWarning(string message) =>
Console.WriteLine("[WRN] " + message);
public void LogError(string message) =>
Console.WriteLine("[ERR] " + message);
}
class Program
{
static void Main()
{
var logger = new ConsoleLogger<Program>();
logger.LogDebug("Ayrıntılı bilgi");
logger.LogInformation("Normal akış");
logger.LogWarning("Dikkat gerektiren durum");
logger.LogError("Hata oluştu");
}
}
Test yourself
-
Which level logs a normal-flow event?
- LogError
- LogDebug
- LogInformation
- LogWarning
Show answer
LogInformation — Correct. LogInformation logs normal-flow events.
-
Complete the error-level log call
logger.____("Dosya bulunamadı: " + path);Show answer
logger.LogError("Dosya bulunamadı: " + path); -
Match levels to their use
- LogDebug
- Developer detail
- LogInformation
- Normal flow
- LogWarning
- Noteworthy situation
- LogError
- Failure