diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..c4088ed --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,42 @@ +name: build + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + linux-build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Setup .NET + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 7.0.x + - name: Restore dependencies + run: dotnet restore + - name: Build + run: dotnet build --no-restore --configuration Release + - name: Test + run: dotnet test --no-build --no-restore --configuration Release --verbosity normal -f net7.0 + win-build: + runs-on: windows-latest + steps: + - uses: actions/checkout@v2 + - name: Setup MSBuild + uses: microsoft/setup-msbuild@v1 + - name: Setup NuGet + uses: NuGet/setup-nuget@v1.0.5 + - name: Setup VSTest + uses: darenm/Setup-VSTest@v1 + - name: Navigate to Workspace + run: cd $GITHUB_WORKSPACE + - name: Restore Packages + run: nuget restore Logging.Xunit.sln + - name: Build Solution + run: | + msbuild.exe Logging.Xunit.sln /p:platform="Any CPU" /p:configuration="Release" + - name: Run Tests + run: vstest.console.exe .\test\Logging.Xunit.UnitTests\bin\Release\net472\Logging.Xunit.UnitTests.dll diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml new file mode 100644 index 0000000..e91238a --- /dev/null +++ b/.github/workflows/package.yml @@ -0,0 +1,33 @@ +name: package + +on: + push: + tags: + - 'v*.*.*' + +jobs: + linux-build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Setup .NET + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 7.0.x + - name: Restore dependencies + run: dotnet restore + - name: Build + run: dotnet build --no-restore --configuration Release + - name: Test + run: dotnet test --no-build --no-restore --configuration Release --verbosity normal -f net7.0 + - name: Package + run: | + dotnet pack --no-build --no-restore --configuration Release --output ./artifacts/Logging.Xunit ./src/Logging.Xunit/Logging.Xunit.csproj + - name: Add Github Nuget Source + run: dotnet nuget add source --username devodo --password ${{ secrets.PACKAGE_REPOSITORY_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/devodo/index.json" + - name: Nuget Push + run: | + dotnet nuget push ./artifacts/Logging.Xunit/*.nupkg --api-key ${{ secrets.PACKAGE_REPOSITORY_TOKEN }} --source github \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..234b40c --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,31 @@ +name: publish + +on: + push: + tags: + - 'v[0-9]+.[0-9]+.[0-9]+' + +jobs: + linux-build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Setup .NET + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 7.0.x + - name: Restore dependencies + run: dotnet restore + - name: Build + run: dotnet build --no-restore --configuration Release + - name: Test + run: dotnet test --no-build --no-restore --configuration Release --verbosity normal -f net7.0 + - name: Package + run: | + dotnet pack --no-build --no-restore --configuration Release --output ./artifacts/Logging.Xunit ./src/Logging.Xunit/Logging.Xunit.csproj + - name: Nuget Push + run: | + dotnet nuget push ./artifacts/Logging.Xunit/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_API_KEY }} \ No newline at end of file diff --git a/README.md b/README.md index c9861b4..65d44ba 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,9 @@ [![nuget](https://img.shields.io/nuget/v/Devodo.Logging.Xunit.svg)](https://www.nuget.org/packages/Devodo.Logging.Xunit) [![build](https://github.com/devodo/Logging.Xunit/actions/workflows/build.yml/badge.svg)](https://github.com/devodo/Logging.Xunit/actions/workflows/build.yml) -A Microsoft.Extensions.Logging adapter for xUnit test output +A Microsoft.Extensions.Logging adapter for xUnit test output. + +The log output format and config options are based on the [SimpleConsoleFormatter](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.consoleloggerextensions.addsimpleconsole) from [Microsoft.Extensions.Logging.Console](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console/). # Installing @@ -70,8 +72,19 @@ public class ExampleTests } ``` -The test above outputs the following: +The output from the test above is: ``` info: Program[0] Handling echo request with input: hello xunit logging ``` + +Additional format options can be configured: +```csharp +logging.AddXunit(_outputHelper, options => +{ + options.IncludeScopes = true; + options.TimestampFormat = "HH:mm:ss "; + options.SingleLine = false; + options.UseUtcTimestamp = true; +}); +``` \ No newline at end of file diff --git a/examples/Logging.Xunit.ExampleTests/ExampleTests.cs b/examples/Logging.Xunit.ExampleTests/ExampleTests.cs index 3eff20c..842c98d 100644 --- a/examples/Logging.Xunit.ExampleTests/ExampleTests.cs +++ b/examples/Logging.Xunit.ExampleTests/ExampleTests.cs @@ -14,7 +14,7 @@ public ExampleTests(ITestOutputHelper outputHelper) } [Fact] - public async Task GivenHttpClient_WhenGetRoot_ThenRespondsHello() + public async Task GivenHttpClient_WhenEchoMessage_ThenRespondsMessage() { // ARRANGE var httpClient = new WebApplicationFactory().WithWebHostBuilder(builder => @@ -32,4 +32,30 @@ public async Task GivenHttpClient_WhenGetRoot_ThenRespondsHello() // ASSERT Assert.Equal("hello xunit logging", await response.Content.ReadAsStringAsync()); } + + [Fact] + public async Task GivenHttpClient_WhenEcho_ThenResponds() + { + // ARRANGE + var httpClient = new WebApplicationFactory().WithWebHostBuilder(builder => + { + builder.ConfigureLogging(logging => + { + // Add an xUnit logging provider that writes to the ITestOutputHelper + logging.AddXunit(_outputHelper, options => + { + options.IncludeScopes = true; + options.TimestampFormat = "HH:mm:ss "; + options.SingleLine = false; + options.UseUtcTimestamp = true; + }); + }); + }).CreateClient(); + + // ACT + var response = await httpClient.GetAsync("/echo/hello xunit logging"); + + // ASSERT + Assert.Equal("hello xunit logging", await response.Content.ReadAsStringAsync()); + } } \ No newline at end of file diff --git a/src/Logging.Xunit/Logging.Xunit.csproj b/src/Logging.Xunit/Logging.Xunit.csproj index 3e62c34..d018d8f 100644 --- a/src/Logging.Xunit/Logging.Xunit.csproj +++ b/src/Logging.Xunit/Logging.Xunit.csproj @@ -1,14 +1,39 @@ + + + Devodo.Logging.Xunit + David Naylor + A Microsoft.Extensions.Logging adapter for xUnit test output + MIT Copyright (c) David Naylor + https://github.com/devodo/Logging.Xunit + https://github.com/devodo/Logging.Xunit + xunit logging logger log + MIT + git + Devodo.Logging.Xunit + Devodo.Logging.Xunit + v + true + snupkg + true + README.md + - netstandard2.1 10 enable + netstandard2.1;netstandard2.0 + + + + + + diff --git a/src/Logging.Xunit/XunitLoggerExtensions.cs b/src/Logging.Xunit/XunitLoggerExtensions.cs index e50e318..b5f3f22 100644 --- a/src/Logging.Xunit/XunitLoggerExtensions.cs +++ b/src/Logging.Xunit/XunitLoggerExtensions.cs @@ -12,7 +12,7 @@ public static class XunitLoggerExtensions /// /// Add an to the . /// - /// The builder. + /// The logging builder. /// The Xunit output helper that logs are written to. /// The logging options. If not provided the default options are used. /// The input builder. @@ -34,7 +34,7 @@ public static ILoggingBuilder AddXunit(this ILoggingBuilder builder, ITestOutput /// /// Add an to the . /// - /// The builder. + /// The logging builder. /// The Xunit output helper that logs are written to. /// A delegate to configure the . /// The input builder. @@ -45,4 +45,44 @@ public static ILoggingBuilder AddXunit(this ILoggingBuilder builder, ITestOutput return builder.AddXunit(outputHelper, options); } + + /// + /// Add an to the . + /// + /// The logger factory. + /// The Xunit output helper that logs are written to. + /// The logging options. If not provided the default options are used. + /// + /// + public static ILoggerFactory AddXunit(this ILoggerFactory factory, ITestOutputHelper outputHelper, XunitLoggerOptions? options = null) + { + if (factory == null) + { + throw new ArgumentNullException(nameof(factory)); + } + + if (outputHelper == null) + { + throw new ArgumentNullException(nameof(outputHelper)); + } + + factory.AddProvider(new XunitLoggerProvider(outputHelper, options ?? new XunitLoggerOptions())); + + return factory; + } + + /// + /// Add an to the . + /// + /// The logger factory. + /// The Xunit output helper that logs are written to. + /// A delegate to configure the . + /// + public static ILoggerFactory AddXunit(this ILoggerFactory factory, ITestOutputHelper outputHelper, Action? configure) + { + var options = new XunitLoggerOptions(); + configure?.Invoke(options); + + return factory.AddXunit(outputHelper, options); + } } \ No newline at end of file diff --git a/test/Logging.Xunit.UnitTests/Logging.Xunit.UnitTests.csproj b/test/Logging.Xunit.UnitTests/Logging.Xunit.UnitTests.csproj index 6afce00..aa6243b 100644 --- a/test/Logging.Xunit.UnitTests/Logging.Xunit.UnitTests.csproj +++ b/test/Logging.Xunit.UnitTests/Logging.Xunit.UnitTests.csproj @@ -1,19 +1,17 @@ - net7.0 enable enable - false - 11 + net7.0;net472 - - + + diff --git a/test/Logging.Xunit.UnitTests/XunitLoggerTests.cs b/test/Logging.Xunit.UnitTests/XunitLoggerTests.cs index c2a1552..68be6d2 100644 --- a/test/Logging.Xunit.UnitTests/XunitLoggerTests.cs +++ b/test/Logging.Xunit.UnitTests/XunitLoggerTests.cs @@ -210,7 +210,12 @@ private IEnumerable LogWrites() private static ILogger CreateLogger(ITestOutputHelper outputHelper, Action? configure = null) { +#if NET472 + using var loggerFactory = new LoggerFactory(); + loggerFactory.AddXunit(outputHelper, configure); +#else using var loggerFactory = LoggerFactory.Create(builder => builder.AddXunit(outputHelper, configure)); +#endif return loggerFactory.CreateLogger(); }