c# - 方法未显示在生产中的堆栈跟踪中

标签 c# extension-methods stack-trace

我有一个像这样的方法调用序列:

//This is the Load method
BusinessLogic.Models.ProjectEngineer p = new BusinessLogic.Models.ProjectEngineer(project);
p.IsQualifiedFor(enType, educationEnum);
.
.
.


[UsesRule]
public static void IsQualifiedFor(this Models.ProjectEngineer p, EngineerTypeEnum enType, FieldEducationEnum fieldEducation)
{
    RuleCache.EvaluateRule(p, enType, fieldEducation);
}

.
.
.
public static bool EvaluateRule<TClass>(TClass sourceObj, params object[] parameters)
{
        var frame = new StackTrace().GetFrame(1);
        var methodInfo = frame.GetMethod();
        var atr = methodInfo.GetCustomAttribute<UsesRuleAttribute>();
        if (atr == null)
            throw new EISException("You can't call 'EvaluateRule' method in a method that does not have 'UsesRuleAttribute'.");
    ...
}

它在生产服务器中运行良好,直到今天我更改了一些不相关的内容。现在,EvaluateRule 方法抛出异常,因为它在调用者中找不到 UsesRuleAttribute。这是异常的堆栈跟踪:

BusinessLogic.EISException: You can't call 'EvaluateRule' method in a method that does not have 'UsesRuleAttribute'.
   at BusinessLogic.Rule.RuleCache.EvaluateRule[TClass](TClass sourceObj, Object[] parameters) in e:\EIS\EISMvc\BusinessLogic\Rule\RuleCache.cs:line 146
   at EIS.Quota.Owner.EngineerList.Load() in e:\EIS\EISMvc\EIS\Quota\Owner\EngineerList.aspx.cs:line 66

如您所见,此处缺少 IsQualifiedFor 方法调用(它是一个扩展方法)。当我在我的机器上调试项目时,它工作正常。这是某种优化还是什么?

最佳答案

很可能是inlined由 JIT(即时编译器)处理,即在生成的 native 代码中被其主体替换,因此它不会出现在堆栈中。

您可以使用 MethodImpl 属性来防止内联:

[UsesRule]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void IsQualifiedFor(this Models.ProjectEngineer p, EngineerTypeEnum enType, FieldEducationEnum fieldEducation)

关于c# - 方法未显示在生产中的堆栈跟踪中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23007286/

相关文章:

c# - 如何知道哪些设备连接到我的网络(使用 C#)?

c# - 在 Visual Studio 2017 中生成 .NET Core 控制台应用程序 .exe

Angular2 和 typescript : How to add string extension method/prototype

c# - 扩展基类方法

来自未处理异常的 C++ 堆栈跟踪?

c# - StackTrace 文件名未知

c# - 如何实例化 DataReceivedEventArgs 或者能够用数据填充它?

c# - 向现有系统类添加对象

javascript - 从 NodeJS 中抛出的字符串获取堆栈跟踪

c# - Foreach 仅适用于 ListBox 的第一项