Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions test/TestBuildingBlocks/LogOutputFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ public enum LogOutputFields
{
None = 0,
Level = 1,
Category = 1 << 1,
Message = 1 << 2,
Exception = 1 << 3,
Scopes = 1 << 4,
CategoryName = 1 << 1,
CategoryNamespace = 1 << 2,
Message = 1 << 3,
Exception = 1 << 4,
Scopes = 1 << 5,

Category = CategoryName | CategoryNamespace,
All = Level | Category | Message | Exception | Scopes
}
36 changes: 32 additions & 4 deletions test/TestBuildingBlocks/XUnitLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ namespace TestBuildingBlocks;
// Based on https://www.meziantou.net/how-to-get-asp-net-core-logs-in-the-output-of-xunit-tests.htm.
public sealed class XUnitLoggerProvider : ILoggerProvider
{
private const LogOutputFields DefaultLogOutputFields = LogOutputFields.All & ~LogOutputFields.CategoryNamespace;
private readonly ITestOutputHelper _testOutputHelper;
private readonly LogOutputFields _outputFields;
private readonly string? _categoryPrefixFilter;

public XUnitLoggerProvider(ITestOutputHelper testOutputHelper, string? categoryPrefixFilter, LogOutputFields outputFields = LogOutputFields.All)
public XUnitLoggerProvider(ITestOutputHelper testOutputHelper, string? categoryPrefixFilter, LogOutputFields outputFields = DefaultLogOutputFields)
{
ArgumentNullException.ThrowIfNull(testOutputHelper);

Expand Down Expand Up @@ -41,7 +42,34 @@ private sealed class XUnitLogger(ITestOutputHelper testOutputHelper, LogOutputFi
{
private readonly ITestOutputHelper _testOutputHelper = testOutputHelper;
private readonly LogOutputFields _outputFields = outputFields;
private readonly string _categoryName = categoryName;
private readonly string? _categoryText = GetCategoryText(categoryName, outputFields);

private static string? GetCategoryText(string categoryName, LogOutputFields outputFields)
{
if (outputFields.HasFlag(LogOutputFields.Category))
{
return categoryName;
}

bool hasName = outputFields.HasFlag(LogOutputFields.CategoryName);
bool hasNamespace = outputFields.HasFlag(LogOutputFields.CategoryNamespace);

if (hasName || hasNamespace)
{
// Microsoft.Extensions.Logging.LoggerFactory.CreateLogger(Type) removes generic type parameters
// and replaces '+' (nested class) with '.'.
int lastDotIndex = categoryName.LastIndexOf('.');

if (lastDotIndex == -1)
{
return hasName ? categoryName : string.Empty;
}

return hasName ? categoryName[(lastDotIndex + 1)..] : categoryName[..lastDotIndex];
}

return null;
}

public bool IsEnabled(LogLevel logLevel)
{
Expand All @@ -68,15 +96,15 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
builder.Append(logLevelString);
}

if (_outputFields.HasFlag(LogOutputFields.Category))
if (_categoryText != null)
{
if (builder.Length > 0)
{
builder.Append(' ');
}

builder.Append('[');
builder.Append(_categoryName);
builder.Append(_categoryText);
builder.Append(']');
}

Expand Down
Loading