Tolitech.Logging.Extensions
1.0.0-preview.3
dotnet add package Tolitech.Logging.Extensions --version 1.0.0-preview.3
NuGet\Install-Package Tolitech.Logging.Extensions -Version 1.0.0-preview.3
<PackageReference Include="Tolitech.Logging.Extensions" Version="1.0.0-preview.3" />
<PackageVersion Include="Tolitech.Logging.Extensions" Version="1.0.0-preview.3" />
<PackageReference Include="Tolitech.Logging.Extensions" />
paket add Tolitech.Logging.Extensions --version 1.0.0-preview.3
#r "nuget: Tolitech.Logging.Extensions, 1.0.0-preview.3"
#:package Tolitech.Logging.Extensions@1.0.0-preview.3
#addin nuget:?package=Tolitech.Logging.Extensions&version=1.0.0-preview.3&prerelease
#tool nuget:?package=Tolitech.Logging.Extensions&version=1.0.0-preview.3&prerelease
Tolitech.Logging.Extensions
Tolitech.Logging.Extensions enhances logging in .NET applications using Microsoft.Extensions.Logging, providing standardized, high-performance, and maintainable logging practices.
Overview
This library centralizes logger message definitions using Microsoft's LoggerMessage
attribute, enabling source-generated logging methods for optimal performance and consistency.
Features
- Predefined Logger Messages: Centralized, strongly-typed logging methods for common scenarios.
- Source Generation: Uses
LoggerMessage
for compile-time generation, reducing runtime overhead. - Consistency: Ensures uniform log messages across applications.
- Extensibility: Easily add new log definitions for your domain.
Usage
1. Add NuGet Reference
Add Tolitech.Logging.Extensions to your project:
# dotnet CLI
dotnet add package Tolitech.Logging.Extensions
2. Use Predefined Logger Messages
using Microsoft.Extensions.Logging;
using Tolitech.Logging.Extensions;
public class MyService
{
private readonly ILogger<MyService> _logger;
public MyService(ILogger<MyService> logger)
{
_logger = logger;
}
public void DoWork()
{
try
{
// ...
}
catch (Exception ex)
{
_logger.UnexpectedError(ex);
}
}
}
3. Available Logger Methods
_logger.UnexpectedError(exception); // Logs unexpected errors
_logger.DatabaseConstraintError(exception); // Logs database constraint violations
4. Custom Logger Messages
You can extend the LoggerMessageDefinitions
class with your own methods:
public static partial class LoggerMessageDefinitions
{
[LoggerMessage(
EventName = "CustomDomainError",
Level = LogLevel.Warning,
Message = "A domain-specific warning occurred.",
SkipEnabledCheck = true)]
public static partial void CustomDomainError(this ILogger logger, Exception exception);
}
5. Logging Configuration
Ensure your logging provider is configured in appsettings.json
or programmatically:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Tolitech.Logging.Extensions": "Error"
}
}
}
FAQ
Q: What are the benefits of source-generated logging? A: It reduces allocations and improves performance by generating efficient code at compile time.
Q: Can I use this library with other logging providers? A: Yes, it works with any provider compatible with Microsoft.Extensions.Logging.
Q: How do I add new log message definitions?
A: Add new partial methods to the LoggerMessageDefinitions
class using the LoggerMessage
attribute.
Advanced Scenarios
- AOT & Trimming: Fully compatible with .NET Native AOT and trimming.
- Unit Testing: Use mocking frameworks (e.g., Moq) to verify logging calls.
// Example unit test
var mockLogger = new Mock<ILogger<MyService>>();
mockLogger.Object.UnexpectedError(new Exception("Test"));
mockLogger.Verify(
logger => logger.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.IsAny<object>(),
It.IsAny<Exception>(),
It.IsAny<Func<object, Exception, string>>()),
Times.Once);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Microsoft.Extensions.Logging (>= 9.0.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0-preview.3 | 397 | 7/21/2025 |