Skip to content

Commit a7962a5

Browse files
committed
first commit
1 parent a5b55c2 commit a7962a5

9 files changed

+196
-0
lines changed

XunitLoggerSolution.sln

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.3.32929.385
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xunit.Logging", "src\XunitLogger\Xunit.Logging.csproj", "{0FE5DA83-E1B1-40C8-AD5D-85D5C7B96E69}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XunitLoggerTest", "tests\XunitLoggerTest\XunitLoggerTest.csproj", "{E56A8E82-7E73-4721-B961-06DFBDF0F11D}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{0FE5DA83-E1B1-40C8-AD5D-85D5C7B96E69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{0FE5DA83-E1B1-40C8-AD5D-85D5C7B96E69}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{0FE5DA83-E1B1-40C8-AD5D-85D5C7B96E69}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{0FE5DA83-E1B1-40C8-AD5D-85D5C7B96E69}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{E56A8E82-7E73-4721-B961-06DFBDF0F11D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{E56A8E82-7E73-4721-B961-06DFBDF0F11D}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{E56A8E82-7E73-4721-B961-06DFBDF0F11D}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{E56A8E82-7E73-4721-B961-06DFBDF0F11D}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {01183500-65F0-452B-B46A-765464BBAFCF}
30+
EndGlobalSection
31+
EndGlobal

src/XunitLogger/Logger.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.Extensions.Logging;
2+
using System;
3+
using Xunit.Abstractions;
4+
5+
namespace Xunit.Logging
6+
{
7+
public class Logger : ILogger
8+
{
9+
private readonly ITestOutputHelper _testOutputHelper;
10+
private readonly string _name;
11+
12+
public Logger(ITestOutputHelper testOutputHelper, string name)
13+
{
14+
_testOutputHelper = testOutputHelper;
15+
_name = name;
16+
}
17+
18+
public IDisposable BeginScope<TState>(TState state) => default;
19+
public bool IsEnabled(LogLevel logLevel) => true;
20+
21+
public void Log<TState>(
22+
LogLevel logLevel,
23+
EventId eventId,
24+
TState state,
25+
Exception exception,
26+
Func<TState, Exception, string> formatter)
27+
{
28+
_testOutputHelper.WriteLine($"[{eventId.Id,2}: {logLevel,-12}]");
29+
30+
_testOutputHelper.WriteLine($" {_name} - {formatter(state, exception)}");
31+
if (exception != null)
32+
_testOutputHelper.WriteLine(exception.ToString());
33+
}
34+
}
35+
}

src/XunitLogger/LoggerFactory.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
namespace Xunit.Logging
4+
{
5+
public class LoggerFactory : ILoggerFactory
6+
{
7+
ILoggerProvider _provider = null;
8+
public void AddProvider(ILoggerProvider provider)
9+
{
10+
_provider = provider;
11+
}
12+
13+
public ILogger CreateLogger(string categoryName)
14+
{
15+
return _provider.CreateLogger(categoryName);
16+
}
17+
18+
public void Dispose()
19+
{
20+
_provider?.Dispose();
21+
}
22+
}
23+
}

src/XunitLogger/LoggerProvider.cs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.Extensions.Logging;
2+
using System;
3+
using System.Collections.Concurrent;
4+
using Xunit.Abstractions;
5+
6+
namespace Xunit.Logging
7+
{
8+
public class LoggerProvider : ILoggerProvider
9+
{
10+
private readonly ITestOutputHelper _testOutputHelper;
11+
12+
private readonly ConcurrentDictionary<string, Logger> _loggers = new ConcurrentDictionary<string, Logger>(StringComparer.OrdinalIgnoreCase);
13+
14+
public LoggerProvider()
15+
{
16+
}
17+
18+
public LoggerProvider(ITestOutputHelper testOutputHelper)
19+
{
20+
_testOutputHelper = testOutputHelper;
21+
}
22+
23+
public ILogger CreateLogger(string categoryName) => _loggers.GetOrAdd(categoryName, name => new Logger(_testOutputHelper, name));
24+
25+
public void Dispose()
26+
{
27+
_loggers.Clear();
28+
}
29+
}
30+
}

src/XunitLogger/Xunit.Logging.csproj

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFrameworks>netcoreapp3.1;net5.0*;net6.0*</TargetFrameworks>
4+
<Title>xUnit Logger</Title>
5+
<RepositoryUrl>https://github.com/Franchef/XunitLogger</RepositoryUrl>
6+
<PackageTags>xUnit; Logger; xUnit log;</PackageTags>
7+
<AssemblyVersion>1.0.0</AssemblyVersion>
8+
<FileVersion>1.0.0</FileVersion>
9+
<Company>Franchef</Company>
10+
<Authors>$(Authors)</Authors>
11+
<Description>A logger for xUnit output for lazy people (like me)</Description>
12+
<PackageIcon>xunit.png</PackageIcon>
13+
</PropertyGroup>
14+
<ItemGroup>
15+
<None Include="xunit.png" Pack="true" PackagePath=""/>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.2" />
19+
<PackageReference Include="System.Buffers" Version="4.5.1" />
20+
<PackageReference Include="System.Memory" Version="4.5.5" />
21+
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
22+
</ItemGroup>
23+
</Project>

src/XunitLogger/xunit.png

7 KB
Loading

tests/XunitLoggerTest/LoggerTests.cs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.Extensions.Logging;
2+
using Xunit.Abstractions;
3+
using Xunit.Logging;
4+
5+
namespace XunitLoggerTest
6+
{
7+
public class LoggerTests
8+
{
9+
private readonly ITestOutputHelper output;
10+
11+
public LoggerTests(ITestOutputHelper output)
12+
{
13+
this.output = output;
14+
}
15+
16+
[Fact]
17+
public void LogMessage()
18+
{
19+
ILogger logger = new Logger(output, nameof(LoggerTests));
20+
21+
logger.LogTrace("Hello World");
22+
}
23+
}
24+
}

tests/XunitLoggerTest/Usings.cs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using Xunit;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.2" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
14+
<PackageReference Include="xunit" Version="2.4.2" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
<PackageReference Include="coverlet.collector" Version="3.2.0">
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\..\src\XunitLogger\Xunit.Logging.csproj" />
27+
</ItemGroup>
28+
29+
</Project>

0 commit comments

Comments
 (0)