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,440
SanedBuildingBlocks
This package built for OIG-Saned
2,437
SanedBuildingBlocks
This package built for OIG-Saned
915
SanedBuildingBlocks
This package built for OIG-Saned
837
SanedBuildingBlocks
This package built for OIG-Saned
762
SanedBuildingBlocks
This package built for OIG-Saned
650
SanedBuildingBlocks
This package built for OIG-Saned
458
SanedBuildingBlocks
This package built for OIG-Saned
426
SanedBuildingBlocks
This package built for OIG-Saned
280
SanedBuildingBlocks
This package built for OIG-Saned
272
SanedBuildingBlocks
This package built for OIG-Saned
234
SanedBuildingBlocks
This package built for OIG-Saned
231
SanedBuildingBlocks
This package built for OIG-Saned
195
SanedBuildingBlocks
This package built for OIG-Saned
189
SanedBuildingBlocks
This package built for OIG-Saned
187
SanedBuildingBlocks
This package built for OIG-Saned
185
SanedBuildingBlocks
This package built for OIG-Saned
130
SanedBuildingBlocks
This package built for OIG-Saned
124
SanedBuildingBlocks
This package built for OIG-Saned
120
SanedBuildingBlocks
This package built for OIG-Saned
118

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

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