IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest 1.0.1

dotnet add package IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest --version 1.0.1
                    
NuGet\Install-Package IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest -Version 1.0.1
                    
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="IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest" />
                    
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 IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest --version 1.0.1
                    
#r "nuget: IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest, 1.0.1"
                    
#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 IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest@1.0.1
                    
#: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=IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest&version=1.0.1
                    
Install as a Cake Tool

IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest

A high-performance MSTest integration library for Microsoft.Extensions.Logging, enabling seamless log output capture in unit tests. Compatible with .NET Framework 4.6.2+, .NET Core 2.0+, .NET 6/8/9+, and modern .NET platforms.

Features

  • Microsoft Extensions Logging compatible ILogger implementation for MSTest tests
  • Forwards log messages to MSTest's TestContext for test output visibility
  • Multi-targeting: netstandard2.0, netstandard2.1, net462, net8.0, net9.0
  • Full log level support with formatted output
  • Exception logging with stack traces
  • Thread-safe logger implementation
  • Easy integration with existing logging infrastructure

Installation

Install from NuGet:

# .NET CLI
dotnet add package IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest

# Package Manager Console
Install-Package IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest

Quick Start

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class UnitTest1
{
    public TestContext TestContext { get; set; }
    private ILoggerFactory loggerFactory;

    [TestInitialize]
    public void TestInitialize()
    {
        this.loggerFactory = new LoggerFactory(new[] { new MSTestLoggerProvider(TestContext) });
    }

    [TestCleanup]
    public void TestCleanup()
    {
        this.loggerFactory?.Dispose();
    }

    [TestMethod]
    public void TestMethod1()
    {
        var logger = loggerFactory.CreateLogger("Test1");
        logger.LogInformation("Hello World!");

        var typedLogger = loggerFactory.CreateLogger<UnitTest1>();
        typedLogger.LogInformation("Hello from typed logger!");

        // Log with structured data
        logger.LogWarning("Processing {ItemCount} items", 42);

        // Log exceptions
        try
        {
            throw new InvalidOperationException("Test exception");
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "An error occurred during processing");
        }
    }
}

Advanced Usage

Using with Dependency Injection

[TestClass]
public class ServiceTest
{
    public TestContext TestContext { get; set; }
    private ServiceProvider serviceProvider;

    [TestInitialize]
    public void TestInitialize()
    {
        var services = new ServiceCollection();
        services.AddLogging(builder =>
        {
            builder.AddProvider(new MSTestLoggerProvider(TestContext));
        });
        services.AddTransient<MyService>();

        this.serviceProvider = services.BuildServiceProvider();
    }

    [TestCleanup]
    public void TestCleanup()
    {
        this.serviceProvider?.Dispose();
    }

    [TestMethod]
    public void TestService()
    {
        var service = serviceProvider.GetRequiredService<MyService>();
        service.DoWork(); // Logs will appear in test output
    }
}

Base Test Class Pattern

For better reusability, you can create a base test class:

[TestClass]
public abstract class BaseTest
{
    public TestContext TestContext { get; set; }
    protected ILoggerFactory LoggerFactory { get; private set; }

    [TestInitialize]
    public virtual void TestInitialize()
    {
        LoggerFactory = new LoggerFactory(new[] { new MSTestLoggerProvider(TestContext) });
    }

    [TestCleanup]
    public virtual void TestCleanup()
    {
        LoggerFactory?.Dispose();
    }

    protected ILogger<T> CreateLogger<T>() => LoggerFactory.CreateLogger<T>();
    protected ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName);
}

[TestClass]
public class MyServiceTest : BaseTest
{
    [TestMethod]
    public void TestMyService()
    {
        var logger = CreateLogger<MyServiceTest>();
        logger.LogInformation("Testing my service...");
        // Your test logic here
    }
}

Custom Log Formatting

The logger automatically formats messages with log level, category, and timestamp information for clear test output readability.

Example Output

When running tests, log messages appear in the MSTest test output:

info: Test1[0]
      Hello World!
warn: Test1[0]
      Processing 42 items
fail: Test1[0]
      An error occurred during processing
      System.InvalidOperationException: Test exception
         at UnitTest1.TestMethod1() in C:\Example\UnitTest1.cs:line 35

Comparison with Other Test Frameworks

Feature MSTest xUnit NUnit
Test Context ✅ TestContext ✅ ITestOutputHelper ✅ TestContext
Dependency Injection ✅ Manual setup ✅ Constructor injection ✅ Manual setup
Parallel Tests ✅ Supported ✅ Supported ✅ Supported
.NET Core Support ✅ Full support ✅ Full support ✅ Full support

Requirements

  • .NET Framework 4.6.2+ or .NET Core 2.0+ or .NET 6/8/9+
  • MSTest.TestFramework 2.2.1+
  • Microsoft.Extensions.Logging.Abstractions 2.1.0+

License

GPL-3.0-or-later


Feedback and contributions are welcome! Open an issue or PR on GitHub.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.1 116 7/16/2025

# Release Notes
## Version 1.0.1
### Features
- **MSTest Integration**: Seamless integration with MSTest framework for logging output capture
- **ILogger Implementation**: Full Microsoft.Extensions.Logging compatible ILogger implementation
- **TestContext Support**: Forwards log messages to MSTest's TestContext for test output visibility
- **Multi-Target Framework Support**: Compatible with netstandard2.0, netstandard2.1, net462, net8.0, and net9.0
- **Thread-Safe Logging**: Thread-safe logger implementation for parallel test execution
- **Structured Logging**: Support for structured logging with parameters and formatting
- **Exception Logging**: Complete exception logging with stack traces
- **Log Level Support**: Full support for all Microsoft.Extensions.Logging log levels (Trace, Debug, Information, Warning, Error, Critical)
### Benefits
- **Easy Debugging**: Log messages appear directly in MSTest test output for easy debugging
- **No Configuration Required**: Works out-of-the-box with minimal setup
- **Performance Optimized**: High-performance implementation designed for test scenarios
- **Enterprise Ready**: Suitable for enterprise-grade testing scenarios with comprehensive logging needs
- **Developer Friendly**: Simple API that integrates seamlessly with existing logging infrastructure
### Technical Details
- **Dependencies**:
- Microsoft.Extensions.Logging.Abstractions 2.1.0+
- MSTest.TestFramework 2.2.1+
- **Package ID**: IO.Github.Hcoona.MicrosoftExtensions.Logging.MSTest
- **License**: GPL-3.0-or-later
- **Documentation**: Comprehensive README with examples and best practices
### Usage Examples
- Basic logging with ILogger
- Dependency injection integration
- Base test class patterns for reusability
- Structured logging with parameters
- Exception handling and logging
### Compatibility
- .NET Framework 4.6.2+
- .NET Core 2.0+
- .NET 6/8/9+
- All modern .NET platforms
- Visual Studio Test Explorer
- Azure DevOps Test Results
- GitHub Actions and other CI/CD platforms