GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - dshe/MXLogger: A minimal Microsoft.Extensions.Logging provider for test frameworks such as xUnit, NUnit and MSTest.

BranchesTags
Open more actions menu

Latest commit

 

History

202 Commits
202 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
MXLogger.MSTests
MXLogger.MSTests
 
 
MXLogger.NUnitTests
MXLogger.NUnitTests
 
 
MXLogger.xUnitTests
MXLogger.xUnitTests
 
 
MXLogger
MXLogger
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
MXLogger.slnx
MXLogger.slnx
 
 
README.md
README.md
 
 
global.json
global.json
 
 

Repository files navigation

MXLogger  

Build, Test, Publish NuGet NuGet License

Minimalist Microsoft Extensions Logging Provider

  • .NET Standard 2.0 library
  • compatible with xUnit, NUnit, MSTest and other test frameworks
  • customizable formatting
  • supports scopes
  • supports Microsoft.Extensions.DependencyInjection
  • dependencies: Microsoft.Extensions.Logging

Installation

PM> Install-Package MXLogger

Simple Example (xUnit)

using Microsoft.Extensions.Logging;
using Xunit;
namespace Xunit.Abstractions;

// This class may be used as the base class for test classes.
public abstract class XunitTestBase
{
    private readonly ITestOutputHelper Output;
    protected readonly ILoggerFactory LogFactory;
    protected readonly ILogger Logger;
    protected void Write(string format, params object[] args) =>
        Output.WriteLine(string.Format(format, args) + Environment.NewLine);

    protected XunitTestBase(ITestOutputHelper output, LogLevel logLevel = LogLevel.Debug, string name = "Test")
    {
        Output = output;
        LogFactory = LoggerFactory.Create(builder => builder
            .AddMXLogger(output.WriteLine, MXLogFormatType.SingleLine)
            .SetMinimumLevel(logLevel));
        Logger = LogFactory.CreateLogger(name);
    }
}
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;
namespace YourNamespace;

public class SimpleExample : XunitTestBase
{
    public SimpleExample(ITestOutputHelper output) : base(output) { }

    [Fact]
    public void Test()
    {
        Logger.LogInformation("message!");
        Write("test!");
    }
}

output:

Info: Test
message!

test!

Dependency Injection Example (xUnit)

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Xunit.Abstractions;

public class MyComponent
{
    private readonly ILogger Logger;
    
    public MyComponent(ILogger<MyComponent> logger)
    {
        Logger = logger;
    }
    
    public void Run()
    {
        Logger.LogCritical("Message");
        ...
    }    
}

public class DependencyInjectionTest
{
    private readonly MyComponent MyComponent;

    public DependencyInjectionTest(ITestOutputHelper output)
    {
        MyComponent = new ServiceCollection()
            .AddTransient<MyComponent>()
            .AddLogging(builder => builder
                .AddMXLogger(output.WriteLine)
                .SetMinimumLevel(LogLevel.Debug))
            .BuildServiceProvider()
            .GetRequiredService<MyComponent>();
    }

    [Fact]
    public void Test()
    {
        MyComponent.Run();
        ...
    }
}

output:

Crit: MyNamespace.MyComponent
Message

About

A minimal Microsoft.Extensions.Logging provider for test frameworks such as xUnit, NUnit and MSTest.

github.com/dshe/MXLogger

Topics

loggerloggingmicrosoft-extensionsmicrosoft-extensions-loggingmstestnunittesttestingxunit

Resources

Readme
Apache-2.0 license
Activity

Stars

7 stars

Watchers

1 watching

Forks

0 forks
Report repository

Used by

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.