Microsoft.Extensions.Logging.Abstractions 9.0.2

About

Microsoft.Extensions.Logging.Abstractions provides abstractions of logging. Interfaces defined in this package are implemented by classes in Microsoft.Extensions.Logging and other logging packages.

This package includes a logging source generator that produces highly efficient and optimized code for logging message methods.

Key Features

  • Define main logging abstraction interfaces like ILogger, ILoggerFactory, ILoggerProvider, etc.

How to Use

Custom logger provider implementation example

using Microsoft.Extensions.Logging;

public sealed class ColorConsoleLogger : ILogger
{
    private readonly string _name;
    private readonly Func<ColorConsoleLoggerConfiguration> _getCurrentConfig;

    public ColorConsoleLogger(
        string name,
        Func<ColorConsoleLoggerConfiguration> getCurrentConfig) =>
        (_name, _getCurrentConfig) = (name, getCurrentConfig);

    public IDisposable? BeginScope<TState>(TState state) where TState : notnull => default!;

    public bool IsEnabled(LogLevel logLevel) =>
        _getCurrentConfig().LogLevelToColorMap.ContainsKey(logLevel);

    public void Log<TState>(
        LogLevel logLevel,
        EventId eventId,
        TState state,
        Exception? exception,
        Func<TState, Exception?, string> formatter)
    {
        if (!IsEnabled(logLevel))
        {
            return;
        }

        ColorConsoleLoggerConfiguration config = _getCurrentConfig();
        if (config.EventId == 0 || config.EventId == eventId.Id)
        {
            ConsoleColor originalColor = Console.ForegroundColor;

            Console.ForegroundColor = config.LogLevelToColorMap[logLevel];
            Console.WriteLine($"[{eventId.Id,2}: {logLevel,-12}]");

            Console.ForegroundColor = originalColor;
            Console.Write($"     {_name} - ");

            Console.ForegroundColor = config.LogLevelToColorMap[logLevel];
            Console.Write($"{formatter(state, exception)}");

            Console.ForegroundColor = originalColor;
            Console.WriteLine();
        }
    }
}

Create logs


// Worker class that uses logger implementation of teh interface ILogger<T>

public sealed class Worker : BackgroundService
{
    private readonly ILogger<Worker> _logger;

    public Worker(ILogger<Worker> logger) =>
        _logger = logger;

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            _logger.LogInformation("Worker running at: {time}", DateTimeOffset.UtcNow);
            await Task.Delay(1_000, stoppingToken);
        }
    }
}

Use source generator

public static partial class Log
{
    [LoggerMessage(
        EventId = 0,
        Level = LogLevel.Critical,
        Message = "Could not open socket to `{hostName}`")]
    public static partial void CouldNotOpenSocket(this ILogger logger, string hostName);
}

public partial class InstanceLoggingExample
{
    private readonly ILogger _logger;

    public InstanceLoggingExample(ILogger logger)
    {
        _logger = logger;
    }

    [LoggerMessage(
        EventId = 0,
        Level = LogLevel.Critical,
        Message = "Could not open socket to `{hostName}`")]
    public partial void CouldNotOpenSocket(string hostName);
}

Main Types

The main types provided by this library are:

  • Microsoft.Extensions.Logging.ILogger
  • Microsoft.Extensions.Logging.ILoggerProvider
  • Microsoft.Extensions.Logging.ILoggerFactory
  • Microsoft.Extensions.Logging.ILogger<TCategoryName>
  • Microsoft.Extensions.Logging.LogLevel
  • Microsoft.Extensions.Logging.Logger<T>
  • Microsoft.Extensions.Logging.LoggerMessage
  • Microsoft.Extensions.Logging.Abstractions.NullLogger

Additional Documentation

Microsoft.Extensions.Logging Microsoft.Extensions.Logging.Console Microsoft.Extensions.Logging.Debug Microsoft.Extensions.Logging.EventSource Microsoft.Extensions.Logging.EventLog Microsoft.Extensions.Logging.TraceSource

Feedback & Contributing

Microsoft.Extensions.Logging.Abstractions is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.

Showing the top 20 packages that depend on Microsoft.Extensions.Logging.Abstractions.

Packages Downloads
SanedBuildingBlocks
This package built for OIG-Saned
3,442
SanedBuildingBlocks
This package built for OIG-Saned
2,443
SanedBuildingBlocks
This package built for OIG-Saned
916
SanedBuildingBlocks
This package built for OIG-Saned
838
SanedBuildingBlocks
This package built for OIG-Saned
764
SanedBuildingBlocks
This package built for OIG-Saned
652
SanedBuildingBlocks
This package built for OIG-Saned
460
SanedBuildingBlocks
This package built for OIG-Saned
428
SanedBuildingBlocks
This package built for OIG-Saned
281
SanedBuildingBlocks
This package built for OIG-Saned
273
SanedBuildingBlocks
This package built for OIG-Saned
236
SanedBuildingBlocks
This package built for OIG-Saned
233
SanedBuildingBlocks
This package built for OIG-Saned
196
SanedBuildingBlocks
This package built for OIG-Saned
191
SanedBuildingBlocks
This package built for OIG-Saned
188
SanedBuildingBlocks
This package built for OIG-Saned
186
SanedBuildingBlocks
This package built for OIG-Saned
171
SanedBuildingBlocks
This package built for OIG-Saned
150
SanedBuildingBlocks
This package built for OIG-Saned
131
SanedBuildingBlocks
This package built for OIG-Saned
126

https://go.microsoft.com/fwlink/?LinkID=799421

Version Downloads Last updated
9.0.2 22 02/18/2025
9.0.1 17 02/18/2025
9.0.0 20 12/01/2024
8.0.3 18 02/18/2025
8.0.2 20 02/18/2025
8.0.1 17 08/02/2024
8.0.0 20 12/31/2023
7.0.1 17 06/13/2023
7.0.0 19 02/18/2025
6.0.4 18 02/18/2025
6.0.3 17 02/18/2025
6.0.2 16 02/18/2025
6.0.1 17 02/18/2025
6.0.0 22 06/15/2023
5.0.0 18 02/18/2025
3.1.32 16 02/18/2025
3.1.31 17 02/18/2025
3.1.30 19 02/18/2025
3.1.29 17 02/18/2025
3.1.28 17 02/18/2025
3.1.27 17 02/18/2025
3.1.26 17 02/18/2025
3.1.25 17 02/18/2025
3.1.24 18 02/18/2025
3.1.23 17 02/18/2025
3.1.22 18 02/18/2025
3.1.21 17 02/18/2025
3.1.20 16 02/18/2025
3.1.19 16 02/18/2025
3.1.18 16 02/18/2025
3.1.17 19 02/18/2025
3.1.16 16 02/18/2025
3.1.15 17 02/18/2025
3.1.14 20 02/18/2025
3.1.13 17 02/18/2025
3.1.12 16 02/18/2025
3.1.11 17 02/18/2025
3.1.10 16 02/18/2025
3.1.9 17 02/18/2025
3.1.8 20 06/26/2023
3.1.7 16 02/18/2025
3.1.6 16 02/18/2025
3.1.5 18 08/16/2023
3.1.4 19 02/18/2025
3.1.3 19 08/16/2023
3.1.2 17 02/18/2025
3.1.1 19 02/18/2025
3.1.0 17 10/02/2023
3.0.3 19 02/18/2025
3.0.2 18 02/18/2025
3.0.1 16 02/18/2025
3.0.0 20 08/16/2023
2.2.0 19 06/16/2023
2.1.1 18 02/18/2025
2.1.0 19 06/26/2023
2.1.0-rc1-final 19 02/18/2025
2.1.0-preview2-final 18 02/18/2025
2.1.0-preview1-final 18 02/18/2025
2.0.2 18 02/18/2025
2.0.1 17 02/18/2025
2.0.0 19 02/18/2025
2.0.0-preview2-final 18 02/18/2025
2.0.0-preview1-final 18 02/18/2025
1.1.2 17 02/18/2025
1.1.0 17 02/18/2025
1.1.0-preview1-final 18 02/18/2025
1.0.2 16 02/18/2025
1.0.1 16 02/18/2025
1.0.0 17 02/18/2025
1.0.0-rc2-final 16 02/18/2025
1.0.0-rc1-final 17 02/18/2025