WCF故障异常行号

时间:2015-05-14 09:52:39

标签: c# wcf

我试图在WCF服务中获取完整的异常详细信息,但是,我没有得到发生异常的行号。

try
{
    ...
}
catch (Exception e)
{
    throw new FaultException(e.ToString());
}

我尝试了各种各样的方法,例如没有运气就返回e.StackTrace等。请帮助我如何获得发生异常的行号。

3 个答案:

答案 0 :(得分:0)

使用:

    catch (Exception e)
    {
       // Get stack trace for the exception with source file information
        var st = new StackTrace(e, true);
       // Get the top stack frame
       var frame = st.GetFrame(0);
       // Get the line number from the stack frame
       var line = frame.GetFileLineNumber();
}

它会为您提供所需的信息。

答案 1 :(得分:0)

此代码将为堆栈跟踪提供行号

try
{
    ///...
}
catch (Exception e)
{
    throw new FaultException<Exception>(ex, ex.Message);
}

答案 2 :(得分:0)

我会说你必须删除整个try catch块:

更好的方法就是评论它:

//try
//{
      Code.DoSomething(); //of course this code line is only a example and not working

      /*the code inside the try block should now stop on the line, where the error appears*/
      // might look like this: http://www.homeandlearn.co.uk/csharp/images/conditional_logic/Error_Parse.gif
//}
//catch (Exception e)
//{
      //throw new FaultException(e.ToString());
//}    

你应该得到喜欢这个:(根据这个我在Google上找到的唯一图片)

error window

相关问题