简体   繁体   English

如何在 SpecFlow 中使用 Nlog 进行登录?

[英]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

See wiki维基

If that is still an issue, check the internal log如果这仍然是一个问题,请检查内部日志

I've done similar thing in my framework with this workaround:我用这个解决方法在我的框架中做了类似的事情:

  • Add hook [AfterStep], that calls Console.WriteLine() [or your logger] with the name of the step + if passed or not (if test error != null, that means failed, ow passed)添加钩子 [AfterStep],它调用 Console.WriteLine() [或您的记录器] 与步骤的名称 + 如果通过或不通过(如果测试错误!= null,这意味着失败,通过)
    Please note that this works perfect in parallel execution.请注意,这在并行执行中非常有效。 (the correct output goes to each test) (正确的输出进入每个测试)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM