DotNetToolbox.TestUtilities 8.0.4

dotnet add package DotNetToolbox.TestUtilities --version 8.0.4
NuGet\Install-Package DotNetToolbox.TestUtilities -Version 8.0.4
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="DotNetToolbox.TestUtilities" Version="8.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotNetToolbox.TestUtilities --version 8.0.4
#r "nuget: DotNetToolbox.TestUtilities, 8.0.4"
#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.
// Install DotNetToolbox.TestUtilities as a Cake Addin
#addin nuget:?package=DotNetToolbox.TestUtilities&version=8.0.4

// Install DotNetToolbox.TestUtilities as a Cake Tool
#tool nuget:?package=DotNetToolbox.TestUtilities&version=8.0.4

TestUtilities (DotNetToolbox.TestUtilities)

Introduction

DotNetToolbox.TestUtilities is a comprehensive C# library for .NET 8, designed to enhance logging and tracking capabilities in testing environments. It provides robust tools for inspecting, verifying, and validating log outputs. The library includes classes that can be used as mocked instances in test cases where a test subject requires ILogger, ILoggerFactory, and ILoggerProvider to be injected. This makes it particularly useful for unit testing scenarios where you need to verify logging behavior or when testing components that interact with logging services.

Table of Contents

  1. Installation
  2. Dependencies
  3. Features
  4. Usage
  5. Examples
  6. Contributors
  7. License

Installation

PM> Install-Package DotNetToolbox.TestUtilities

Dependencies

  • .NET 8

Features

  • ITrackedLogger: Interface for loggers that track and store log entries.
  • Log: Data structure for representing individual log entries.
  • TrackedLogger: Extends basic logging to include tracking of log messages.
  • TrackedNullLogger: An in-memory only logger for capturing and inspecting logs during tests.
  • TrackedLoggerFactory: Factory for creating instances of TrackedLogger.
  • TrackedNullLoggerFactory: Factory for creating instances of TrackedNullLogger.
  • TrackedLoggerProvider: Provides TrackedLogger instances, integrating with existing logging systems.

Usage

To use DotNetToolbox.TestUtilities, include it in your test project and instantiate the desired logging components, such as TrackedNullLogger or TrackedLogger, as per your testing requirements.

Examples

Here are some examples demonstrating the usage of the library:

  1. Implementing TrackedLogger:

    var trackedLogger = new TrackedLogger<MyClass>(logger);
    trackedLogger.Log(LogLevel.Warning, new EventId(2, "WarningEvent"), "Warning message", null, Formatter);
    
  2. Creating Log Entries:

    var logEntry = new Log(LogLevel.Error, new EventId(3, "ErrorEvent"), "Error message", new Exception("TestException"), "Formatted message");
    
  3. Using TrackedNullLogger:

    var logger = TrackedNullLogger<MyClass>.Instance;
    logger.Log(LogLevel.Information, new EventId(1, "TestEvent"), "Test message", null, Formatter);
    
  4. Mocking ILoggerFactory for a Class Constructor:

    public class MyClass {
        private readonly ILogger _logger;
    
        public MyClass(ILoggerFactory loggerFactory) {
            _logger = loggerFactory.CreateLogger<MyClass>();
        }
    
        // Class methods
    }
    
    // In your test
    var loggerFactory = new TrackedLoggerFactory(Substitute.For<ILoggerFactory>());
    var myClassInstance = new MyClass(loggerFactory);
    
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DotNetToolbox.TestUtilities:

Package Downloads
DotNetToolbox.FluentAssertions

An extension of FluentAssertions that provides specialized assertion capabilities for logging components.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.4 152 2/5/2024
8.0.3 121 1/11/2024
8.0.2-rc2 66 1/8/2024
8.0.2-rc1 79 1/6/2024

v8.0.4:

     Stable release of the DotNetToolbox.TestUtilities library, a comprehensive C# library for .NET 8, designed to enhance logging and tracking capabilities in testing environments.

     Key Features:
     - ITrackedLogger: Interface for loggers that track and store log entries.
     - Log: Data structure for representing individual log entries.
     - TrackedLogger: Extends basic logging to include tracking of log messages.
     - TrackedNullLogger: An in-memory only logger for capturing and inspecting logs during tests.
     - TrackedLoggerProvider: Provides `TrackedLogger` instances, integrating with existing logging systems.
     - TrackedNullLoggerFactory: Factory for creating instances of `TrackedNullLogger`.
     - TrackedLoggerFactory: Factory for creating instances of `TrackedLogger`.