Skip to content

Commit

Permalink
test: simplify request logging
Browse files Browse the repository at this point in the history
  • Loading branch information
skwasjer committed Oct 2, 2024
1 parent 86f820e commit f5641d4
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 278 deletions.
230 changes: 0 additions & 230 deletions test/MockHttp.Server.Tests/Fixtures/CapturingLoggerFactoryFixture.cs

This file was deleted.

53 changes: 53 additions & 0 deletions test/MockHttp.Server.Tests/Fixtures/FakeLogRecordSerialization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Text;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;

namespace MockHttp.Fixtures;

internal static class FakeLogRecordSerialization
{
internal static string Serialize(FakeLogRecord e)
{
var sb = new StringBuilder();
const int len = 4;
string indent = new(' ', len + 2);

sb.AppendLine($"{GetLogLevelString(e.Level)}: {e.Category}[{e.Id.Id}]");
foreach (IEnumerable<KeyValuePair<string, object?>> scope in e.Scopes.OfType<IEnumerable<KeyValuePair<string, object?>>>())
{
sb.Append(indent);
// ReSharper disable once UsageOfDefaultStructEquality
foreach (KeyValuePair<string, object?> kvp in scope)
{
sb.Append($"=> {kvp} ");
}

sb.AppendLine();
}

sb.Append(indent);
sb.AppendLine(e.Message);

if (e.Exception is not null)
{
sb.Append(indent);
sb.AppendLine(e.Exception.ToString());
}

return sb.ToString();
}

private static string GetLogLevelString(LogLevel logLevel)
{
return logLevel switch
{
LogLevel.Trace => "trce",
LogLevel.Debug => "dbug",
LogLevel.Information => "info",
LogLevel.Warning => "warn",
LogLevel.Error => "fail",
LogLevel.Critical => "crit",
_ => throw new ArgumentOutOfRangeException(nameof(logLevel))
};
}
}
46 changes: 20 additions & 26 deletions test/MockHttp.Server.Tests/Fixtures/LoggerFactoryFixture.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,44 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;

namespace MockHttp.Fixtures;

public abstract class LoggerFactoryFixture : IAsyncLifetime, IAsyncDisposable
internal sealed class LoggerFactoryFixture : IAsyncDisposable, IDisposable
{
private readonly ServiceProvider _services;

protected LoggerFactoryFixture(Action<ILoggingBuilder>? configure = null)
public LoggerFactoryFixture(Action<ILoggingBuilder>? configure = null)
{
_services = new ServiceCollection()
.AddLogging(builder =>
{
builder.SetMinimumLevel(LogLevel.Trace);
builder
.AddDebug()
.AddSimpleConsole(opts => opts.IncludeScopes = true)
;
configure?.Invoke(builder);
})
.AddLogging(
builder =>
{
builder.SetMinimumLevel(LogLevel.Trace);
builder
.AddFakeLogging()
.AddDebug();
configure?.Invoke(builder);
}
)
.BuildServiceProvider();

Factory = _services.GetRequiredService<ILoggerFactory>();
FakeLogCollector = _services.GetRequiredService<FakeLogCollector>();
}

public ILoggerFactory Factory { get; }

public async ValueTask DisposeAsync()
{
await DisposeAsyncCore();
GC.SuppressFinalize(this);
}
public FakeLogCollector FakeLogCollector { get; }

Task IAsyncLifetime.InitializeAsync()
public void Dispose()
{
return Task.CompletedTask;
_services.Dispose();
}

Task IAsyncLifetime.DisposeAsync()
{
return DisposeAsync().AsTask();
}

protected virtual async ValueTask DisposeAsyncCore()
public async ValueTask DisposeAsync()
{
await _services.DisposeAsync();
}
Expand Down
Loading

0 comments on commit f5641d4

Please sign in to comment.