Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Log4net捕获异常,导致传入数据块报告_C#_Selenium_Selenium Webdriver_Log4net_Selenium Extent Report - Fatal编程技术网

C# Log4net捕获异常,导致传入数据块报告

C# Log4net捕获异常,导致传入数据块报告,c#,selenium,selenium-webdriver,log4net,selenium-extent-report,C#,Selenium,Selenium Webdriver,Log4net,Selenium Extent Report,我已经使用specflow/xunit为日志消息和数据块报告实现了log4net。 如果出现任何错误/异常,并且我没有log4net的try/catch语句,那么扩展报告将捕获异常并将其作为报告中的失败步骤打印出来。但是如果我有try和catch语句,并且log4net正在捕获异常,那么扩展报告不会将该步骤记录为失败的步骤,并使该步骤与已通过的步骤相同,但应该失败 如何使扩展报告认为log4net捕获了异常/错误,并且此步骤必须失败 下面是一个将失败的方法的try/catch语句 public

我已经使用specflow/xunit为日志消息和数据块报告实现了log4net。 如果出现任何错误/异常,并且我没有log4net的try/catch语句,那么扩展报告将捕获异常并将其作为报告中的失败步骤打印出来。但是如果我有try和catch语句,并且log4net正在捕获异常,那么扩展报告不会将该步骤记录为失败的步骤,并使该步骤与已通过的步骤相同,但应该失败

如何使扩展报告认为log4net捕获了异常/错误,并且此步骤必须失败

下面是一个将失败的方法的try/catch语句

public void ClickonLoginButton()
{
try{
      ClickonElement(LoginbuttonLocator);
      Log.info("successfully clicked");
}
catch(exception ex){
Log.error("unable to click");
Console.WriteLine(ex.StackTrace);
}
}
我的扩展数据块报告步骤后代码:

 [AfterStep]
        public void InsertReportingSteps()
        {

            var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();

            if (ScenarioContext.Current.TestError == null)
            {
                if (stepType == "Given")
                    _scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text);
                else if (stepType == "When")
                    _scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text);
                else if (stepType == "Then")
                    _scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text);
                else if (stepType == "And")
                    _scenario.CreateNode<And>(ScenarioStepContext.Current.StepInfo.Text);
            }
            else if (ScenarioContext.Current.TestError != null)
            {
                if (stepType == "Given")
                    _scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.InnerException);
                else if (stepType == "When")
                    _scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.InnerException);
                else if (stepType == "Then")
                {
                    string Runname = screenshot();
                    _scenario.AddScreenCaptureFromPath("C:\\Users\\xxxx- PC\\Desktop\\XUnit_Assignement_3\\XUnit_Assignement\\target\\ErrorScreenshots\\" 
                        + Runname + ".Png");
                    _scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).
                        Fail(ScenarioContext.Current.TestError.Message);
                    _scenario.Fail("Failed Because of Some issues",
                        MediaEntityBuilder.CreateScreenCaptureFromPath(@"C:\Users\xxxx- PC\Desktop\XUnit_Assignement_3\XUnit_Assignement\TestResults\image.JPG", 
                        "Failed").Build());
                }
            }
[AfterStep]
public void InsertReportingSteps()
{
var stepType=ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();
if(ScenarioContext.Current.TestError==null)
{
如果(步骤类型==“给定”)
_scenario.CreateNode(ScenarioStepContext.Current.StepInfo.Text);
else if(stepType==“When”)
_scenario.CreateNode(ScenarioStepContext.Current.StepInfo.Text);
else if(stepType==“Then”)
_scenario.CreateNode(ScenarioStepContext.Current.StepInfo.Text);
else if(stepType==“And”)
_scenario.CreateNode(ScenarioStepContext.Current.StepInfo.Text);
}
else if(ScenarioContext.Current.TestError!=null)
{
如果(步骤类型==“给定”)
_scenario.CreateNode(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.InnerException);
else if(stepType==“When”)
_scenario.CreateNode(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.InnerException);
else if(stepType==“Then”)
{
字符串Runname=screenshot();
_场景.AddScreenCaptureFromPath(“C:\\Users\\xxxx-PC\\Desktop\\XUnit\u Assignment\u 3\\XUnit\u Assignment\\target\\ErrorScreenshots\\”
+Runname+“.Png”);
_scenario.CreateNode(ScenarioStepContext.Current.StepInfo.Text)。
失败(ScenarioContext.Current.TestError.Message);
_scenario.Fail(“由于某些问题而失败”,
MediaEntityBuilder.CreateScreateScreenCaptureFromPath(@“C:\Users\xxxx-PC\Desktop\XUnit\u Assignment\u 3\XUnit\u Assignment\TestResults\image.JPG”,
“失败”).Build());
}
}

使用try/catch时会发生什么:

它与log4net无关,但与您捕获异常有关。 如果不这样做,异常将通过堆栈传递,直到有东西捕获它或它最终进入全局处理程序。 如果您在代码中捕获它,它将停止在那里。它将被视为“已处理”,并且控制流在捕获块之后继续移动

因此,您的报表引擎没有“看到”异常,因为您在它到达引擎将捕获它的位置之前捕获了它

如何让引擎意识到异常:

您需要重新显示异常:

catch(Exception ex){
    Log.error("unable to click");
    Console.WriteLine(ex.StackTrace);
    throw;
}
然后它会像往常一样冒泡。捕获它会阻止它进一步冒泡-因为它预期会在catch块中得到相应的处理。如果您只是为了日志记录而使用catch块,那么您需要再次抛出相同的异常

注意:
throw;
throw ex;
之间有区别。它们将产生不同的堆栈跟踪。
throw;
将保留堆栈跟踪,而
throw ex
将重置堆栈跟踪。
(请参阅)

为什么不使用扩展报告记录它们?例如test.log(Status.Pass,“details”)