在捕获块中,如何获得抛出异常的行号?


当前回答

简单的方法,使用exception . tostring()函数,它将返回异常描述之后的行。

您还可以检查程序调试数据库,因为它包含关于整个应用程序的调试信息/日志。

其他回答

为我工作:

var st = new StackTrace(e, true);

// Get the bottom stack frame
var frame = st.GetFrame(st.FrameCount - 1);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
var method = frame.GetMethod().ReflectedType.FullName;
var path = frame.GetFileName();

看看这个

StackTrace st = new StackTrace(ex, true);
//Get the first stack frame
StackFrame frame = st.GetFrame(0);

//Get the file name
string fileName = frame.GetFileName();

//Get the method name
string methodName = frame.GetMethod().Name;

//Get the line number from the stack frame
int line = frame.GetFileLineNumber();

//Get the column number
int col = frame.GetFileColumnNumber();

如果生成异常的库是用调试符号编译的,那么行号将包含在堆栈跟踪中。这可以是一个单独的文件(*.pdb),也可以嵌入到库中。

对于.NET Core、.NET 5及更高版本,要在发布版本中拥有完整的异常行号,请按如下方式配置项目:

<PropertyGroup>    
  <DebugSymbols>true</DebugSymbols>
  <DebugType>embedded</DebugType>

    <!-- Only enable the following if the line numbers mismatch -->
    <!--<Optimize>false</Optimize>-->
    
    <!--
      Additional properties which may impact how printed line numbers match the source code line numbers are listed here:
      https://learn.microsoft.com/en-us/dotnet/core/run-time-config/compilation
    -->
</PropertyGroup>

上面的配置将在构建的文件中直接包含调试符号,这些符号可以作为块发布。

上面的替代方法是将调试包与主要的nuget包一起恢复,目前还不支持:https://github.com/NuGet/Home/issues/9667

现在获取异常行号:

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

更新到答案

    // Get stack trace for the exception with source file information
    var st = new StackTrace(ex, true);
    // Get the top stack frame
    var frame = st.GetFrame(st.FrameCount-1);
    // Get the line number from the stack frame
    var line = frame.GetFileLineNumber();

在全球。resx文件中有一个名为Application_Error的事件

每当发生错误时,它就会触发,您可以很容易地获得有关错误的任何信息,并将其发送到错误跟踪电子邮件。

此外,我认为所有你需要做的是编译全局。Resx并将其dll's (2 dll)添加到您的bin文件夹,它将工作!