CodeBlanks Logo Icon CodeBlanks

Log providers

C# course · Advanced · Lesson 77/83

Learn Console and Debug log providers.

Log providers

A provider decides where logs go. Two common providers: ConsoleLoggerProvider writes to the console; DebugLoggerProvider writes to Visual Studio's Output window. Multiple providers can be attached to the same logger.

Providers are configured through a factory: loggerFactory.AddConsole().AddDebug(). So a single ILogger<T> call lands in both the console and the debugger. In production Debug is turned off; only Console or a file remains.

Multiple providers

A logger can write to several providers at once. For example Console and Debug both receive the same message; the user sees it on the console, the developer in the Output window. This is one call, many targets.

In tests this multi-target shape is easy to verify with a fake provider: a provider that collects logs in memory; then you check its contents with Assert.Equal. This is more reliable than capturing real console output.

Exercise Implement Console and Debug providers

Define an ILogger<T> interface with void LogInformation(string message). ConsoleLogger<T> calls Console.WriteLine("[INF] " + message). DebugLogger<T> calls System.Diagnostics.Debug.WriteLine. In Main, create one of each logger and call LogInformation; send "Konsola yazıldı" to Console and "Hata ayıklayıcıya yazıldı" to Debug.

Show example solution
Define interface ILogger<T> { void LogInformation(string message); }. Write class ConsoleLogger<T> : ILogger<T> { public void LogInformation(string message) => Console.WriteLine("[INF] " + message); }. Write class DebugLogger<T> : ILogger<T> { public void LogInformation(string message) => Debug.WriteLine("[INF] " + message); }. In Main: var c = new ConsoleLogger<Program>(); c.LogInformation("Konsola yazıldı"); var d = new DebugLogger<Program>(); d.LogInformation("Hata ayıklayıcıya yazıldı");
Example solution:
using System;
using System.Diagnostics;

public interface ILogger<T>
{
    void LogInformation(string message);
}

public class ConsoleLogger<T> : ILogger<T>
{
    public void LogInformation(string message) =>
        Console.WriteLine("[INF] " + message);
}

public class DebugLogger<T> : ILogger<T>
{
    public void LogInformation(string message) =>
        Debug.WriteLine("[INF] " + message);
}

class Program
{
    static void Main()
    {
        var console = new ConsoleLogger<Program>();
        console.LogInformation("Konsola yazıldı");
        var debug = new DebugLogger<Program>();
        debug.LogInformation("Hata ayıklayıcıya yazıldı");
    }
}

Test yourself

  1. Which provider writes to the Visual Studio Output window?

    • DebugLoggerProvider
    • ConsoleLoggerProvider
    • FileLoggerProvider
    • EventLogLoggerProvider
    Show answer

    DebugLoggerProvider — Correct. DebugLoggerProvider writes to the debugger via System.Diagnostics.Debug.

  2. Match providers to their target

    ConsoleLoggerProvider
    Console window
    DebugLoggerProvider
    Debugger output
    AddConsole
    Adds the console provider
    AddDebug
    Adds the debug provider
  3. Complete the provider-add call

    loggerFactory.Add____();
    Show answer
    loggerFactory.AddConsole();

All lessons in Advanced