gpt4 book ai didi

c# - 关于异常详细信息的问题 c#

转载 作者:太空宇宙 更新时间:2023-11-03 20:18:32 25 4
gpt4 key购买 nike

我试图用下面的代码捕获异常细节,但它没有给出细节错误。

我试图从我们的 ftp 服务器下载一个文件,其中包含类似 ftp 类的代码。当找不到文件时,我触发上述方法并传入异常。不幸的是,该方法打印了以下内容:

Error occured and Error details as follows
Exception occured in file
Exception occured in method SyncRequestCallback
Exception occured in line & column number 0 0

这是我的代码。如何捕获异常详细信息(例如行号、引发异常的方法等)?

public static string GetException(Exception ex)
{
//Get a StackTrace object for the exception
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();

string strBody = "Hi,<br/><br/>";
strBody = strBody + "Error occured and Error details as follows<br/>";
strBody = strBody + "Error message " + ex.Message + "<br/>";
strBody = strBody + "Exception occured in file " + fileName + "<br/>";
strBody = strBody + "Exception occured in method " + methodName + "<br/>";
strBody = strBody + "Exception occured in line & column number " + line + " " + col + "<br/><br/>Thanks";
return strBody;
}

谢谢

最佳答案

为了捕获更多信息,您需要部署构建时生成的 .pdb 文件。这将使您知道发生错误的具体行。

附带说明一下,在我看来,您正在重新发明轮子来创建如此复杂的日志逻辑。有很多框架可以为您完成肮脏的工作 log4net .

关于c# - 关于异常详细信息的问题 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15137885/

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