π§ͺ Microsoft.Extensions.Logging integration for TUnit testing framework
A modern NuGet package that provides an ILogger implementation seamlessly integrated with TUnit's TestContext.Current.OutputWriter. Perfect for capturing structured logging output directly in your test results.
- π― Native TUnit Integration - Works seamlessly with TUnit's test context
- π Structured Logging - Full support for Microsoft.Extensions.Logging features
- π Scope Support - Hierarchical logging scopes for better test organization
- β‘ Multiple Target Frameworks - Supports .NET 8.0, .NET 9.0, and .NET Standard 2.0
- ποΈ Configurable - Flexible configuration options for different testing scenarios
- π Zero Dependencies - Minimal footprint with only essential dependencies
Install-Package Dameng.Logging.TUnitdotnet add package Dameng.Logging.TUnitOr visit the NuGet package page for more installation options.
Get started with Dameng.Logging.TUnit in seconds! Use TestContext.Current.GetLogger() to get an ILogger instance that writes directly to your TUnit test output.
using Dameng.Logging.TUnit;
using Microsoft.Extensions.Logging;
public class Tests
{
    [Test]
    public void Basic()
    {
        var logger = TestContext.Current!.GetLogger("Basic");
        logger.LogCritical("This is a critical log message.");
    }
}Output:
this will output:
```text
2025-06-29 15:20:13.198 crit: Basic [0]: This is a critical log message.
var logger = TestContext.Current!.GetLogger(
    "Scope",
    includeScope: true
);
using (logger.BeginScope("Scope 1"))
{
    logger.LogInformation("This is scope 1 message.");
    using (logger.BeginScope("Scope 2"))
    {
        logger.LogInformation("This is scope 2 message");
    }
    logger.LogInformation("This is scope 1 message after scope 2.");
}Output:
2025-06-29 15:26:54.394 info Scope: This is scope 1 message.
 => Scope 1 
2025-06-29 15:26:54.398 info Scope: This is scope 2 message
 => Scope 1  => Scope 2 
2025-06-29 15:26:54.399 info Scope: This is scope 1 message after scope 2.
 => Scope 1
var logger = TestContext.Current!.GetLogger(
    "Exception",
    includeScope: true
);
using (logger.BeginScope("Scope for exception"))
{
    try
    {
        throw new InvalidOperationException("This is a test exception with scope.");
    }
    catch (Exception ex)
    {
        logger.LogError(ex, "An error occurred while processing the request with scope.");
    }
}Output:
2025-06-29 15:26:54.394 fail Exception: An error occurred while processing the request with scope.
 => Scope for exception 
System.InvalidOperationException: This is a test exception with scope.
   at Dameng.Logging.TUnit.Tests.Tests.ExceptionAndScope() in C:\...\Tests.cs:line 68
For more complex scenarios, use the GetLoggerFactory() extension method to get a pre-configured ILoggerFactory with TUnit logging:
using Dameng.Logging.TUnit;
using Microsoft.Extensions.Logging;
public class Tests
{
    [Test]
    public void LoggerFactoryBasic()
    {
        var loggerFactory = TestContext.Current!.GetLoggerFactory();
        var logger = loggerFactory.CreateLogger("MyCategory");
        logger.LogInformation("This is logged via LoggerFactory.");
    }
}Configure additional logging providers and filters alongside TUnit:
[Test]
public void LoggerFactoryWithCustomConfiguration()
{
    var loggerFactory = TestContext.Current!.GetLoggerFactory(
        includeScope: true,
        dateTimeFormat: "HH:mm:ss",
        builderAction: builder =>
        {
            // Add additional logging providers or configuration
            builder.SetMinimumLevel(LogLevel.Debug);
            builder.AddFilter("MyCategory", LogLevel.Warning);
        }
    );
    
    var logger = loggerFactory.CreateLogger("MyCategory");
    logger.LogInformation("This message will be filtered out due to the filter above.");
    logger.LogWarning("This warning will be displayed.");
}Alternative approach using ILoggerBuilder for additional configuration options:
var logger = LoggerFactory.Create(logging =>
    logging.AddTUnit(TestContext.Current!)
).CreateLogger("Basic");Control the minimum log level when creating loggers:
var logger = TestContext.Current!.GetLogger("Basic", minLevel: LogLevel.Trace);You can control the datetime format and whether to include datetime in the log output:
// Custom datetime format
var logger = TestContext.Current!.GetLogger(
    "CustomDateTime",
    dateTimeFormat: "HH:mm:ss"
);
logger.LogInformation("This message uses custom datetime format.");Output:
15:30:45 info: CustomDateTime [0]: This message uses custom datetime format.
// Disable datetime display
var logger = TestContext.Current!.GetLogger(
    "NoDateTime",
    includeDateTime: false
);
logger.LogInformation("This message has no datetime.");Output:
info: NoDateTime [0]: This message has no datetime.
You can also configure datetime settings when using GetLoggerFactory():
var loggerFactory = TestContext.Current!.GetLoggerFactory(
    dateTimeFormat: "yyyy-MM-dd HH:mm:ss",
    includeDateTime: true
);
var logger = loggerFactory.CreateLogger("MyCategory");
logger.LogInformation("Custom datetime format via factory.");Or when using AddTUnit with ILoggingBuilder:
var logger = LoggerFactory.Create(logging =>
    logging.AddTUnit(
        TestContext.Current!,
        dateTimeFormat: "HH:mm:ss.fff",
        includeDateTime: false
    )
).CreateLogger("NoDateTimeLogger");| Parameter | Type | Default | Description | 
|---|---|---|---|
| categoryName | string | Required | The category name for the logger | 
| includeScope | bool | false | Whether to include scope information in log output | 
| dateTimeFormat | string | "yyyy-MM-dd HH:mm:ss.fff" | Custom date/time format | 
| minLevel | LogLevel | LogLevel.Information | Minimum log level to output | 
| builderAction | Action<ILoggingBuilder> | null | Additional configuration for the logging builder | 
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- .NET 8.0, .NET 9.0, or .NET Standard 2.0
- TUnit testing framework
- Microsoft.Extensions.Logging
This project is licensed under the MIT License. See the LICENSE file for details.
- TUnit - The modern testing framework this package integrates with
- Microsoft.Extensions.Logging - The logging abstraction this package implements
Made with β€οΈ by dameng324