Skip to content

Commit

Permalink
README and example usage
Browse files Browse the repository at this point in the history
  • Loading branch information
devodo committed Apr 21, 2023
1 parent 76092b3 commit 9972868
Show file tree
Hide file tree
Showing 17 changed files with 247 additions and 89 deletions.
21 changes: 15 additions & 6 deletions Logging.Xunit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{11D6E6C7-9
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Logging.Xunit.UnitTests", "test\Logging.Xunit.UnitTests\Logging.Xunit.UnitTests.csproj", "{BF9A6FE2-1F13-4062-95CC-6C1683821425}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "src\ConsoleApp1\ConsoleApp1.csproj", "{FB3991C8-21D0-48E0-A68E-F36F4914E201}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{E67C3372-D051-41E6-8D18-0ACEDE23CDC2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Logging.Xunit.ExampleWebApp", "examples\Logging.Xunit.ExampleWebApp\Logging.Xunit.ExampleWebApp.csproj", "{8A552951-32C3-433A-B27F-D2B940D9CC14}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Logging.Xunit.ExampleTests", "examples\Logging.Xunit.ExampleTests\Logging.Xunit.ExampleTests.csproj", "{7EA36BCB-C3C0-4D7C-A020-F0627B6D1AE3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -18,7 +22,8 @@ Global
GlobalSection(NestedProjects) = preSolution
{CC7FBBE3-9FCA-4B1D-B19F-A01BC1C748A5} = {6D722543-2764-408F-9288-2ADB6F564042}
{BF9A6FE2-1F13-4062-95CC-6C1683821425} = {11D6E6C7-999F-43CB-A23F-5D2141CD6E58}
{FB3991C8-21D0-48E0-A68E-F36F4914E201} = {6D722543-2764-408F-9288-2ADB6F564042}
{8A552951-32C3-433A-B27F-D2B940D9CC14} = {E67C3372-D051-41E6-8D18-0ACEDE23CDC2}
{7EA36BCB-C3C0-4D7C-A020-F0627B6D1AE3} = {E67C3372-D051-41E6-8D18-0ACEDE23CDC2}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CC7FBBE3-9FCA-4B1D-B19F-A01BC1C748A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand All @@ -29,9 +34,13 @@ Global
{BF9A6FE2-1F13-4062-95CC-6C1683821425}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF9A6FE2-1F13-4062-95CC-6C1683821425}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF9A6FE2-1F13-4062-95CC-6C1683821425}.Release|Any CPU.Build.0 = Release|Any CPU
{FB3991C8-21D0-48E0-A68E-F36F4914E201}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB3991C8-21D0-48E0-A68E-F36F4914E201}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB3991C8-21D0-48E0-A68E-F36F4914E201}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB3991C8-21D0-48E0-A68E-F36F4914E201}.Release|Any CPU.Build.0 = Release|Any CPU
{8A552951-32C3-433A-B27F-D2B940D9CC14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8A552951-32C3-433A-B27F-D2B940D9CC14}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A552951-32C3-433A-B27F-D2B940D9CC14}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A552951-32C3-433A-B27F-D2B940D9CC14}.Release|Any CPU.Build.0 = Release|Any CPU
{7EA36BCB-C3C0-4D7C-A020-F0627B6D1AE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EA36BCB-C3C0-4D7C-A020-F0627B6D1AE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EA36BCB-C3C0-4D7C-A020-F0627B6D1AE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EA36BCB-C3C0-4D7C-A020-F0627B6D1AE3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
79 changes: 77 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,77 @@
# Logging.Xunit
Microsoft.Extensions.Logging adapter for xUnit test output
# Devodo.Logging.Xunit

[![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

# Installing

Install `Devodo.Logging.Xunit` as a [NuGet package](https://www.nuget.org/packages/Devodo.Logging.Xunit):

```sh
Install-Package Devodo.Logging.Xunit
```

Or via the .NET command line interface:

```sh
dotnet add package Devodo.Logging.Xunit
```

# Usage

Given a simple ASP.NET Minimal API as follows:

```csharp
var app = WebApplication.Create(args);

app.MapGet("/echo/{message}", (string message, ILogger<Program> logger) =>
{
logger.LogInformation("Handling echo request with input: {Message}", message);

return message;
});

app.Run();
```

Log output can be captured in Xunit tests as follows:

```csharp
public class ExampleTests
{
private readonly ITestOutputHelper _outputHelper;

public ExampleTests(ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;
}

[Fact]
public async Task GivenHttpClient_WhenGetRoot_ThenRespondsHello()
{
// ARRANGE
var httpClient = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
{
builder.ConfigureLogging(logging =>
{
// Add an xUnit logging provider that writes to the ITestOutputHelper
logging.AddXunit(_outputHelper);
});
}).CreateClient();

// ACT
var response = await httpClient.GetAsync("/echo/hello xunit logging");

// ASSERT
Assert.Equal("hello xunit logging", await response.Content.ReadAsStringAsync());
}
}
```

The test above outputs the following:
```
info: Program[0]
Handling echo request with input: hello xunit logging
```
35 changes: 35 additions & 0 deletions examples/Logging.Xunit.ExampleTests/ExampleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit.Abstractions;

namespace Logging.Xunit.ExampleTests;

public class ExampleTests
{
private readonly ITestOutputHelper _outputHelper;

public ExampleTests(ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;
}

[Fact]
public async Task GivenHttpClient_WhenGetRoot_ThenRespondsHello()
{
// ARRANGE
var httpClient = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
{
builder.ConfigureLogging(logging =>
{
// Add an xUnit logging provider that writes to the ITestOutputHelper
logging.AddXunit(_outputHelper);
});
}).CreateClient();

// ACT
var response = await httpClient.GetAsync("/echo/hello xunit logging");

// ASSERT
Assert.Equal("hello xunit logging", await response.Content.ReadAsStringAsync());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Logging.Xunit\Logging.Xunit.csproj" />
<ProjectReference Include="..\Logging.Xunit.ExampleWebApp\Logging.Xunit.ExampleWebApp.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions examples/Logging.Xunit.ExampleTests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="Logging.Xunit.ExampleTests" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions examples/Logging.Xunit.ExampleWebApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var app = WebApplication.Create(args);

app.MapGet("/echo/{message}", (string message, ILogger<Program> logger) =>
{
logger.LogInformation("Handling echo request with input: {Message}", message);

return message;
});

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:28128",
"sslPort": 44397
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5201",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7139;http://localhost:5201",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions examples/Logging.Xunit.ExampleWebApp/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
15 changes: 0 additions & 15 deletions src/ConsoleApp1/ConsoleApp1.csproj

This file was deleted.

25 changes: 0 additions & 25 deletions src/ConsoleApp1/Program.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Logging.Xunit/IXunitFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Logging.Xunit;
/// <summary>
/// Interface for custom log messages formatting.
/// </summary>
public interface IXunitFormatter
internal interface IXunitFormatter
{
/// <summary>
/// Writes the log message to the specified TextWriter.
Expand Down
3 changes: 2 additions & 1 deletion src/Logging.Xunit/LogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

// Copied from: https://raw.githubusercontent.com/dotnet/runtime/release/7.0/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogEntry.cs
// Copying to avoid requiring a dependency on a later version of Microsoft.Extensions.Logging.Abstractions as the LogEntry type was only introduced in v5.0.0.

using System;
using Microsoft.Extensions.Logging;
Expand All @@ -11,7 +12,7 @@ namespace Logging.Xunit
/// <summary>
/// Holds the information for a single log entry.
/// </summary>
public readonly struct LogEntry<TState>
internal readonly struct LogEntry<TState>
{
/// <summary>
/// Initializes an instance of the LogEntry struct.
Expand Down
2 changes: 1 addition & 1 deletion src/Logging.Xunit/XunitLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Logging.Xunit
///
/// Based on: https://github.com/dotnet/runtime/blob/release/7.0/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs
/// </summary>
public class XunitLogger : ILogger
internal sealed class XunitLogger : ILogger
{
private readonly string _name;
private readonly ITestOutputHelper _testOutputHelper;
Expand Down
Loading

0 comments on commit 9972868

Please sign in to comment.