[英]How to log with Nlog in SpecFlow?
SpecFlow writes its output into Console like this: SpecFlow 将其输出写入控制台,如下所示:
Given the "TestOperator" user is logged in
-> done: WebUserSteps.GivenTheUserIsLoggedIn("TestOperator", "") (9.5s)
How can we make it use NLog to configure where it should write?我们如何让它使用 NLog 来配置它应该写在哪里?
With this:有了这个:
public class CustomListener : ITraceListener
{
private Logger Log = LogManager.GetLogger("SPECFLOW");
public void WriteTestOutput(string message)
{
Log.Trace(message);
}
public void WriteToolOutput(string message)
{
Log.Trace(message);
}
}
And和
[Binding]
public class ScenarioContextInitializer
{
private readonly IObjectContainer _container;
public ScenarioContextInitializer(ScenarioContext scenarioContext)
{
_container = (scenarioContext ?? throw new ArgumentNullException(nameof(scenarioContext))).ScenarioContainer;
}
[Before]
protected void Load()
{
_container.RegisterTypeAs<CustomListener, ITraceListener>();
}
}
It didn't work.它没有用。 I know there is ability to add plugins but that seems too much overhead.
我知道可以添加插件,但这似乎开销太大。
Also we use ObjectivityLtd Test.Automation extensions.我们还使用ObjectivityLtd Test.Automation扩展。
It works via xunit tests generated by SpecFlow.xUnit它通过SpecFlow.xUnit生成的xunit测试工作
The issue is probably that NLog cannot find it's config.问题可能是 NLog 找不到它的配置。
When running unit tests, the dlls are moved and not the nlog.config运行单元测试时,会移动 dll 而不是 nlog.config
There are multiple solutions:有多种解决方案:
Load the config from a fixed path, eg从固定路径加载配置,例如
LogManager.Configuration = new XmlLoggingConfiguration("c:/mydir/nlog.config");
Or setup from code instead of config, eg:或者从代码而不是配置进行设置,例如:
var config = new LoggingConfiguration();
config.AddRuleForAllLevels(new FileTarget()
{
FileName = "c:/temp/logfile.log"
});
LogManager.Configuration = config; //apply config
If that is still an issue, check the internal log如果这仍然是一个问题,请检查内部日志
I've done similar thing in my framework with this workaround:我用这个解决方法在我的框架中做了类似的事情:
Here is the example: https://github.com/LirazShay/SpecFlowDemo/blob/master/src/SpecFlowDemo/Hooks.cs这是示例: https : //github.com/LirazShay/SpecFlowDemo/blob/master/src/SpecFlowDemo/Hooks.cs
Something like this:像这样的东西:
[AfterStep]
public void LogStepResult()
{
string stepText = StepContext.StepInfo.StepDefinitionType + " " + StepContext.StepInfo.Text;
Console.WriteLine(stepText);
var stepTable = StepContext.StepInfo.Table;
if (stepTable != null && stepTable.ToString() != "") Console.WriteLine(stepTable);
var error = ScenarioContext.TestError;
Console.WriteLine(error != null ? "-> error: " + error.Message : "-> done.");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.