-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor refactors and additional tests.
Added logging output wrappers as utilities.
- Loading branch information
1 parent
89a6886
commit c602413
Showing
15 changed files
with
388 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/C3D/Extensions/Logging/Xunit/Utilities/LoggingMessageSink.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Collections.Immutable; | ||
using Xunit.Abstractions; | ||
|
||
namespace C3D.Extensions.Logging.Xunit.Utilities; | ||
|
||
public class LoggingMessageSink : IMessageSink | ||
{ | ||
private readonly List<IMessageSinkMessage> messages = new(); | ||
private readonly IMessageSink output; | ||
|
||
public LoggingMessageSink(IMessageSink output) => this.output = output; | ||
|
||
#if NET6_0_OR_GREATER | ||
public IReadOnlyList<IMessageSinkMessage> Messages => messages.ToImmutableList(); | ||
#else | ||
public IReadOnlyList<IMessageSinkMessage> Messages => ImmutableList<IMessageSinkMessage>.Empty.AddRange(messages); | ||
#endif | ||
|
||
public bool OnMessage(IMessageSinkMessage message) | ||
{ | ||
messages.Add(message); | ||
return output.OnMessage(message); | ||
} | ||
|
||
public void Clear() => messages.Clear(); | ||
} | ||
|
32 changes: 32 additions & 0 deletions
32
src/C3D/Extensions/Logging/Xunit/Utilities/LoggingTestOutputHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Collections.Immutable; | ||
using Xunit.Abstractions; | ||
|
||
namespace C3D.Extensions.Logging.Xunit.Utilities; | ||
|
||
public class LoggingTestOutputHelper : ITestOutputHelper | ||
{ | ||
private readonly ITestOutputHelper output; | ||
private readonly List<string> messages = new(); | ||
|
||
#if NET6_0_OR_GREATER | ||
public IReadOnlyList<string> Messages => messages.ToImmutableList(); | ||
#else | ||
public IReadOnlyList<string> Messages => ImmutableList<string>.Empty.AddRange(messages); | ||
#endif | ||
|
||
public void ClearMessages() { messages.Clear(); } | ||
|
||
public LoggingTestOutputHelper(ITestOutputHelper output) => this.output = output; | ||
|
||
public void WriteLine(string message) | ||
{ | ||
output.WriteLine(message); | ||
messages.Add(message); | ||
} | ||
|
||
public void WriteLine(string format, params object[] args) | ||
{ | ||
output.WriteLine(format, args); | ||
messages.Add(string.Format(format, args)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
namespace C3D.Extensions.Logging.Xunit.Test; | ||
|
||
public class FixtureTests : IClassFixture<LoggingTestFixture> | ||
{ | ||
private readonly LoggingTestFixture fixture; | ||
private readonly ITestOutputHelper output; | ||
|
||
private void WriteFunctionName([CallerMemberName] string? caller = null) => output.WriteLine(caller); | ||
|
||
public FixtureTests(LoggingTestFixture fixture, ITestOutputHelper output) | ||
{ | ||
this.fixture = fixture; | ||
this.output = output; | ||
} | ||
|
||
[Fact] | ||
public void CreateLoggerFromServices() | ||
{ | ||
WriteFunctionName(); | ||
|
||
fixture.ClearMessages(); | ||
|
||
var services = fixture.Services; | ||
|
||
var log = services.GetRequiredService<ILogger<FixtureTests>>(); | ||
|
||
Assert.NotNull(log); | ||
|
||
const string debug = "Here is some debugging"; | ||
const string info = "Here is some information"; | ||
|
||
log.LogDebug(debug); | ||
log.LogInformation(info); | ||
|
||
var messages = fixture.Messages; | ||
|
||
Assert.NotNull(messages); | ||
|
||
Assert.Equal(2, messages.Count); | ||
|
||
var diagnosticMessages = messages.OfType<DiagnosticMessage>(); | ||
|
||
Assert.Collection(diagnosticMessages, s => s.Message.EndsWith(debug), s => s.Message.EndsWith(info)); | ||
} | ||
|
||
[Fact] | ||
public void CreateLoggerFromFactory() | ||
{ | ||
WriteFunctionName(); | ||
|
||
fixture.ClearMessages(); | ||
|
||
var factory = fixture.LoggerFactory; | ||
|
||
var log = factory.CreateLogger<TestOutputHelperTests>(); | ||
|
||
Assert.NotNull(log); | ||
|
||
const string debug = "Here is some debugging"; | ||
const string info = "Here is some information"; | ||
|
||
log.LogDebug(debug); | ||
log.LogInformation(info); | ||
|
||
var messages = fixture.Messages; | ||
|
||
var diagnosticMessages = messages.OfType<DiagnosticMessage>(); | ||
|
||
Assert.Collection(diagnosticMessages, s => s.Message.EndsWith(debug), s => s.Message.EndsWith(info)); | ||
} | ||
} |
56 changes: 49 additions & 7 deletions
56
test/C3D/Extensions/Logging/Xunit/Test/LoggingTestFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit.Abstractions; | ||
using C3D.Extensions.Logging.Xunit.Utilities; | ||
|
||
namespace C3D.Extensions.Logging.Xunit.Net6 | ||
namespace C3D.Extensions.Logging.Xunit.Test; | ||
|
||
public partial class LoggingTestFixture | ||
{ | ||
internal class LoggingTestFixture | ||
private readonly LoggingMessageSink output; | ||
public LoggingTestFixture(IMessageSink output) => this.output = new LoggingMessageSink(output); | ||
|
||
|
||
public IReadOnlyCollection<IMessageSinkMessage> Messages => output.Messages; | ||
public void ClearMessages() => output.Clear(); | ||
|
||
private static IConfiguration CreateConfiguration() | ||
{ | ||
var builder = new ConfigurationBuilder(); | ||
builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); | ||
return builder.Build(); | ||
|
||
} | ||
|
||
private ILoggingBuilder ConfigureLogger(ILoggingBuilder builder, IConfiguration configuration) => builder | ||
.ClearProviders() | ||
.SetMinimumLevel(LogLevel.Debug) | ||
.AddConfiguration(configuration.GetSection("Logging")) | ||
.AddDebug(); | ||
|
||
private IServiceCollection CreateServices() | ||
{ | ||
var configuration = CreateConfiguration(); | ||
|
||
return new ServiceCollection() | ||
.AddSingleton(configuration) | ||
.AddSingleton<IMessageSink>(output) | ||
.AddTransient(_ => output.Messages) | ||
.AddLogging(logging => ConfigureLogger(logging, configuration).AddXunit()); | ||
} | ||
|
||
private IServiceProvider? services; | ||
public IServiceProvider Services => services ??= CreateServices().BuildServiceProvider(); | ||
|
||
private ILoggerFactory CreateLoggerFactory() | ||
{ | ||
var configuration = CreateConfiguration(); | ||
return Microsoft.Extensions.Logging.LoggerFactory.Create(logging => ConfigureLogger(logging, configuration).AddXunit(output)); | ||
} | ||
|
||
private ILoggerFactory? loggerFactory; | ||
public ILoggerFactory LoggerFactory => loggerFactory ??= CreateLoggerFactory(); | ||
} |
Oops, something went wrong.