Microsoft.Extensions.Logging.Abstractions 9.0.0-preview.3.24172.9

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
DotNetCore.CAP
Distributed transaction solution in micro-service base on eventually consistency, also an eventbus with Outbox pattern.
924
Blazorise
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
606
Blazorise
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
559
Blazorise
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
548
Blazorise.Snackbar
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
451
Blazorise.DataGrid
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
440
Blazorise.Snackbar
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
437
Blazorise.DataGrid
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
428
Blazorise.DataGrid
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
423
Blazorise.DataGrid
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
419
Blazorise.Snackbar
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
418
Blazorise.DataGrid
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
412
DotNetCore.CAP
Eventually consistency in distributed architectures.
243
Blazorise.Snackbar
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
235
Magicodes.Wx.Sdk.Core
最简洁最易于使用的微信Sdk,包括公众号Sdk、小程序Sdk、企业微信Sdk等,以及Abp VNext集成。 开源库地址:https://github.com/xin-lai 博客地址:http://www.cnblogs.com/codelove/ 公众号:麦扣聊技术 交流QQ群:85318032
18
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/8486d31e24f30e3fa1809a95699a0adc16f448d7
18
TimServerSDK
腾讯云 即时通信 IM 服务端API
16
Microsoft.Extensions.Logging
Logging infrastructure default implementation for Microsoft.Extensions.Logging.
14
Microsoft.Extensions.Hosting.Abstractions
Hosting and startup abstractions for applications.
13

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

.NET Framework 4.6.2

.NET 8.0

.NET 9.0

.NET Standard 2.0

Version Downloads Last updated
9.0.0-preview.3.24172.9 12 2024/5/10
8.0.0 13 2024/2/29
7.0.1 8 2023/12/25
7.0.0 10 2023/7/15
6.0.3 7 2023/2/10
6.0.1 10 2022/7/18
6.0.0 12 2022/7/18
5.0.0 14 2021/9/28
3.1.8 12 2021/10/10
3.1.6 7 2023/9/8
3.1.5 8 2022/9/12
3.1.2 10 2021/10/7
3.1.0 10 2022/9/12
3.0.1 8 2022/9/12
3.0.0 11 2023/8/11
2.2.0 11 2023/2/10
2.1.0 11 2021/9/28
2.0.1 8 2021/10/7
2.0.0 15 2021/9/28