c# - StackTrace 在发布模式下返回错误的异常行号
问题描述
我有这样的User
课:
public class User
{
public string Name { get; set; }
public string Family { get; set; }
public string Type { get; set; }
}
和Program.cs
文件:
public static void Main()
{
try
{
List<User> users = new List<User>();
users.Add(new User
{
Family = "Zamani",
Name = "Farhad",
Type = "ADY"
});
DoSomething(users);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
public static void DoSomething(List<User> users)
{
var adult = users.Where(a => // line 36
a.Name.ToUpperInvariant() == "Farhad".ToUpperInvariant()
&& a.Family.ToUpperInvariant() == "zam".ToUpperInvariant()
).FirstOrDefault();
(string name, string type) = GetPassengerInfo(GetType(adult.Type), "farhad");//line 41
}
private static (string name, string type) GetPassengerInfo(string v, string d)
{
return (v, d);
}
static string GetType(string type)
{
string result = string.Empty;
switch (type)
{
case "ADT":
result = "Adult";
break;
}
return result;
}
当我在调试模式下运行程序时,Stacktrace
异常显示Line:41
,没关系。
但是当我通过发布模式运行项目时,我在Stacktrace
.
在发布模式下它显示Line:36
.
为什么?
解决方案
为什么?
因为在发布模式下,优化器可以内联函数并执行其他更改源代码中实际行号的事情。
仅输出堆栈跟踪可能没有帮助(恕我直言)。你得到了什么类型的异常?错误信息是什么?它起源于什么方法?根据我的经验,这些对诊断问题更有帮助。我会将您的异常处理更改为至少Console.WriteLine(ex)
. 这将为您提供所有这些数据以及堆栈跟踪。
此链接可能会有所帮助。
推荐阅读
- javascript - Webpack 别名工作,但 Typescript 给我错误
- c# - 在除 Swagger 之外的所有服务器上从 IIS 接收 401.1 Unauthorized
- function - 在lua中,有前向声明吗?
- python - 无法在 Python 中使用“Strip”从字符串中消除字符串
- uwp - 如何在 UWP 中为不同的颜色主题设置不同的图标
- flutter - 如何在 Flutter App 的文本框右侧添加美元符号
- amazon-web-services - 使用 QoS.EXACTLY_ONCE 订阅主题时使用 awsiotsdk,遇到 AWS_ERROR_MQTT_UNEXPECTED_HANGUP
- javascript - 如何检查是否使用 Jasmine 中的特定选择器调用了 jQuery、$?
- curl - 在 WSO2 API 管理器中使用 POST 请求时出现错误 411
- wordpress - WP_Query 传递一个唯一标识符以用于稍后的操作挂钩?