我已阅读http://xunit.github.io/docs/capturing-output.html,它似乎适用于在测试运行期间制作我的测试输出特定消息,但我真的希望能够捕获已集成到我的类中的log4net输出测试
过去我已经设置了log4net来使用TraceLogger,测试框架能够将输出与测试相关联。 (不同的测试框架)。我怎么能以某种方式将log4net输出关联到Xunit IOutputHelper?
答案 0 :(得分:3)
这是我提出的答案
这是我可以让我的测试类继承自的类:
public class LogOutputTester:IDisposable
{
private readonly IAppenderAttachable _attachable;
private TestOutputAppender _appender;
protected LogOutputTester(ITestOutputHelper output)
{
log4net.Config.XmlConfigurator.Configure();
var root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;
_attachable = root;
_appender = new TestOutputAppender(output);
if (_attachable != null)
_attachable.AddAppender(_appender);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
_attachable.RemoveAppender(_appender);
}
}
这是我在上面的帮助器中引用的自定义Appender:
public class TestOutputAppender : AppenderSkeleton
{
private readonly ITestOutputHelper _xunitTestOutputHelper;
public TestOutputAppender(ITestOutputHelper xunitTestOutputHelper)
{
_xunitTestOutputHelper = xunitTestOutputHelper;
Name = "TestOutputAppender";
Layout = new PatternLayout("%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n");
}
protected override void Append(LoggingEvent loggingEvent)
{
_xunitTestOutputHelper.WriteLine(RenderLoggingEvent(loggingEvent));
}
}
这可以更多地定制,以采取自定义布局或其他...
最后 - 我只是让我的测试类继承自这个帮助器:
public class MyTestClass:LogOutputTester
{
public EdgeClientTests(ITestOutputHelper output):base(output)
{
}
...
您也可以让测试直接访问输出对象......
答案 1 :(得分:1)
根据此处描述的解决方案how to scrape a list of URLs,我使用log4net重新编写了它。 可能对某人有帮助。
public static class LogHelper
{
private static readonly Subject<LoggingEvent> LogEventSubject = new Subject<LoggingEvent>();
private const string CaptureCorrelationIdKey = "EventId";
private static readonly ILayout layout;
static LogHelper()
{
XmlConfigurator.Configure();
var root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;
IAppenderAttachable attachable = root;
var appender = new XunitAppender(logEvent => LogEventSubject.OnNext(logEvent));
attachable?.AddAppender(appender);
layout = appender.Layout;
appender.ActivateOptions();
}
public static IDisposable Capture(ITestOutputHelper testOutputHelper)
{
var captureId = Guid.NewGuid();
Func<LoggingEvent, bool> filter = logEvent =>
logEvent.GetProperties().Contains(CaptureCorrelationIdKey) &&
logEvent.LookupProperty(CaptureCorrelationIdKey).ToString() == captureId.ToString();
var subscription = LogEventSubject.Where(filter).Subscribe(logEvent =>
{
using (var writer = new StringWriter())
{
layout.Format(writer, logEvent);
testOutputHelper.WriteLine(writer.ToString());
}
});
ThreadContext.Properties[CaptureCorrelationIdKey] = captureId.ToString();
return new DisposableAction(() =>
{
subscription.Dispose();
ThreadContext.Properties.Clear();
});
}
private class DisposableAction : IDisposable
{
private readonly Action _action;
public DisposableAction(Action action)
{
_action = action;
}
public void Dispose()
{
_action();
}
}
public sealed class XunitAppender : AppenderSkeleton
{
private readonly Action<LoggingEvent> _action;
public XunitAppender(Action<LoggingEvent> action)
{
_action = action;
Name = "XunitAppender";
Layout = new PatternLayout("%date %-5level %logger - %message");
}
protected override void Append(LoggingEvent loggingEvent)
{
_action(loggingEvent);
}
}
}