Understanding Log Levels

Osama HaiDer
2 min readAug 6, 2024

--

Logging is an essential part of any application. It helps developers understand what’s happening within the application, especially when things go wrong. Logging is managed through different log levels. Each level serves a specific purpose, helping to categorize the severity and importance of log messages. Let’s break down these log levels with easy-to-understand examples.

Log Levels

  1. Trace
  • Description: Logs with the most detailed messages. These might contain sensitive data and are disabled by default in production.
  • Example:
logger.LogTrace("Starting method ExecuteOrder with parameters {orderId}, {userId}", orderId, userId);

2. Debug

  • Description: Used during development for debugging purposes. These logs help in the interactive investigation.
  • Example:
logger.LogDebug("Order details: {order}", order);

3. Information

  • Description: Tracks the general flow of the application. These logs have long-term value.
  • Example:
logger.LogInformation("User {userId} has logged in.", userId);

4. Warning

  • Description: Highlights abnormal or unexpected events in the application flow that don’t stop the execution.
  • Example:
logger.LogWarning("Disk space is running low.");

5. Error

  • Description: Indicates a failure in the current activity or request but not an application-wide failure.
  • Example:
logger.LogError("Error occurred while processing the order {orderId}", orderId);

6. Critical

  • Description: Describes an unrecoverable application or system crash, requiring immediate attention.
  • Example:
logger.LogCritical("Database connection failed. Immediate action required.");

7. None

  • Description: Specifies that a logging category should not write any messages. This is not used for writing log messages.
  • Example:
// No log message should be written for this category.

When to Use Each Log Level

  • Trace: Use for the most detailed logging, typically during development for tracing code execution paths.
  • Debug: Use detailed debugging information that can help developers understand the flow and state of the application during development.
  • Information: This is used to record the general operations of the application, such as user logins or application start/stop events.
  • Warning: Use for logging unusual situations that are not errors but could potentially cause issues, like deprecated API usage.
  • Error: This is used to log errors that occur during the execution of an operation, such as exceptions or failed operations.
  • Critical: Use for logging critical errors that require immediate attention, like a server outage or database connection failure.
  • None: Use to turn off logging for a specific category.

Conclusion

Understanding and using the appropriate log levels in your .NET applications can greatly enhance your ability to troubleshoot and maintain the application. Proper logging provides insights into the application’s behavior, helping to catch issues early and ensure smooth operation.

Here’s a simple example of setting up logging in a .NET application:

public class Program
{
public static void Main(string[] args)
{
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
builder.SetMinimumLevel(LogLevel.Debug);
});

var logger = loggerFactory.CreateLogger<Program>();

logger.LogTrace("This is a trace log.");
logger.LogDebug("This is a debug log.");
logger.LogInformation("This is an information log.");
logger.LogWarning("This is a warning log.");
logger.LogError("This is an error log.");
logger.LogCritical("This is a critical log.");
}
}

With this setup, you’ll see different log messages in the console, helping you understand the flow and state of your application.

Happy logging!

--

--

Osama HaiDer
Osama HaiDer

Written by Osama HaiDer

SSE at TEO International | .Net | Azure | AWS | Web APIs | C#