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.
927
Blazorise
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
609
Blazorise
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
562
Blazorise
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
550
Blazorise.Snackbar
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
454
Blazorise.DataGrid
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
442
Blazorise.Snackbar
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.
439
Blazorise.DataGrid
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
431
Blazorise.DataGrid
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
426
Blazorise.DataGrid
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
422
Blazorise.Snackbar
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
420
Blazorise.DataGrid
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
415
DotNetCore.CAP
Eventually consistency in distributed architectures.
246
Blazorise.Snackbar
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
238
Magicodes.Wx.Sdk.Core
最简洁最易于使用的微信Sdk,包括公众号Sdk、小程序Sdk、企业微信Sdk等,以及Abp VNext集成。 开源库地址:https://github.com/xin-lai 博客地址:http://www.cnblogs.com/codelove/ 公众号:麦扣聊技术 交流QQ群:85318032
21
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
20
TimServerSDK
腾讯云 即时通信 IM 服务端API
19
Microsoft.Extensions.Logging
Logging infrastructure default implementation for Microsoft.Extensions.Logging.
17
StackExchange.Redis
High performance Redis client, incorporating both synchronous and asynchronous usage.
16

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 14 2024/5/10
8.0.0 16 2024/2/29
7.0.1 11 2023/12/25
7.0.0 13 2023/7/15
6.0.3 10 2023/2/10
6.0.1 13 2022/7/18
6.0.0 15 2022/7/18
5.0.0 17 2021/9/28
3.1.8 15 2021/10/10
3.1.6 10 2023/9/8
3.1.5 11 2022/9/12
3.1.2 13 2021/10/7
3.1.0 13 2022/9/12
3.0.1 11 2022/9/12
3.0.0 15 2023/8/11
2.2.0 14 2023/2/10
2.1.0 14 2021/9/28
2.0.1 11 2021/10/7
2.0.0 18 2021/9/28