Walter.Extensions.Logging.MSTest 2024.8.15.1410

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Walter.Extensions.Logging.MSTest --version 2024.8.15.1410                
NuGet\Install-Package Walter.Extensions.Logging.MSTest -Version 2024.8.15.1410                
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="Walter.Extensions.Logging.MSTest" Version="2024.8.15.1410" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Walter.Extensions.Logging.MSTest --version 2024.8.15.1410                
#r "nuget: Walter.Extensions.Logging.MSTest, 2024.8.15.1410"                
#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 Walter.Extensions.Logging.MSTest as a Cake Addin
#addin nuget:?package=Walter.Extensions.Logging.MSTest&version=2024.8.15.1410

// Install Walter.Extensions.Logging.MSTest as a Cake Tool
#tool nuget:?package=Walter.Extensions.Logging.MSTest&version=2024.8.15.1410                

About this NuGet Package

This package, Walter.Extensions.Logging.MSTest, was created to assist in capturing and directing ILogger output to the MSTest output window. This is especially useful for debugging and tracing logs during test execution, ensuring that all log output is easily accessible in your test results.

How to Use:

Below are samples of how to integrate the package into a test project using the MSTest framework:

[TestClass]
public class MyFactoryTests
{
    static TestContext? _context;

    [ClassInitialize]
    public static void ClassInit(TestContext context)
    { 
        _context = context;
    }

    [TestMethod]
    public void Test1() 
    {
        using var service = new ServiceCollection()
            //your di configuration
            .AddMsUnitTestLogger(_context!, LogLevel.Debug)
            .BuildServiceProvider();

        ...
    }
    ...
}

2nd Sample

Perhaps a more complete option implementation will provide more value

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

namespace TestProject
{
    [TestClass]
    public class UnitTest1
    {
        private static ServiceProvider? _serviceProvider;

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            var serviceCollection = new ServiceCollection();
            // Integrating Microsoft UnitTest ILogger support here
            serviceCollection.AddMsUnitTestLogger(context, LogLevel.Debug);
            _serviceProvider = serviceCollection.BuildServiceProvider();
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            _serviceProvider?.Dispose();
        }

        [TestMethod]
        [ExpectedException(typeof(System.IO.DirectoryNotFoundException))]
        public void TestMethod1()
        {
            if (_serviceProvider is null) Assert.Fail("Class setup failed");

            // Get non-generic ILogger
            var logger = _serviceProvider.GetRequiredService<ILogger>();
            logger.LogInformation("This is a non-generic test log message.");

            // Get generic ILogger<>
            var genericLogger = _serviceProvider.GetRequiredService<ILogger<UnitTest1>>();
            genericLogger.LogInformation("This is a generic test log message.");

            try
            {
                var willFail = File.Open("x:\\dirNotfound\\and\\file\\doesNotExist.txt", FileMode.Open, FileAccess.Read);
            }
            catch (Exception ex)
            {
                genericLogger.LogException(ex);
                throw;
            }
        }
    }
}

Expected Output

When the above test is executed, the output will be captured in the MSTest TestContext and should appear similar to the following:


TestContext Messages:
Information:  - This is a non-generic test log message.
Information: TestProject.UnitTest1 - This is a generic test log message.
Error: TestProject.UnitTest1 - Calling UnitTest1.TestMethod1 in C:\Users\wave\source\repos\Walter.Extensions.Logging.MSTest\TestProject\UnitTest1.cs generated a DirectoryNotFoundException on line: 49. The exception: Could not find a part of the path 'x:\dirNotfound\and\file\doesNotExist.txt'.
System.IO.DirectoryNotFoundException: Could not find a part of the path 'x:\dirNotfound\and\file\doesNotExist.txt'.
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.File.Open(String path, FileMode mode, FileAccess access)
   at TestProject.UnitTest1.TestMethod1() in C:\Users\wave\source\repos\Walter.Extensions.Logging.MSTest\TestProject\UnitTest1.cs:line 45

Explanation

  • Non-Generic Logger Output: Captures and displays the log message generated by the non-generic ILogger.
  • Generic Logger Output: Displays log messages associated with the specific type UnitTest1, giving you more context about where the logs originated.
  • Exception Logging: Demonstrates how the LogException extension method formats and logs an exception, providing detailed information including the exception type, the line number, and the error message.

By following this structure, users will have a clear understanding of both how to implement the package and what output to expect when running their tests.

Why Use the Walter.Extensions.Logging.MSTest Package?

Using the Walter.Extensions.Logging.MSTest NuGet package is an invaluable tool when you want to enhance your MSTest framework's logging capabilities. By directing ILogger output directly to the MSTest context, you gain several advantages:

  • Enhanced Debugging: All log output is captured directly in the MSTest results, making it easy to trace what happened during the test, especially when tests fail.
  • Seamless Integration: The package integrates effortlessly with your existing ILogger setup, requiring minimal changes to your test project.
  • Increased Visibility: Log messages that would typically be difficult to access during automated test runs are now directly available, making it easier to diagnose issues.

Including in a Build Pipeline

Including Walter.Extensions.Logging.MSTest in your build pipeline offers significant benefits:

  • Log Parsing and Analysis: By capturing all log output during test execution, you can set up your build pipeline to parse the logs for specific patterns or errors, enabling automatic validation of log content as part of your integration tests.

  • Continuous Integration (CI) Insights: Integrating this logging capability within your CI pipeline allows for detailed insights into the test runs. If a test fails, the logs are readily available for examination without needing to replicate the issue locally.

  • Improved Test Coverage: Since logging can be crucial in understanding the flow and catching unexpected issues, having detailed logs helps ensure that your tests cover more than just expected outcomes—they also handle and document unexpected ones effectively.

In summary,

The Walter.Extensions.Logging.MSTest package enhances the visibility and usability of logs within MSTest, making it a powerful tool for both development and continuous integration environments.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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.  net8.0-windows7.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  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
2024.8.25.1115 121 8/25/2024
2024.8.25.1057 119 8/25/2024
2024.8.19.1112 111 8/19/2024
2024.8.15.1410 111 8/15/2024
2024.8.5.1010 118 8/12/2024