gpt4 book ai didi

c# - 获取源文件、方法和异常行

转载 作者:太空狗 更新时间:2023-10-29 17:51:59 28 4
gpt4 key购买 nike

我正在尝试从我的 c# (4.0) asp.net web 应用程序中发生的异常中获取详细信息。我在 bin 文件夹中有 .pdb 文件。以下代码未按预期工作 -

protected void Button1_Click(object sender, EventArgs e)
{
try
{
//throwing Exception
using (SqlConnection connection=new SqlConnection(""))
{
connection.Open();
}
}
catch (Exception exception)
{
//Get a StackTrace object for the exception
StackTrace st = new StackTrace(exception, true);

//Get the first stack frame
StackFrame frame = st.GetFrame(0); //returns {PermissionDemand at offset 5316022 in file:line:column <filename unknown>:0:0}

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

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

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

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

这里有什么问题吗?

更新: “StackFrame frame = st.GetFrame(st.FrameCount - 1);”解决了这个问题。

最佳答案

//Remove this line
StackFrame frame = st.GetFrame(0);

//Add this line
StackFrame frame = st.GetFrame(st.FrameCount - 1);

关于c# - 获取源文件、方法和异常行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19264135/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com