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,447
SanedBuildingBlocks
This package built for OIG-Saned
2,467
SanedBuildingBlocks
This package built for OIG-Saned
920
SanedBuildingBlocks
This package built for OIG-Saned
843
SanedBuildingBlocks
This package built for OIG-Saned
770
SanedBuildingBlocks
This package built for OIG-Saned
655
SanedBuildingBlocks
This package built for OIG-Saned
464
SanedBuildingBlocks
This package built for OIG-Saned
431
SanedBuildingBlocks
This package built for OIG-Saned
359
SanedBuildingBlocks
This package built for OIG-Saned
285
SanedBuildingBlocks
This package built for OIG-Saned
276
SanedBuildingBlocks
This package built for OIG-Saned
240
SanedBuildingBlocks
This package built for OIG-Saned
237
SanedBuildingBlocks
This package built for OIG-Saned
233
SanedBuildingBlocks
This package built for OIG-Saned
202
SanedBuildingBlocks
This package built for OIG-Saned
199
SanedBuildingBlocks
This package built for OIG-Saned
195
SanedBuildingBlocks
This package built for OIG-Saned
193
SanedBuildingBlocks
This package built for OIG-Saned
192
SanedBuildingBlocks
This package built for OIG-Saned
136

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

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