Let’s go through debugging and logging in C# β€” essential tools for finding and fixing issues and monitoring application behavior.


πŸ“Œ 1. Debugging in C#

Debugging is the process of examining your code while it runs to identify and fix errors.

Common Techniques

  1. Breakpoints

    • Set in Visual Studio or VS Code to pause execution at a specific line.

    • Allows you to inspect variables, watch expressions, and step through code.

  2. Step Execution

    • Step Into (F11) β†’ enter a method call

    • Step Over (F10) β†’ execute the method without entering it

    • Step Out (Shift+F11) β†’ exit current method

  3. Watch & Immediate Window

    • Monitor variables and evaluate expressions during debugging.
  4. Conditional Breakpoints

    • Break only when a condition is true:

      // Example: break when i == 5
      if(i == 5) { /* breakpoint here */ }

πŸ“Œ 2. Logging in C#

Logging is recording information about your application’s execution to help debug, monitor, or audit behavior.

Using Console.WriteLine (Simple Logging)

int x = 5;
Console.WriteLine($"Value of x: {x}");
  • Quick and easy for small apps or testing, but not ideal for production

Using System.Diagnostics.Debug

using System.Diagnostics;
 
Debug.WriteLine("This is a debug message");
Debug.WriteLine($"Variable value: {x}");
  • Outputs messages to Output window in Visual Studio

  • Only visible in Debug builds, not Release


Using ILogger and Logging Frameworks

  • ASP.NET Core provides built-in logging via ILogger

  • Supports multiple targets: console, files, databases, etc.

using Microsoft.Extensions.Logging;
 
public class Program
{
    private readonly ILogger<Program> _logger;
 
    public Program(ILogger<Program> logger)
    {
        _logger = logger;
    }
 
    public void Run()
    {
        _logger.LogInformation("Application started");
        _logger.LogWarning("This is a warning");
        _logger.LogError("An error occurred");
    }
}

LibraryDescription
NLogFlexible, multiple targets
SerilogStructured logging, JSON output
log4netClassic logging framework
  • These libraries allow file logging, rolling logs, and structured logging

πŸ“Œ 3. Best Practices

  1. Use proper log levels: Debug, Info, Warning, Error, Critical

  2. Avoid excessive logging in performance-critical code

  3. Include context: timestamps, user ID, method names

  4. Separate Debug and Production logging

  5. Dispose or close loggers properly if writing to files


πŸ“Œ 4. Summary Table

FeatureDescription
BreakpointsPause execution to inspect code
Step ExecutionNavigate through code line by line
Debug.WriteLineQuick debug messages in development
Console.WriteLineSimple logging to console
ILogger / NLog / SerilogStructured, production-ready logging
Best PracticesProper log levels, context, avoid performance hits

βœ… Tip:

  • Combine debugging for immediate problem solving and logging for long-term monitoring.

  • Structured logging (with Serilog or NLog) is preferred for production apps.