• Stars
    star
    249
  • Rank 162,987 (Top 4 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Logging extensions for xunit

xunit Logging

NuGet

Build status codecov OpenSSF Scorecard

Introduction

MartinCostello.Logging.XUnit provides extensions to hook into the ILogger infrastructure to output logs from your xunit tests to the test output.

ℹ️ This library is designed for the Microsoft logging implementation of ILoggerFactory. For other logging implementations, such as Serilog, consider using packages such as Serilog.Sinks.XUnit instead.

Installation

To install the library from NuGet using the .NET SDK run:

dotnet add package MartinCostello.Logging.XUnit

Usage

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

namespace MyApp.Calculator;

public class CalculatorTests
{
    public CalculatorTests(ITestOutputHelper outputHelper)
    {
        OutputHelper = outputHelper;
    }

    private ITestOutputHelper OutputHelper { get; }

    [Fact]
    public void Calculator_Sums_Two_Integers()
    {
        // Arrange
        var services = new ServiceCollection()
            .AddLogging((builder) => builder.AddXUnit(OutputHelper))
            .AddSingleton<Calculator>();

        var calculator = services
            .BuildServiceProvider()
            .GetRequiredService<Calculator>();

        // Act
        int actual = calculator.Sum(1, 2);

        // Assert
        Assert.AreEqual(3, actual);
    }
}

public sealed class Calculator
{
    private readonly ILogger _logger;

    public Calculator(ILogger<Calculator> logger)
    {
        _logger = logger;
    }

    public int Sum(int x, int y)
    {
        int sum = x + y;

        _logger.LogInformation("The sum of {x} and {y} is {sum}.", x, y, sum);

        return sum;
    }
}

See below for links to more examples:

  1. Unit tests
  2. Integration tests for an ASP.NET Core HTTP application

Feedback

Any feedback or issues can be added to the issues for this project in GitHub.

Repository

The repository is hosted in GitHub: https://github.com/martincostello/xunit-logging.git

License

This project is licensed under the Apache 2.0 license.

Building and Testing

Compiling the library yourself requires Git and the .NET SDK to be installed (version 7.0.100 or later).

To build and test the library locally from a terminal/command-line, run one of the following set of commands:

git clone https://github.com/martincostello/xunit-logging.git
cd xunit-logging
./build.ps1

More Repositories

1

dotnet-minimal-api-integration-testing

An example of integration testing ASP.NET Core Minimal hosting and APIs
C#
321
star
2

sqllocaldb

SQL LocalDB Wrapper is a .NET library providing interop with the Microsoft SQL Server LocalDB Instance API
C#
226
star
3

SignInWithAppleSample

A sample implementation of Sign in with Apple using ASP.NET Core
C#
45
star
4

lambda-test-server

A NuGet package that provides an in-memory test server for testing AWS Lambda functions
C#
31
star
5

dotnet-bumper

Upgrades projects to a newer version of .NET
C#
25
star
6

dotnet-playwright-tests

.NET tests using Playwright
C#
24
star
7

update-dotnet-sdk

A GitHub Action that updates the .NET SDK
TypeScript
18
star
8

dependabot-helper

An application that helps manage Dependabot updates across multiple repositories.
C#
14
star
9

alexa-london-travel-site

The companion site for the London Travel Amazon Alexa skill
C#
14
star
10

website

Martin Costello's website
C#
13
star
11

adventofcode

Solutions for Advent of Code in C#
C#
11
star
12

costellobot

GitHub automation for my repositories.
C#
10
star
13

apple-fitness-workout-mapper

Visualise multiple journeys from Apple Fitness on a map
C#
9
star
14

applepayjs-typings

TypeScript typings for Apple Pay JS
TypeScript
9
star
15

dotnet-macos-notarization-example

An example of implementing notarization for a self-contained .NET application on macOS
PowerShell
9
star
16

polly-rate-limiting

An example of using Polly's RateLimit policy in a ASP.NET Core Minimal API
C#
9
star
17

antiforgery-testing-application-part

Sample application for integration testing ASP.NET Core applications using antiforgery protections
C#
8
star
18

browserstack-automate

.NET client for the BrowserStack Automate REST API
C#
8
star
19

alexa-london-travel

An Amazon Alexa skill for checking the status of travel in London
C#
8
star
20

MinimalApisWithJsonSourceGenerator

Sample application that uses the .NET 6 JSON source generator with Minimal APIs
C#
8
star
21

api

Martin Costello's API
C#
7
star
22

dotnet-core-selenium-tests

A simple test implemented in .NET Core for Selenium
C#
7
star
23

update-static-assets

A GitHub Action that updates HTML static assets
TypeScript
6
star
24

openapi-extensions

Extensions for Microsoft.AspNetCore.OpenApi
C#
6
star
25

aspnetcore-openapi

A demonstration of different ways to add OpenAPI (Swagger) support to ASP.NET Core applications
C#
5
star
26

rebaser

A GitHub Action that can resolve basic version conflicts between Git branches
TypeScript
5
star
27

aspnet-core-pseudo-localization

An example application that demonstrates using pseudo-localization with ASP.NET Core
C#
5
star
28

dotnet-patch-automation-sample

Sample repository that demonstrates automated monthly patching of .NET applications
C#
5
star
29

dotnet-8-samples

Samples for new features in .NET 8
C#
4
star
30

project-euler

My solutions for problems on https://projecteuler.net/
C#
3
star
31

aspnet-core-7-samples

Samples for ASP.NET Core 7
C#
3
star
32

presentations

Content for public presentations πŸ›
3
star
33

crank-sandbox

A sandbox for playing with the crank .NET performance testing tool
PowerShell
3
star
34

benchmarks-dashboard

A dashboard for visualising benchmark results from benchmarkdotnet-results-publisher
C#
2
star
35

wait-for-nuget-package

Waits for a new version of a NuGet package to be published
C#
2
star
36

dotnet-lambda-annotations

Playground repo for Amazon.Lambda.Annotations
C#
2
star
37

benchmarks-demo

Demonstrates continuous benchmarking in GitHub Actions using BenchmarkDotNet
C#
2
star
38

twitterarchiveparser

Parses the JSON of Twitter archives
C#
2
star
39

Refit-Json-Benchmarks

Benchmarks for (de)serializing JSON using Refit
C#
2
star
40

credit-card-splitter

An application to split parts of a credit card bill between two people
JavaScript
2
star
41

blog

Martin Costello's blog
HTML
1
star
42

cdn

Source content for my CDN
1
star
43

nugetpackagestidier

Tidies up NuGet packages in your local NuGet package folders.
C#
1
star
44

polly-sandbox

A sandbox repository for integrating Polly
C#
1
star
45

martincostello

Martin Costello's README
1
star
46

azure-functions

Contains custom Azure Functions
C#
1
star
47

Update-DotNet-Core-SDK

A PowerShell script that can be used by GitHub Actions to keep your .NET SDK up-to-date
PowerShell
1
star
48

bootstrap

Scripts to setup a new computer
PowerShell
1
star
49

benchmarks

Repository for storing benchmark results
1
star
50

benchmarkdotnet-results-publisher

A GitHub Action that publishes results from BenchmarkDotNet benchmarks to a GitHub repository
TypeScript
1
star
51

github-automation

GitHub automation
TypeScript
1
star