Microsoft.Extensions.Logging.Abstractions 9.0.6

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
Dapper.Extensions.NetCore
A dapper extension library. Support MySQL,SQL Server,PostgreSQL,SQLite,Oracle and ODBC, Support cache.
10
Grpc.Net.Client
.NET client for gRPC
4
MassTransit
MassTransit provides a developer-focused, modern platform for creating distributed applications without complexity.
10
Microsoft.AspNetCore.Authorization
ASP.NET Core authorization classes. Commonly used types: Microsoft.AspNetCore.Authorization.AllowAnonymousAttribute Microsoft.AspNetCore.Authorization.AuthorizeAttribute This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/ed74665e773dd1ebea3289c5662d71c590305932
4
Microsoft.AspNetCore.OutputCaching.StackExchangeRedis
Output cache implementation of Microsoft.AspNetCore.OutputCaching.IOutputCacheStore using Redis. This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/ed74665e773dd1ebea3289c5662d71c590305932
4
Microsoft.Extensions.Caching.Memory
In-memory cache implementation of Microsoft.Extensions.Caching.Memory.IMemoryCache.
9
Microsoft.Extensions.Caching.Memory
In-memory cache implementation of Microsoft.Extensions.Caching.Memory.IMemoryCache. When using NuGet 3.x this package requires at least version 3.4.
10
Microsoft.Extensions.Diagnostics.HealthChecks
Components for performing health checks in .NET applications Commonly Used Types: Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckService Microsoft.Extensions.Diagnostics.HealthChecks.IHealthChecksBuilder This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/47576478939fdd59b4400ad135f47938af486ab3
10
Microsoft.Extensions.Diagnostics.HealthChecks
Components for performing health checks in .NET applications Commonly Used Types: Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckService Microsoft.Extensions.Diagnostics.HealthChecks.IHealthChecksBuilder This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/af22effae4069a5dfb9b0735859de48820104f5b
9
Microsoft.Extensions.Hosting.Abstractions
Hosting and startup abstractions for applications.
9
Microsoft.Extensions.Hosting.Abstractions
Hosting and startup abstractions for applications.
10
Microsoft.Extensions.Http
The HttpClient factory is a pattern for configuring and retrieving named HttpClients in a composable way. The HttpClient factory provides extensibility to plug in DelegatingHandlers that address cross-cutting concerns such as service location, load balancing, and reliability. The default HttpClient factory provides built-in diagnostics and logging and manages the lifetimes of connections in a performant way. Commonly Used Types: System.Net.Http.IHttpClientFactory
10
Microsoft.Extensions.Logging
Logging infrastructure default implementation for Microsoft.Extensions.Logging.
9
Microsoft.Extensions.Logging
Logging infrastructure default implementation for Microsoft.Extensions.Logging.
10
Microsoft.Extensions.Logging
Logging infrastructure default implementation for Microsoft.Extensions.Logging.
11
Serilog.Extensions.Hosting
Serilog support for .NET Core logging in hosted services
9

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

Version Downloads Last updated
10.0.0-preview.5.25277.114 1 6/8/2025
10.0.0-preview.4.25258.110 2 5/16/2025
10.0.0-preview.3.25171.5 2 4/19/2025
10.0.0-preview.2.25163.2 2 4/11/2025
10.0.0-preview.1.25080.5 2 2/28/2025
9.0.6 0 6/21/2025
9.0.5 11 5/16/2025
9.0.4 11 4/19/2025
9.0.3 1 4/18/2025
9.0.2 1 2/28/2025
9.0.1 1 1/18/2025
9.0.0 9 11/14/2024
9.0.0-rc.2.24473.5 1 11/14/2024
9.0.0-preview.6.24327.7 1 11/10/2024
9.0.0-preview.3.24172.9 1 11/10/2024
8.0.3 1 2/28/2025
8.0.2 9 11/14/2024
8.0.1 1 11/10/2024
8.0.0 9 3/17/2025
8.0.0-rc.1.23419.4 1 11/10/2024
8.0.0-preview.6.23329.7 1 11/10/2024
8.0.0-preview.4.23259.5 1 11/10/2024
7.0.1 1 3/17/2025
7.0.0-preview.6.22324.4 1 11/10/2024
7.0.0-preview.4.22229.4 1 11/10/2024
7.0.0-preview.2.22152.2 1 11/10/2024
6.0.2 1 11/10/2024
6.0.1 1 11/10/2024
6.0.0 10 3/17/2025
6.0.0-preview.2.21154.6 1 11/10/2024
5.0.0 5 11/10/2024
5.0.0-preview.8.20407.11 1 11/10/2024
5.0.0-preview.6.20305.6 1 11/10/2024
5.0.0-preview.1.20120.4 1 11/10/2024
3.1.32 1 11/10/2024
3.1.28 1 11/10/2024
3.1.24 1 11/12/2024
3.1.21 1 11/10/2024
3.1.18 1 11/10/2024
3.1.11 1 11/10/2024
3.1.7 1 11/10/2024
3.1.5 0 6/21/2025
3.1.2 1 11/10/2024
3.1.1 1 11/10/2024
3.0.0 1 11/10/2024
3.0.0-preview8.19405.4 1 11/10/2024
2.2.0 2 11/12/2024
2.2.0-preview2-35157 1 11/10/2024
2.2.0-preview1-35029 1 11/10/2024
2.1.0 1 11/10/2024
1.0.1 1 11/10/2024