Coder Social home page Coder Social logo

dshe / mxlogger Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 0.0 137 KB

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

Home Page: https://github.com/dshe/MXLogger

License: Apache License 2.0

C# 100.00%
microsoft-extensions-logging xunit logger logging microsoft-extensions testing mstest nunit test

mxlogger's Introduction

MXLogger  Build status 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)
            .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

mxlogger's People

Contributors

dshe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.