I just started testing xUnit.net, but it doesn\'t seem to capture any output (Console, Debug, Trace), as I would have expected.
Is that possible? I am using a sample
I landed here with the same question. Here's what I ended up with. I hope it helps someone else.
How to write a custom target
///
/// Use this to output NLog information when running test cases.
///
[Target("XUnit")]
public class XUnitTarget : TargetWithLayout
{
[RequiredParameter] public ITestOutputHelper OutputHelper { get; set; }
protected override void Write(LogEventInfo logEvent)
{
var logMessage = Layout.Render(logEvent);
OutputHelper.WriteLine(logMessage);
}
///
/// Call this in the test where you wish to enable logging.
///
/// The xUnit output helper from the test.
public static void Configure(ITestOutputHelper testOutputHelper)
{
var config = new LoggingConfiguration();
var xUnitTarget = new XUnitTarget
{
OutputHelper = testOutputHelper
};
config.AddTarget("xUnit", xUnitTarget);
config.AddRule(LogLevel.Trace, LogLevel.Fatal, xUnitTarget);
LogManager.Configuration = config;
}
}