Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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# 如何获取引发异常的行号?_C#_Exception - Fatal编程技术网

C# 如何获取引发异常的行号?

C# 如何获取引发异常的行号?,c#,exception,C#,Exception,在catch块中,如何获取引发异常的行号?您可以包括与包含元数据信息的程序集关联的.PDB符号文件,当引发异常时,它将在堆栈跟踪中包含此异常起源的完整信息。它将包含堆栈中每个方法的行号。您可以包括与包含元数据信息的程序集关联的.PDB符号文件,当引发异常时,它将在堆栈跟踪中包含此异常起源的完整信息。它将包含堆栈中每个方法的行号。简单地说,使用Exception.ToString()函数,它将返回异常描述后的行 您还可以检查程序调试数据库,因为它包含有关整个应用程序的调试信息/日志。简单的方法是,

catch
块中,如何获取引发异常的行号?

您可以包括与包含元数据信息的程序集关联的
.PDB
符号文件,当引发异常时,它将在堆栈跟踪中包含此异常起源的完整信息。它将包含堆栈中每个方法的行号。

您可以包括与包含元数据信息的程序集关联的
.PDB
符号文件,当引发异常时,它将在堆栈跟踪中包含此异常起源的完整信息。它将包含堆栈中每个方法的行号。

简单地说,使用
Exception.ToString()
函数,它将返回异常描述后的行


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

简单的方法是,使用
Exception.ToString()
函数,它将返回异常描述后的行


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

如果您需要的行号不仅仅是从Exception.StackTrace获得的格式化堆栈跟踪,您可以使用类:


请注意,这仅在有可用于部件的pdb文件时才起作用

如果您需要的行号不仅仅是从Exception.StackTrace获得的格式化堆栈跟踪,您可以使用以下类:


请注意,这仅在有可用于部件的pdb文件时才起作用

在Global.resx文件中有一个名为Application\u Error的事件

它会在发生错误时激发,您可以轻松获取有关错误的任何信息,并将其发送到错误跟踪电子邮件


另外,我认为你需要做的就是编译global.resx并将其dll(2个dll)添加到你的bin文件夹中,它就会工作

在Global.resx文件中有一个名为Application\u Error的事件

它会在发生错误时激发,您可以轻松获取有关错误的任何信息,并将其发送到错误跟踪电子邮件


另外,我认为你需要做的就是编译global.resx并将其dll(2个dll)添加到你的bin文件夹中,它就会工作

如果您没有
.PBO
文件:

C#

Vb.net

Public Function GetLineNumber(ByVal ex As Exception)
    Dim lineNumber As Int32 = 0
    Const lineSearch As String = ":line "
    Dim index = ex.StackTrace.LastIndexOf(lineSearch)
    If index <> -1 Then
        Dim lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length)
        If Int32.TryParse(lineNumberText, lineNumber) Then
        End If
    End If
    Return lineNumber
End Function

如果您没有
.PBO
文件:

C#

Vb.net

Public Function GetLineNumber(ByVal ex As Exception)
    Dim lineNumber As Int32 = 0
    Const lineSearch As String = ":line "
    Dim index = ex.StackTrace.LastIndexOf(lineSearch)
    If index <> -1 Then
        Dim lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length)
        If Int32.TryParse(lineNumberText, lineNumber) Then
        End If
    End If
    Return lineNumber
End Function
更新答案

    // 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();
更新答案

    // 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();
检查这个

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();
检查这个

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();

我尝试使用@davy-c的解决方案,但出现了一个异常“System.FormatException:“输入字符串的格式不正确”。”,这是因为仍然有超过行号的文本,我修改了他发布的代码并提出:

int line = Convert.ToInt32(objErr.ToString().Substring(objErr.ToString().IndexOf("line")).Substring(0, objErr.ToString().Substring(objErr.ToString().IndexOf("line")).ToString().IndexOf("\r\n")).Replace("line ", ""));

这在VS2017 C#中对我有效。

我尝试使用@davy-C的解决方案,但出现了一个异常“System.FormatException:“输入字符串的格式不正确”。”,这是因为仍然有超过行号的文本,我修改了他发布的代码,并得出了以下结论:

int line = Convert.ToInt32(objErr.ToString().Substring(objErr.ToString().IndexOf("line")).Substring(0, objErr.ToString().Substring(objErr.ToString().IndexOf("line")).ToString().IndexOf("\r\n")).Replace("line ", ""));
这在VS2017 C#扩展方法中适用 用法 扩展方法 用法 为我工作:

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();
为我工作:

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();

我为Exception添加了一个扩展名,它返回行、列、方法、文件名和消息:

public static class Extensions
{
    public static string ExceptionInfo(this Exception exception)
    {

        StackFrame stackFrame = (new StackTrace(exception, true)).GetFrame(0);
        return string.Format("At line {0} column {1} in {2}: {3} {4}{3}{5}  ",
           stackFrame.GetFileLineNumber(), stackFrame.GetFileColumnNumber(),
           stackFrame.GetMethod(), Environment.NewLine, stackFrame.GetFileName(),
           exception.Message);

    }
}

我为Exception添加了一个扩展名,它返回行、列、方法、文件名和消息:

public static class Extensions
{
    public static string ExceptionInfo(this Exception exception)
    {

        StackFrame stackFrame = (new StackTrace(exception, true)).GetFrame(0);
        return string.Format("At line {0} column {1} in {2}: {3} {4}{3}{5}  ",
           stackFrame.GetFileLineNumber(), stackFrame.GetFileColumnNumber(),
           stackFrame.GetMethod(), Environment.NewLine, stackFrame.GetFileName(),
           exception.Message);

    }
}

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

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


真的
嵌入的
上面的配置将包括直接与生成文件一起使用的调试符号,这些文件可以作为NUGET发布

上述方法的一种替代方法是将调试包与主nuget包一起还原,这目前尚不受支持:

现在获取异常行号:

试试看
{
抛出新异常();
}
捕获(例外情况除外)
{
//使用源文件信息获取异常的堆栈跟踪
var st=新堆栈跟踪(ex,true);
//获取顶部堆栈帧
var frame=st.GetFrame(0);
//从堆栈帧中获取行号
var line=frame.GetFileLineNumber();
}

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

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


真的
嵌入的
上面的配置将包括直接与生成文件一起使用的调试符号,这些文件可以作为NUGET发布

上述方法的一种替代方法是将调试包与主nuget包一起还原,这目前尚不受支持:

现在获取异常行号:

试试看
{
抛出新异常();
}
捕获(例外情况除外)
{
//使用源文件信息获取异常的堆栈跟踪
var st=新堆栈跟踪(ex,true);
//获取顶部堆栈帧
var frame=st.GetFrame(0);
//从堆栈帧中获取行号
var line=frame.GetFileLineNumber();
}

运行时没有源代码。这行代码的用途是什么?在调试时,IDE清楚地显示抛出异常的行。如果没有IDE,@ankitjaininfo的可能重复项的可能重复项没有帮助!这回答了你的问题吗?在运行时没有源代码。这行代码的用途是什么?在调试时,IDE清楚地显示抛出异常的行。可能重复的@ankitjaininfo的可能重复如果没有