Qua*_*ter 258
如果您需要的行号不仅仅是从Exception.StackTrace获得的格式化堆栈跟踪,您可以使用StackTrace类:
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();
}
Run Code Online (Sandbox Code Playgroud)
请注意,只有在可用于程序集的pdb文件时,这才有效.
Sim*_*ect 66
简单的方法,使用该Exception.ToString()
函数,它将在异常描述后返回行.
您还可以检查程序调试数据库,因为它包含有关整个应用程序的调试信息/日志.
rad*_*byx 25
如果您没有该.PBO
文件:
C#
public int GetLineNumber(Exception ex)
{
var lineNumber = 0;
const string lineSearch = ":line ";
var index = ex.StackTrace.LastIndexOf(lineSearch);
if (index != -1)
{
var lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length);
if (int.TryParse(lineNumberText, out lineNumber))
{
}
}
return lineNumber;
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
或者作为Exception类的扩展
public static class MyExtensions
{
public static int LineNumber(this Exception ex)
{
var lineNumber = 0;
const string lineSearch = ":line ";
var index = ex.StackTrace.LastIndexOf(lineSearch);
if (index != -1)
{
var lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length);
if (int.TryParse(lineNumberText, out lineNumber))
{
}
}
return lineNumber;
}
}
Run Code Online (Sandbox Code Playgroud)
有用:
var LineNumber = new StackTrace(ex, True).GetFrame(0).GetFileLineNumber();
Run Code Online (Sandbox Code Playgroud)
小智 5
检查这个
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();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
180313 次 |
最近记录: |