Tolitech.Logging.Extensions 1.0.0-preview.3

This is a prerelease version of Tolitech.Logging.Extensions.
dotnet add package Tolitech.Logging.Extensions --version 1.0.0-preview.3
                    
NuGet\Install-Package Tolitech.Logging.Extensions -Version 1.0.0-preview.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tolitech.Logging.Extensions" Version="1.0.0-preview.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tolitech.Logging.Extensions" Version="1.0.0-preview.3" />
                    
Directory.Packages.props
<PackageReference Include="Tolitech.Logging.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Tolitech.Logging.Extensions --version 1.0.0-preview.3
                    
#r "nuget: Tolitech.Logging.Extensions, 1.0.0-preview.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Tolitech.Logging.Extensions@1.0.0-preview.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Tolitech.Logging.Extensions&version=1.0.0-preview.3&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Tolitech.Logging.Extensions&version=1.0.0-preview.3&prerelease
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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