如何在抛出异常后记录详细信息?
问题描述:
我对.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”);
上面提到的投掷方法是这样的:
static void Throw<E>(string message) where E : Exception
{
throw Activator.CreateInstance(typeof(E), message) as E;
}
这一切的伟大工程的测试,但我希望能够记录从用户发生的细节。从这一点我如何获得堆栈跟踪信息?
任何意见赞赏。
答
Exception newException = Activator.CreateInstance(typeof(E), message) as E;
<Something?>newException.StackTrace
throw newException;
+0
Something是指什么? – dotnetnoob 2013-02-15 15:38:59
+0
无论你想用堆栈跟踪...(这是一个字符串顺便说一句) – 2013-02-15 16:14:40
为什么不能使用日志框架? Log4Net,ELMAH – 2013-02-15 14:43:21
我同意Ravi,你应该检查Log4Net。它记录你想要的一切。 – Kadir 2013-02-15 14:46:46
我不知道为什么这已被低估。这不是重复的,因为我没有问如何执行错误处理,我特别问了我是如何从我的代码中得到堆栈跟踪的。 – dotnetnoob 2013-02-15 15:40:39