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.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,423 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
2,419 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
912 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
833 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
753 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
647 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
456 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
422 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
276 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
269 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
221 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
191 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
186 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
184 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
157 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
125 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
121 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
118 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
115 |
|
SanedBuildingBlocks
This package built for OIG-Saned
|
110 |
.NET Framework 4.6.2
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.2)
- System.Diagnostics.DiagnosticSource (>= 9.0.2)
- System.Buffers (>= 4.5.1)
- System.Memory (>= 4.5.5)
.NET 8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.2)
- System.Diagnostics.DiagnosticSource (>= 9.0.2)
.NET 9.0
.NET Standard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.2)
- System.Diagnostics.DiagnosticSource (>= 9.0.2)
- System.Buffers (>= 4.5.1)
- System.Memory (>= 4.5.5)
| Version | Downloads | Last updated |
|---|---|---|
| 9.0.2 | 17 | 02/18/2025 |
| 9.0.1 | 13 | 02/18/2025 |
| 9.0.0 | 17 | 12/01/2024 |
| 8.0.3 | 15 | 02/18/2025 |
| 8.0.2 | 16 | 02/18/2025 |
| 8.0.1 | 14 | 08/02/2024 |
| 8.0.0 | 16 | 12/31/2023 |
| 7.0.1 | 14 | 06/13/2023 |
| 7.0.0 | 16 | 02/18/2025 |
| 6.0.4 | 14 | 02/18/2025 |
| 6.0.3 | 13 | 02/18/2025 |
| 6.0.2 | 13 | 02/18/2025 |
| 6.0.1 | 13 | 02/18/2025 |
| 6.0.0 | 18 | 06/15/2023 |
| 5.0.0 | 15 | 02/18/2025 |
| 3.1.32 | 13 | 02/18/2025 |
| 3.1.31 | 13 | 02/18/2025 |
| 3.1.30 | 15 | 02/18/2025 |
| 3.1.29 | 13 | 02/18/2025 |
| 3.1.28 | 14 | 02/18/2025 |
| 3.1.27 | 13 | 02/18/2025 |
| 3.1.26 | 13 | 02/18/2025 |
| 3.1.25 | 13 | 02/18/2025 |
| 3.1.24 | 14 | 02/18/2025 |
| 3.1.23 | 13 | 02/18/2025 |
| 3.1.22 | 15 | 02/18/2025 |
| 3.1.21 | 13 | 02/18/2025 |
| 3.1.20 | 12 | 02/18/2025 |
| 3.1.19 | 13 | 02/18/2025 |
| 3.1.18 | 13 | 02/18/2025 |
| 3.1.17 | 15 | 02/18/2025 |
| 3.1.16 | 13 | 02/18/2025 |
| 3.1.15 | 14 | 02/18/2025 |
| 3.1.14 | 15 | 02/18/2025 |
| 3.1.13 | 13 | 02/18/2025 |
| 3.1.12 | 13 | 02/18/2025 |
| 3.1.11 | 13 | 02/18/2025 |
| 3.1.10 | 13 | 02/18/2025 |
| 3.1.9 | 13 | 02/18/2025 |
| 3.1.8 | 16 | 06/26/2023 |
| 3.1.7 | 13 | 02/18/2025 |
| 3.1.6 | 12 | 02/18/2025 |
| 3.1.5 | 14 | 08/16/2023 |
| 3.1.4 | 15 | 02/18/2025 |
| 3.1.3 | 16 | 08/16/2023 |
| 3.1.2 | 13 | 02/18/2025 |
| 3.1.1 | 15 | 02/18/2025 |
| 3.1.0 | 14 | 10/02/2023 |
| 3.0.3 | 15 | 02/18/2025 |
| 3.0.2 | 14 | 02/18/2025 |
| 3.0.1 | 13 | 02/18/2025 |
| 3.0.0 | 16 | 08/16/2023 |
| 2.2.0 | 16 | 06/16/2023 |
| 2.1.1 | 14 | 02/18/2025 |
| 2.1.0 | 15 | 06/26/2023 |
| 2.1.0-rc1-final | 15 | 02/18/2025 |
| 2.1.0-preview2-final | 14 | 02/18/2025 |
| 2.1.0-preview1-final | 14 | 02/18/2025 |
| 2.0.2 | 15 | 02/18/2025 |
| 2.0.1 | 14 | 02/18/2025 |
| 2.0.0 | 14 | 02/18/2025 |
| 2.0.0-preview2-final | 14 | 02/18/2025 |
| 2.0.0-preview1-final | 15 | 02/18/2025 |
| 1.1.2 | 14 | 02/18/2025 |
| 1.1.0 | 13 | 02/18/2025 |
| 1.1.0-preview1-final | 14 | 02/18/2025 |
| 1.0.2 | 12 | 02/18/2025 |
| 1.0.1 | 13 | 02/18/2025 |
| 1.0.0 | 13 | 02/18/2025 |
| 1.0.0-rc2-final | 13 | 02/18/2025 |
| 1.0.0-rc1-final | 13 | 02/18/2025 |