Microsoft.Extensions.Logging.Abstractions 8.0.1
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.ILoggerMicrosoft.Extensions.Logging.ILoggerProviderMicrosoft.Extensions.Logging.ILoggerFactoryMicrosoft.Extensions.Logging.ILogger<TCategoryName>Microsoft.Extensions.Logging.LogLevelMicrosoft.Extensions.Logging.Logger<T>Microsoft.Extensions.Logging.LoggerMessageMicrosoft.Extensions.Logging.Abstractions.NullLogger
Additional Documentation
Related Packages
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,378 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
2,351 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
908 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
831 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
740 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
643 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
449 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
419 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
274 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
266 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
189 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
184 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
182 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
123 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
118 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
115 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
112 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
108 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
101 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
92 |
.NET Framework 4.6.2
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- System.Buffers (>= 4.5.1)
- System.Memory (>= 4.5.5)
.NET 6.0
.NET 7.0
.NET 8.0
.NET Standard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- System.Buffers (>= 4.5.1)
- System.Memory (>= 4.5.5)
| Version | Downloads | Last updated |
|---|---|---|
| 9.0.2 | 15 | 02/18/2025 |
| 9.0.1 | 11 | 02/18/2025 |
| 9.0.0 | 15 | 12/01/2024 |
| 8.0.3 | 13 | 02/18/2025 |
| 8.0.2 | 14 | 02/18/2025 |
| 8.0.1 | 12 | 08/02/2024 |
| 8.0.0 | 14 | 12/31/2023 |
| 7.0.1 | 12 | 06/13/2023 |
| 7.0.0 | 14 | 02/18/2025 |
| 6.0.4 | 12 | 02/18/2025 |
| 6.0.3 | 11 | 02/18/2025 |
| 6.0.2 | 11 | 02/18/2025 |
| 6.0.1 | 11 | 02/18/2025 |
| 6.0.0 | 16 | 06/15/2023 |
| 5.0.0 | 13 | 02/18/2025 |
| 3.1.32 | 11 | 02/18/2025 |
| 3.1.31 | 11 | 02/18/2025 |
| 3.1.30 | 13 | 02/18/2025 |
| 3.1.29 | 11 | 02/18/2025 |
| 3.1.28 | 12 | 02/18/2025 |
| 3.1.27 | 11 | 02/18/2025 |
| 3.1.26 | 11 | 02/18/2025 |
| 3.1.25 | 11 | 02/18/2025 |
| 3.1.24 | 12 | 02/18/2025 |
| 3.1.23 | 11 | 02/18/2025 |
| 3.1.22 | 13 | 02/18/2025 |
| 3.1.21 | 11 | 02/18/2025 |
| 3.1.20 | 10 | 02/18/2025 |
| 3.1.19 | 11 | 02/18/2025 |
| 3.1.18 | 11 | 02/18/2025 |
| 3.1.17 | 13 | 02/18/2025 |
| 3.1.16 | 11 | 02/18/2025 |
| 3.1.15 | 12 | 02/18/2025 |
| 3.1.14 | 13 | 02/18/2025 |
| 3.1.13 | 11 | 02/18/2025 |
| 3.1.12 | 11 | 02/18/2025 |
| 3.1.11 | 11 | 02/18/2025 |
| 3.1.10 | 11 | 02/18/2025 |
| 3.1.9 | 11 | 02/18/2025 |
| 3.1.8 | 14 | 06/26/2023 |
| 3.1.7 | 11 | 02/18/2025 |
| 3.1.6 | 10 | 02/18/2025 |
| 3.1.5 | 12 | 08/16/2023 |
| 3.1.4 | 13 | 02/18/2025 |
| 3.1.3 | 14 | 08/16/2023 |
| 3.1.2 | 11 | 02/18/2025 |
| 3.1.1 | 13 | 02/18/2025 |
| 3.1.0 | 12 | 10/02/2023 |
| 3.0.3 | 13 | 02/18/2025 |
| 3.0.2 | 12 | 02/18/2025 |
| 3.0.1 | 11 | 02/18/2025 |
| 3.0.0 | 14 | 08/16/2023 |
| 2.2.0 | 14 | 06/16/2023 |
| 2.1.1 | 12 | 02/18/2025 |
| 2.1.0 | 13 | 06/26/2023 |
| 2.1.0-rc1-final | 13 | 02/18/2025 |
| 2.1.0-preview2-final | 12 | 02/18/2025 |
| 2.1.0-preview1-final | 12 | 02/18/2025 |
| 2.0.2 | 13 | 02/18/2025 |
| 2.0.1 | 12 | 02/18/2025 |
| 2.0.0 | 12 | 02/18/2025 |
| 2.0.0-preview2-final | 12 | 02/18/2025 |
| 2.0.0-preview1-final | 13 | 02/18/2025 |
| 1.1.2 | 12 | 02/18/2025 |
| 1.1.0 | 11 | 02/18/2025 |
| 1.1.0-preview1-final | 12 | 02/18/2025 |
| 1.0.2 | 10 | 02/18/2025 |
| 1.0.1 | 11 | 02/18/2025 |
| 1.0.0 | 11 | 02/18/2025 |
| 1.0.0-rc2-final | 11 | 02/18/2025 |
| 1.0.0-rc1-final | 11 | 02/18/2025 |