抛出异常后如何记录细节?

时间:2013-02-15 14:40:25

标签: asp.net exception-handling

我对.net很新,所以我的知识在很多方面都非常有限。

我正在创建一个网站,并创建了一些静态方法来帮助我 - 这是一个例子:

    public static void ThrowNull<T>(this T obj, string param) where T : class
    {
        if (param.IsNullOrEmpty())
            Throw<ArgumentException>("Undefined param!");

        if (obj.IsNull())
            Throw<ArgumentNullException>(param);
    }

我在其他方法中使用它作为参数保护,调用如下:myVar.ThrowNull(“myVar”);

上面提到的Throw方法如下所示:

    static void Throw<E>(string message) where E : Exception
    {
        throw Activator.CreateInstance(typeof(E), message) as E;
    }

这一切都非常适合测试,但我希望能够记录用户发生的详细信息。如何从这一点获得堆栈跟踪信息?

任何建议表示赞赏。

1 个答案:

答案 0 :(得分:0)

Exception newException = Activator.CreateInstance(typeof(E), message) as E;
<Something?>newException.StackTrace
throw newException;
相关问题