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
11 changes: 10 additions & 1 deletion docs/docs/library/xunit-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ public class ThingamajigTests
_output.WriteLogs(logProvider.GetAllLogEntries())
}
}
```
```

#### Variants

* `ITestOutputHelper.WriteLogs(ITestCaptureLogger logger, Func<LogEntry, string>? formatter = null)`
* Available from v3.0
* Takes an `ITestCaptureLogger` and outputs all of the log messages captured by that logger.
* `ITestOutputHelper.WriteLogs(TestCaptureLoggerProvider provider, Func<LogEntry, string>? formatter = null)`
* Available from v3.0
* Takes an `TestCaptureLoggerProvider` and outputs all of the log messages captured by all the loggers in the provider.
3 changes: 3 additions & 0 deletions release-notes/wip-release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Date: ???
- #123: `TestCaptureLoggerProvider.CreateLogger<T>()`
- Potential breaking change: `TestCaptureLogger<T>` no longer inherits from `TestCaptureLogger`.
- Add `ITestCaptureLogger` and have `TestCaptureLogger` and `TestCaptureLogger<T>` be concrete implementations of the interface so you can reference the interface and not care which concrete implementation you have.
- #170: Additional xunit extension methods to write out all log messages to the `ITestOutputHelper`.
- `ITestOutputHelper.WriteLogs(ITestCaptureLogger...)`
- `ITestOutputHelper.WriteLogs(TestCaptureLoggerProvider...)`

### Miscellaneous

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void WriteLine(string format, params object[] args)
}

[Test]
public void RenderedLogMessagesGetSentToOutputTestHelper()
public void RenderedLogEntryMessagesSentToOutputTestHelper()
{
ResetLogSequence();
var outputHelper = new OutputHelper();
Expand All @@ -36,11 +36,47 @@ public void RenderedLogMessagesGetSentToOutputTestHelper()

var logs = logger.GetLogs();
outputHelper.WriteLogs(logs);


outputHelper.Messages[0].ShouldBe("[0 Information Stravaig.Extensions.Logging.Diagnostics.Tests.XUnit.OutputTestHelperExtensionTests] This is an information Message.");
outputHelper.Messages[1].ShouldBe("[1 Information Stravaig.Extensions.Logging.Diagnostics.Tests.XUnit.OutputTestHelperExtensionTests] This is a Warning Message.");
}

[Test]
public void RenderedLoggerSentToOutputTestHelper()
{
ResetLogSequence();
var outputHelper = new OutputHelper();
var logger = new TestCaptureLogger<OutputTestHelperExtensionTests>();
logger.LogInformation("This is an information Message.");
logger.LogInformation("This is a Warning Message.");

outputHelper.WriteLogs(logger);

outputHelper.Messages[0].ShouldBe("[0 Information Stravaig.Extensions.Logging.Diagnostics.Tests.XUnit.OutputTestHelperExtensionTests] This is an information Message.");
outputHelper.Messages[1].ShouldBe("[1 Information Stravaig.Extensions.Logging.Diagnostics.Tests.XUnit.OutputTestHelperExtensionTests] This is a Warning Message.");
}

[Test]
public void RenderedLoggerProviderSentToOutputTestHelper()
{
ResetLogSequence();
var outputHelper = new OutputHelper();
var loggerProvider = new TestCaptureLoggerProvider();
var logger1 = loggerProvider.CreateLogger("logger1");
var logger2 = loggerProvider.CreateLogger("logger2");
logger1.LogInformation("This is an information Message on the first logger.");
logger2.LogInformation("This is an information Message on the second logger.");
logger2.LogWarning("This is a Warning Message on the second logger.");
logger1.LogWarning("This is a Warning Message on the first logger.");

outputHelper.WriteLogs(loggerProvider);

outputHelper.Messages[0].ShouldBe("[0 Information logger1] This is an information Message on the first logger.");
outputHelper.Messages[1].ShouldBe("[1 Information logger2] This is an information Message on the second logger.");
outputHelper.Messages[2].ShouldBe("[2 Warning logger2] This is a Warning Message on the second logger.");
outputHelper.Messages[3].ShouldBe("[3 Warning logger1] This is a Warning Message on the first logger.");
}

private void ResetLogSequence()
{
var logEntryType = typeof(LogEntry);
Expand All @@ -49,4 +85,4 @@ private void ResetLogSequence()
throw new InvalidOperationException("Expected LogEntry to have a private field called _sequence.");
field.SetValue(null, 0);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Stravaig.Extensions.Logging.Diagnostics.Render;
using Xunit.Abstractions;

Expand All @@ -15,9 +16,27 @@ public static class OutputTestHelperExtensions
/// </summary>
/// <param name="output">The xunit test output helper.</param>
/// <param name="logEntries">The log entries.</param>
/// <param name="formatter"></param>
/// <param name="formatter">An optional formatter that defines how the log entry is to be written.</param>
public static void WriteLogs(this ITestOutputHelper output, IEnumerable<LogEntry> logEntries, Func<LogEntry, string>? formatter = null)
{
logEntries.RenderLogs(formatter ?? Formatter.SimpleBySequence, output.WriteLine);
}
}

/// <summary>
/// Write the logs to the xunit test output helper.
/// </summary>
/// <param name="output">The xunit test output helper.</param>
/// <param name="logger">The logger from which to output the captured logs.</param>
/// <param name="formatter">An optional formatter that defines how the log entry is to be written.</param>
public static void WriteLogs(this ITestOutputHelper output, ITestCaptureLogger logger, Func<LogEntry, string>? formatter = null)
=> output.WriteLogs(logger.GetLogs(), formatter);

/// <summary>
/// Write the logs to the xunit test output helper.
/// </summary>
/// <param name="output">The xunit test output helper.</param>
/// <param name="provider">The log provider from which to output the captured logs.</param>
/// <param name="formatter">An optional formatter that defines how the log entry is to be written.</param>
public static void WriteLogs(this ITestOutputHelper output, TestCaptureLoggerProvider provider, Func<LogEntry, string>? formatter = null)
=> output.WriteLogs(provider.GetAllLogEntries(), formatter);
}