0Pricing
C# Academy · Lesson

Logging abstractions, Debug/Trace

Use System.Diagnostics Debug/Trace, add listeners, and create a tiny ILogger-style abstraction to decouple app code from sinks.

Logging abstractions, Debug/Trace is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why logging

Aim:

  • Send messages with Debug and Trace
  • Add listeners to see output
  • Introduce a tiny logging abstraction
  • Pick levels and simple formatting

Debug/Trace basics

Debug is for development; Trace is for any build. Add a ConsoleTraceListener to see messages in console apps.

using System;
using System.Diagnostics;

// Demo: show Debug/Trace after adding a ConsoleTraceListener
public class Program
{
  public static void Main(string[] args)
  {
    // By default, console apps may not show Debug/Trace. Add a listener.
    Trace.Listeners.Clear();
    Trace.Listeners.Add(new ConsoleTraceListener());

    Debug.WriteLine("Debug: only in debug builds (often)");
    Trace.WriteLine("Trace: in all builds (commonly)");

    Trace.TraceInformation("Info message");
    Trace.TraceWarning("Warning message");
    Trace.TraceError("Error message");
  }
}

TraceSource demo

TraceSource names a logger and filters by SourceLevels; useful for grouping messages.

using System;
using System.Diagnostics;

// Demo: TraceSource can group logs by name and level.
public class Program
{
  public static void Main(string[] args)
  {
    var src = new TraceSource("Calc", SourceLevels.Information);
    src.Listeners.Clear();
    src.Listeners.Add(new ConsoleTraceListener());

    src.TraceEvent(TraceEventType.Information, 1001, "Starting");
    src.TraceEvent(TraceEventType.Warning, 1002, "Low precision");
    src.TraceEvent(TraceEventType.Error, 1003, "Divide by zero prevented");
    src.Flush();
    src.Close();
  }
}

Small ILogger demo

An interface decouples callers from the sink; later you can add a file or remote sink without changing call sites.

using System;

// Minimal ILogger-like abstraction for beginners.
public interface ILogger
{
  void Info(string msg);
  void Warn(string msg);
  void Error(string msg);
}

public class ConsoleLogger : ILogger
{
  private readonly string _name;
  public ConsoleLogger(string name) { _name = name; }

  public void Info(string msg)  { Console.WriteLine("[INFO] "  + _name + " - " + msg); }
  public void Warn(string msg)  { Console.WriteLine("[WARN] "  + _name + " - " + msg); }
  public void Error(string msg) { Console.WriteLine("[ERROR] " + _name + " - " + msg); }
}

public class Program
{
  public static void Main(string[] args)
  {
    ILogger log = new ConsoleLogger("Checkout");
    log.Info("Start");
    log.Warn("Slow network");
    log.Error("Payment failed");
  }
}

Context & timestamp

Add tiny context (key=value) and a UTC timestamp for easier correlation when reading logs.

using System;

// Add a timestamp and tiny key=value context.
public interface ILogger
{
  void Info(string msg, string contextKey, string contextValue);
}

public class ConsoleLogger : ILogger
{
  private readonly string _name;
  public ConsoleLogger(string name) { _name = name; }

  public void Info(string msg, string key, string value)
  {
    string stamp = DateTime.UtcNow.ToString("u"); // 2009-06-15 13:45:30Z
    Console.WriteLine(stamp + " [INFO] " + _name + " " + key + "=" + value + " :: " + msg);
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    ILogger log = new ConsoleLogger("Search");
    log.Info("Query executed", "q", "apple");
    log.Info("Cache miss", "userId", "42");
  }
}

Tips & hygiene

Tips:

  • Choose a few levels (Info/Warn/Error)
  • Add at least one listener for Debug/Trace
  • Keep messages short; add small context pairs
  • Do not log secrets

Why a logging abstraction

Quick check: What is a main benefit of using a logging abstraction (e.g., an ILogger interface)?

Recap

Recap: Use Debug/Trace with listeners to see output, and wrap calls behind a tiny logger interface so sinks can change without touching business code.

Frequently asked questions

Is the “Logging abstractions, Debug/Trace” lesson free?

Yes — the full text of “Logging abstractions, Debug/Trace” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Logging abstractions, Debug/Trace”?

Use System.Diagnostics Debug/Trace, add listeners, and create a tiny ILogger-style abstraction to decouple app code from sinks. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Logging abstractions, Debug/Trace” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Logging abstractions, Debug/Trace
  2. Basic profiling & traces (concepts)
  3. Guard & validation patterns
← Back to C# Academy