Skip to content

Commit

Permalink
More xunit logging changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Feb 14, 2024
1 parent c55ecc9 commit 935d17b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 22 deletions.
18 changes: 17 additions & 1 deletion src/Foundatio.Xunit/Logging/LoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,23 @@ public static ILoggingBuilder AddTestLogger(this ILoggingBuilder builder, ITestO
var options = new TestLoggerOptions {
WriteLogEntryFunc = logEntry =>
{
outputHelper.WriteLine(logEntry.ToString(false));
outputHelper?.WriteLine(logEntry.ToString(false));
}
};

configure?.Invoke(options);

return builder.AddTestLogger(options);
}

public static ILoggingBuilder AddTestLogger(this ILoggingBuilder builder, Func<ITestOutputHelper> getOutputHelper,
Action<TestLoggerOptions> configure = null)
{

var options = new TestLoggerOptions {
WriteLogEntryFunc = logEntry =>
{
getOutputHelper?.Invoke()?.WriteLine(logEntry.ToString(false));
}
};

Expand Down
3 changes: 2 additions & 1 deletion src/Foundatio.Xunit/Logging/TestLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ public int MaxLogEntriesToWrite
public IReadOnlyList<LogEntry> LogEntries => _logEntries.ToArray();


public void Clear()
public void Reset()
{
lock (_logEntries)
{
_logEntries.Clear();
_logLevels.Clear();
Interlocked.Exchange(ref _logEntriesWritten, 0);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Foundatio.Xunit/Logging/TestLoggerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ protected TestLoggerBase(ITestOutputHelper output, TestLoggerFixture fixture)
{
Fixture = fixture;
fixture.Output = output;
fixture.AddServiceRegistrations(RegisterServices);
fixture.ConfigureServices(ConfigureServices);
}

protected TestLoggerFixture Fixture { get; }
protected IServiceProvider Services => Fixture.Services;
protected TestLogger TestLogger => Fixture.TestLogger;
protected ILogger Log => Fixture.Log;

protected virtual void RegisterServices(IServiceCollection services)
protected virtual void ConfigureServices(IServiceCollection services)
{
}

Expand All @@ -32,7 +32,7 @@ public virtual Task InitializeAsync()

public virtual Task DisposeAsync()
{
Fixture.TestLogger.Clear();
Fixture.TestLogger.Reset();
return Task.CompletedTask;
}
}
30 changes: 17 additions & 13 deletions src/Foundatio.Xunit/Logging/TestLoggerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Foundatio.Xunit;

public class TestLoggerFixture : IAsyncLifetime
{
private readonly List<IDisposable> _disposables = [];
private readonly List<object> _disposables = [];
private readonly List<Action<IServiceCollection>> _serviceRegistrations = [];
private readonly Lazy<IServiceProvider> _serviceProvider;
private readonly Lazy<TestLogger> _testLogger;
Expand All @@ -25,7 +25,7 @@ public TestLoggerFixture()

public ITestOutputHelper Output { get; set; }

public void AddServiceRegistrations(Action<IServiceCollection> registerServices)
public void ConfigureServices(Action<IServiceCollection> registerServices)
{
_serviceRegistrations.Add(registerServices);
}
Expand All @@ -34,20 +34,17 @@ public void AddServiceRegistrations(Action<IServiceCollection> registerServices)
public TestLogger TestLogger => _testLogger.Value;
public ILogger Log => _log.Value;

protected virtual void RegisterServices(IServiceCollection services)
protected virtual void ConfigureServices(IServiceCollection services)
{
if (Output == null)
throw new InvalidOperationException("Output should be set before registering services.");

services.AddLogging(c => c.AddTestLogger(Output));
services.AddLogging(c => c.AddTestLogger(() => Output));
foreach (var registration in _serviceRegistrations)
registration(services);
}

protected virtual IServiceProvider BuildServiceProvider()
{
var services = new ServiceCollection();
RegisterServices(services);
ConfigureServices(services);
var sp = services.BuildServiceProvider();
_disposables.Add(sp);
return sp;
Expand All @@ -58,20 +55,27 @@ public virtual Task InitializeAsync()
return Task.CompletedTask;
}

public virtual Task DisposeAsync()
public virtual async Task DisposeAsync()
{
foreach (var disposable in _disposables)
foreach (object disposable in _disposables)
{
try
{
disposable.Dispose();
switch (disposable)
{
case IAsyncDisposable asyncDisposable:
await asyncDisposable.DisposeAsync();
break;
case IDisposable syncDisposable:
syncDisposable.Dispose();
break;
}
}
catch (ObjectDisposedException) {}
catch (Exception ex)
{
Log?.LogError(ex, "Error disposing resource.");
}
}

return Task.CompletedTask;
}
}
10 changes: 10 additions & 0 deletions src/Foundatio.Xunit/Logging/TestLoggerOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Foundatio.Utility;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;

namespace Foundatio.Xunit;

Expand All @@ -11,6 +12,15 @@ public class TestLoggerOptions
public int MaxLogEntriesToWrite { get; set; } = 1000;
public bool IncludeScopes { get; set; } = true;

public void UseOutputHelper(Func<ITestOutputHelper> getOutputHelper, Func<LogEntry, string> formatLogEntry = null)
{
formatLogEntry ??= logEntry => logEntry.ToString(false);
WriteLogEntryFunc = logEntry =>
{
getOutputHelper?.Invoke()?.WriteLine(formatLogEntry(logEntry));
};
}

public Action<LogEntry> WriteLogEntryFunc { get; set; }
internal void WriteLogEntry(LogEntry logEntry) => WriteLogEntryFunc?.Invoke(logEntry);

Expand Down
8 changes: 4 additions & 4 deletions tests/Foundatio.Tests/Utility/TestLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TestLoggerTests : TestLoggerBase
public TestLoggerTests(ITestOutputHelper output, TestLoggerFixture fixture) : base(output, fixture)
{
_output = output;
fixture.AddServiceRegistrations(s => s.AddSingleton<SomeClass>());
fixture.ConfigureServices(s => s.AddSingleton<SomeClass>());
}

[Fact]
Expand All @@ -35,7 +35,7 @@ public void CanUseTestLogger()
Assert.Single(testLogger.LogEntries);
Assert.Contains("Doing something", testLogger.LogEntries[0].Message);

testLogger.Clear();
testLogger.Reset();
testLogger.SetLogLevel<SomeClass>(LogLevel.Error);

someClass.DoSomething(2);
Expand All @@ -56,7 +56,7 @@ public void CanUseTestLoggerFixture()
Assert.Equal(100, TestLogger.LogEntries.Count);
Assert.Contains("Hello 2", TestLogger.LogEntries.Last().Message);

Fixture.TestLogger.Clear();
Fixture.TestLogger.Reset();
TestLogger.SetLogLevel<SomeClass>(LogLevel.Error);

someClass.DoSomething(1002);
Expand All @@ -75,7 +75,7 @@ public void CanUseTestLoggerFixture2()
Assert.Single(TestLogger.LogEntries);
Assert.Contains("Doing something", TestLogger.LogEntries[0].Message);

TestLogger.Clear();
TestLogger.Reset();
TestLogger.SetLogLevel<SomeClass>(LogLevel.Error);

someClass.DoSomething(2);
Expand Down

0 comments on commit 935d17b

Please sign in to comment.