Skip to content

Commit

Permalink
Support xunit v3 (#804)
Browse files Browse the repository at this point in the history
- Add support for xunit v3 via the new MartinCostello.Logging.XUnit.v3 NuGet package.
- Bump version to 0.5.0.
- Add a package icon (xunit's own logo).
  • Loading branch information
martincostello authored Dec 20, 2024
1 parent 8eae44c commit e39e9bf
Show file tree
Hide file tree
Showing 45 changed files with 2,990 additions and 2,814 deletions.
1 change: 1 addition & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ updates:
- dependency-name: Microsoft.Extensions.Logging
- dependency-name: xunit.abstractions
- dependency-name: xunit.extensibility.execution
- dependency-name: xunit.v3.extensibility.core
16 changes: 11 additions & 5 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<NeutralLanguage>en-US</NeutralLanguage>
<NuGetAuditMode>direct</NuGetAuditMode>
<Nullable>enable</Nullable>
<PackageIcon></PackageIcon>
<PackageIcon>package-icon.png</PackageIcon>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/martincostello/xunit-logging</PackageProjectUrl>
<PackageReadmeFile>package-readme.md</PackageReadmeFile>
Expand All @@ -44,8 +44,8 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<UseArtifactsOutput>true</UseArtifactsOutput>
<AssemblyVersion>0.4.0.0</AssemblyVersion>
<VersionPrefix>0.4.1</VersionPrefix>
<AssemblyVersion>0.5.0.0</AssemblyVersion>
<VersionPrefix>0.5.0</VersionPrefix>
</PropertyGroup>
<PropertyGroup Condition=" '$(GITHUB_ACTIONS)' != '' ">
<VersionSuffix Condition=" '$(VersionSuffix)' == '' AND '$(GITHUB_HEAD_REF)' == '' ">beta.$(GITHUB_RUN_NUMBER)</VersionSuffix>
Expand All @@ -72,10 +72,16 @@
</PropertyGroup>
<ItemGroup>
<Using Include="System.Globalization" />
<Using Include="Xunit" />
<Using Include="Xunit.Sdk" />
</ItemGroup>
<ItemGroup Condition=" $(DefineConstants.Contains('XUNIT_V3')) ">
<Using Include="Xunit.v3" />
</ItemGroup>
<ItemGroup Condition=" !$(DefineConstants.Contains('XUNIT_V3')) ">
<Using Include="Xunit.Abstractions" />
</ItemGroup>
<ItemGroup Condition=" '$(IsTestProject)' == 'true' ">
<Using Include="Shouldly" />
<Using Include="Xunit" />
<Using Include="Xunit.Abstractions" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<PackageVersion Include="xunit.abstractions" Version="2.0.3" />
<PackageVersion Include="xunit.extensibility.execution" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.0" />
<PackageVersion Include="xunit.v3" Version="1.0.0" />
<PackageVersion Include="xunit.v3.extensibility.core" Version="1.0.0" />
</ItemGroup>
<ItemGroup Condition=" '$(IsTestProject)' == 'true' ">
<PackageReference Include="coverlet.msbuild" PrivateAssets="All" />
Expand Down
14 changes: 14 additions & 0 deletions Logging.XUnit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
.github\workflows\release.yml = .github\workflows\release.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MartinCostello.Logging.XUnit.v3", "src\Logging.XUnit.v3\MartinCostello.Logging.XUnit.v3.csproj", "{2C161BB9-E6D0-4561-9D98-EDEAA8A49C5E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MartinCostello.Logging.XUnit.v3.Tests", "tests\Logging.XUnit.v3.Tests\MartinCostello.Logging.XUnit.v3.Tests.csproj", "{828C7199-ADF2-456C-8BBB-4A73A329D017}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -85,6 +89,14 @@ Global
{B690F271-3B5D-4975-A607-AED1768595B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B690F271-3B5D-4975-A607-AED1768595B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B690F271-3B5D-4975-A607-AED1768595B1}.Release|Any CPU.Build.0 = Release|Any CPU
{2C161BB9-E6D0-4561-9D98-EDEAA8A49C5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C161BB9-E6D0-4561-9D98-EDEAA8A49C5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C161BB9-E6D0-4561-9D98-EDEAA8A49C5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C161BB9-E6D0-4561-9D98-EDEAA8A49C5E}.Release|Any CPU.Build.0 = Release|Any CPU
{828C7199-ADF2-456C-8BBB-4A73A329D017}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{828C7199-ADF2-456C-8BBB-4A73A329D017}.Debug|Any CPU.Build.0 = Debug|Any CPU
{828C7199-ADF2-456C-8BBB-4A73A329D017}.Release|Any CPU.ActiveCfg = Release|Any CPU
{828C7199-ADF2-456C-8BBB-4A73A329D017}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -97,6 +109,8 @@ Global
{BB443063-F523-474D-83E3-D5FF5B075950} = {2684B19D-7D49-4099-8BD2-4D281455EB29}
{B690F271-3B5D-4975-A607-AED1768595B1} = {278BCCB1-39B2-46DB-9395-7F85995A6132}
{7764A046-DEE7-4D88-83E2-537DB7767123} = {D0426D09-1FF8-4E1F-A9AF-38DDEE5D7CCA}
{2C161BB9-E6D0-4561-9D98-EDEAA8A49C5E} = {2684B19D-7D49-4099-8BD2-4D281455EB29}
{828C7199-ADF2-456C-8BBB-4A73A329D017} = {278BCCB1-39B2-46DB-9395-7F85995A6132}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3B9E157C-5E92-4357-B233-281B4530EABD}
Expand Down
68 changes: 52 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
# xunit Logging

[![NuGet](https://img.shields.io/nuget/v/MartinCostello.Logging.XUnit?logo=nuget&label=Latest&color=blue)](https://www.nuget.org/packages/MartinCostello.Logging.XUnit "Download MartinCostello.Logging.XUnit from NuGet")
[![NuGet Downloads](https://img.shields.io/nuget/dt/MartinCostello.Logging.XUnit?logo=nuget&label=Downloads&color=blue)](https://www.nuget.org/packages/MartinCostello.Logging.XUnit "Download MartinCostello.Logging.XUnit from NuGet")
[![Build status][build-badge]][build-status]
[![codecov][coverage-badge]][coverage-report]
[![OpenSSF Scorecard][scorecard-badge]][scorecard-report]

[![Build status](https://github.com/martincostello/xunit-logging/workflows/build/badge.svg?branch=main&event=push)](https://github.com/martincostello/xunit-logging/actions?query=workflow%3Abuild+branch%3Amain+event%3Apush)
[![codecov](https://codecov.io/gh/martincostello/xunit-logging/branch/main/graph/badge.svg)](https://codecov.io/gh/martincostello/xunit-logging)
[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/martincostello/xunit-logging/badge)](https://securityscorecards.dev/viewer/?uri=github.com/martincostello/xunit-logging)
| **xunit version** | **Package** | **NuGet Version** |
|:------------------|:------------|:------------------|
| xunit v2 | [MartinCostello.Logging.XUnit][package-download-v2] | [![NuGet][package-badge-version-v2]][package-download-v2] [![NuGet Downloads][package-badge-downloads-v2]][package-download-v2] |
| xunit v3 | [MartinCostello.Logging.XUnit.v3][package-download-v3] | [![NuGet][package-badge-version-v3]][package-download-v3] [![NuGet Downloads][package-badge-downloads-v3]][package-download-v3] |

## Introduction

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

Projects using xunit v2 should use the `MartinCostello.Logging.XUnit` package, while projects using
xunit v3 should use the `MartinCostello.Logging.XUnit.v3` package.

> [!NOTE]
> This library is designed for the Microsoft logging implementation of `ILoggerFactory`. For other logging implementations, such as [Serilog](https://serilog.net/), consider using packages such as [Serilog.Sinks.XUnit](https://github.com/trbenning/serilog-sinks-xunit) instead.
> This library is designed for the Microsoft logging implementation of `ILoggerFactory`.
> For other logging implementations, such as [Serilog][serilog], consider using packages such as [Serilog.Sinks.XUnit][serilog-sinks-xunit] instead.
### Installation

To install the library from [NuGet](https://www.nuget.org/packages/MartinCostello.Logging.XUnit/ "MartinCostello.Logging.XUnit on NuGet.org") using the .NET SDK run:
To install the library from NuGet using the .NET SDK run one of the following commands.

#### For xunit v2

```console
dotnet add package MartinCostello.Logging.XUnit
```

#### For xunit v3

```console
dotnet add package MartinCostello.Logging.XUnit.v3
```

### Usage

```csharp
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;
using Xunit.Abstractions; // For xunit v2 - not required for xunit v3
namespace MyApp.Calculator;

Expand Down Expand Up @@ -68,29 +83,50 @@ public sealed class Calculator(ILogger<Calculator> logger)

See below for links to more examples:

- [Unit tests](https://github.com/martincostello/xunit-logging/blob/main/tests/Logging.XUnit.Tests/Examples.cs "Unit test examples")
- [Integration tests for an ASP.NET Core HTTP application](https://github.com/martincostello/xunit-logging/blob/main/tests/Logging.XUnit.Tests/Integration/HttpApplicationTests.cs "Integration test examples")
- [Unit tests][example-unit-tests]
- [Integration tests for an ASP.NET Core HTTP application][example-integration-tests]

## Feedback

Any feedback or issues can be added to the issues for this project in [GitHub](https://github.com/martincostello/xunit-logging/issues "Issues for this project on GitHub.com").
Any feedback or issues can be added to the issues for this project in [GitHub][issues].

## Repository

The repository is hosted in [GitHub](https://github.com/martincostello/xunit-logging "This project on GitHub.com"): <https://github.com/martincostello/xunit-logging.git>
The repository is hosted in [GitHub][repo]: <https://github.com/martincostello/xunit-logging.git>

## License

This project is licensed under the [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt "The Apache 2.0 license") license.
This project is licensed under the [Apache 2.0][license] license.

## Building and Testing

Compiling the library yourself requires Git and the [.NET SDK](https://www.microsoft.com/net/download/core "Download the .NET SDK") to be installed (version `9.0.100` or later).
Compiling the solution yourself requires Git and the [.NET SDK][dotnet-sdk] to be installed (version `9.0.100` or later).

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

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

[build-badge]: https://github.com/martincostello/xunit-logging/actions/workflows/build.yml/badge.svg?branch=main&event=push
[build-status]: https://github.com/martincostello/xunit-logging/actions?query=workflow%3Abuild+branch%3Amain+event%3Apush "Continuous Integration for this project"
[coverage-badge]: https://codecov.io/gh/martincostello/xunit-logging/branch/main/graph/badge.svg
[coverage-report]: https://codecov.io/gh/martincostello/xunit-logging "Code coverage report for this project"
[scorecard-badge]: https://api.securityscorecards.dev/projects/github.com/martincostello/xunit-logging/badge
[scorecard-report]: https://securityscorecards.dev/viewer/?uri=github.com/martincostello/xunit-logging "OpenSSF Scorecard for this project"
[dotnet-sdk]: https://dot.net/download "Download the .NET SDK"
[example-integration-tests]: https://github.com/martincostello/xunit-logging/blob/main/tests/Shared/Integration/HttpApplicationTests.cs "Integration test examples"
[example-unit-tests]: https://github.com/martincostello/xunit-logging/blob/main/tests/Shared/Examples.cs "Unit test examples"
[issues]: https://github.com/martincostello/xunit-logging/issues "Issues for this project on GitHub.com"
[license]: https://www.apache.org/licenses/LICENSE-2.0.txt "The Apache 2.0 license"
[package-badge-downloads-v2]: https://img.shields.io/nuget/dt/MartinCostello.Logging.XUnit?logo=nuget&label=Downloads&color=blue
[package-badge-downloads-v3]: https://img.shields.io/nuget/dt/MartinCostello.Logging.XUnit.v3?logo=nuget&label=Downloads&color=blue
[package-badge-version-v2]: https://img.shields.io/nuget/v/MartinCostello.Logging.XUnit?logo=nuget&label=Latest&color=blue
[package-badge-version-v3]: https://img.shields.io/nuget/v/MartinCostello.Logging.XUnit.v3?logo=nuget&label=Latest&color=blue
[package-download-v2]: https://www.nuget.org/packages/MartinCostello.Logging.XUnit "Download MartinCostello.Logging.XUnit from NuGet"
[package-download-v3]: https://www.nuget.org/packages/MartinCostello.Logging.XUnit.v3 "Download MartinCostello.Logging.XUnit.v3 from NuGet"
[repo]: https://github.com/martincostello/xunit-loggingE "This project on GitHub.com"
[serilog]: https://serilog.net/ "Serilog website"
[serilog-sinks-xunit]: https://github.com/trbenning/serilog-sinks-xunit "Serilog.Sinks.XUnit on GitHub"
12 changes: 9 additions & 3 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ $ProgressPreference = "SilentlyContinue"
$solutionPath = $PSScriptRoot
$sdkFile = Join-Path $solutionPath "global.json"

$libraryProject = Join-Path $solutionPath "src" "Logging.XUnit" "MartinCostello.Logging.XUnit.csproj"
$libraryProjects = @(
(Join-Path $solutionPath "src" "Logging.XUnit" "MartinCostello.Logging.XUnit.csproj")
(Join-Path $solutionPath "src" "Logging.XUnit.v3" "MartinCostello.Logging.XUnit.v3.csproj")
)

$testProjects = @(
(Join-Path $solutionPath "tests" "Logging.XUnit.Tests" "MartinCostello.Logging.XUnit.Tests.csproj")
(Join-Path $solutionPath "tests" "Logging.XUnit.v3.Tests" "MartinCostello.Logging.XUnit.v3.Tests.csproj")
)

$dotnetVersion = (Get-Content $sdkFile | Out-String | ConvertFrom-Json).sdk.version
Expand Down Expand Up @@ -102,8 +106,10 @@ function DotNetTest {
}
}

Write-Information "Packaging library..."
DotNetPack $libraryProject
Write-Information "Packaging libraries..."
ForEach ($libraryProject in $libraryProjects) {
DotNetPack $libraryProject
}

if (-Not $SkipTests) {
Write-Information "Running tests..."
Expand Down
Binary file added package-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 8 additions & 4 deletions package-readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

## Introduction

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

### Usage

```csharp
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;
using Xunit.Abstractions; // For xunit v2 - not required for xunit v3
namespace MyApp.Calculator;

Expand Down Expand Up @@ -50,8 +51,11 @@ public sealed class Calculator(ILogger<Calculator> logger)

## Feedback

Any feedback or issues can be added to the issues for this project in [GitHub](https://github.com/martincostello/xunit-logging/issues "Issues for this project on GitHub.com").
Any feedback or issues can be added to the issues for this project in [GitHub][issues].

## License

This project is licensed under the [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt "The Apache 2.0 license") license.
This project is licensed under the [Apache 2.0][license] license.

[issues]: https://github.com/martincostello/xunit-logging/issues "Issues for this package on GitHub.com"
[license]: https://www.apache.org/licenses/LICENSE-2.0.txt "The Apache 2.0 license"
35 changes: 35 additions & 0 deletions src/Logging.XUnit.v3/MartinCostello.Logging.XUnit.v3.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyTitle>Logging Extensions for xunit v3</AssemblyTitle>
<DefineConstants>$(DefineConstants);XUNIT_V3</DefineConstants>
<Description>Extensions for Microsoft.Extensions.Logging for xunit v3.</Description>
<EnablePackageValidation>true</EnablePackageValidation>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>true</IsPackable>
<OutputType>Library</OutputType>
<PackageId>MartinCostello.Logging.XUnit.v3</PackageId>
<!--
<PackageValidationBaselineVersion>0.5.0</PackageValidationBaselineVersion>
-->
<RootNamespace>MartinCostello.Logging.XUnit</RootNamespace>
<Summary>$(Description)</Summary>
<TargetFrameworks>net8.0;net472</TargetFrameworks>
<Title>xunit v3 Logging Extensions</Title>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Shared\**\*.cs" Link="%(Link)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" VersionOverride="8.0.1" />
<PackageReference Include="xunit.v3.extensibility.core" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.InteropServices.GuidAttribute">
<_Parameter1>ed8d1c5e-3ee7-45fe-8d1d-94257a71f02a</_Parameter1>
</AssemblyAttribute>
<InternalsVisibleTo Include="MartinCostello.Logging.XUnit.v3.Tests" PublicKey="$(StrongNamePublicKey)" />
</ItemGroup>
</Project>
11 changes: 0 additions & 11 deletions src/Logging.XUnit/CompatibilitySuppressions.xml

This file was deleted.

10 changes: 5 additions & 5 deletions src/Logging.XUnit/MartinCostello.Logging.XUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
<Description>Extensions for Microsoft.Extensions.Logging for xunit.</Description>
<EnablePackageValidation>true</EnablePackageValidation>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>true</IsPackable>
<OutputType>Library</OutputType>
<PackageId>MartinCostello.Logging.XUnit</PackageId>
<PackageValidationBaselineVersion>0.3.0</PackageValidationBaselineVersion>
<PackageValidationBaselineVersion>0.4.0</PackageValidationBaselineVersion>
<RootNamespace>MartinCostello.Logging.XUnit</RootNamespace>
<Summary>$(Description)</Summary>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<Title>xunit Logging Extensions</Title>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<EnableAotAnalyzer>true</EnableAotAnalyzer>
<EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<IsAotCompatible>true</IsAotCompatible>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Shared\**\*.cs" Link="%(Link)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="xunit.abstractions" />
Expand Down
Loading

0 comments on commit e39e9bf

Please sign in to comment.