Microsoft.Extensions.Logging.Abstractions 10.0.0-preview.4.25258.110
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
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 |
---|---|
Algolia.Search
Algolia is a powerful search-as-a-service solution, made easy to use with API clients, UI libraries, and pre-built integrations. Algolia API Client for the .NET framework lets you easily use the Algolia Search REST API from your .NET code.
|
5 |
System.ClientModel
Contains building blocks for clients that call cloud services.
|
5 |
Microsoft.CodeAnalysis.Workspaces.MSBuild
.NET Compiler Platform ("Roslyn") support for analyzing MSBuild projects and solutions. This should be used with at least one
of the following packages to add the appropriate language support:
- Microsoft.CodeAnalysis.CSharp.Workspaces
- Microsoft.CodeAnalysis.VisualBasic.Workspaces
More details at https://aka.ms/roslyn-packages
This package was built from the source at https://github.com/dotnet/roslyn/commit/8edf7bcd4f1594c3d68a6a567469f41dbd33dd1b.
|
5 |
Microsoft.Extensions.Hosting
Hosting and startup infrastructures for applications.
|
4 |
Microsoft.Extensions.Localization
Application localization services and default implementation based on ResourceManager to load localized assembly resources.
This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/718171eeb4bd236ab4f0d9b1ecd97ecc63ec0be1
|
4 |
bunit.xunit
bUnit.xunit is an xUnit extension for bUnit, that provides a way for xUnit to run bUnit's razor-based tests, as well as other helpful extensions to make the testing experience better if you use xUnit for writing your tests.
|
4 |
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
|
4 |
Microsoft.Extensions.Localization
Application localization services and default implementation based on ResourceManager to load localized assembly resources.
This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/c967158b960823ccfcb19f1ef56426e2fd38eb3f
|
4 |
Blazorise.Components
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
|
4 |
ServiceStack.Core
ServiceStack is a simple and fast alternative to WCF, MVC and Web API in one cohesive framework for all your services
and web apps that's intuitive and Easy to use!
To get started see: https://servicestack.net/getting-started
|
4 |
Blazorise.DataGrid
Blazorise is a component library built on top of Blazor and CSS frameworks like Bootstrap, Bulma and Material.
|
4 |
Microsoft.Extensions.Localization
Application localization services and default implementation based on ResourceManager to load localized assembly resources.
This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/d0ca5a8d20ac50a33d5451e998a5d411a810c8d7
|
4 |
Microsoft.Extensions.Logging
Logging infrastructure default implementation for Microsoft.Extensions.Logging.
|
4 |
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/dotnet/tree/c22dcd0c7a78d095a94d20e59ec0271b9924c82c
|
4 |
Algolia.Search
Algolia is a powerful search-as-a-service solution, made easy to use with API clients, UI libraries, and pre-built integrations. Algolia API Client for the .NET framework lets you easily use the Algolia Search REST API from your .NET code.
|
4 |
Microsoft.IdentityModel.Tokens
Includes types that provide support for SecurityTokens, Cryptographic operations: Signing, Verifying Signatures, Encryption.
|
4 |
.NET Framework 4.6.2
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0-preview.4.25258.110)
- System.Memory (>= 4.6.3)
- System.Buffers (>= 4.6.1)
- System.Diagnostics.DiagnosticSource (>= 10.0.0-preview.4.25258.110)
.NET Standard 2.0
- System.Memory (>= 4.6.3)
- System.Buffers (>= 4.6.1)
- System.Diagnostics.DiagnosticSource (>= 10.0.0-preview.4.25258.110)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0-preview.4.25258.110)
.NET 10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0-preview.4.25258.110)
.NET 9.0
- System.Diagnostics.DiagnosticSource (>= 10.0.0-preview.4.25258.110)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0-preview.4.25258.110)
.NET 8.0
- System.Diagnostics.DiagnosticSource (>= 10.0.0-preview.4.25258.110)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0-preview.4.25258.110)