2013-02-15 51 views
-1

我對.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; 
    } 

這一切的偉大工程的測試,但我希望能夠記錄從用戶發生的細節。從這一點我如何獲得堆棧跟蹤信息?

任何意見讚賞。

+0

爲什麼不能使用日誌框架? Log4Net,ELMAH – 2013-02-15 14:43:21

+0

我同意Ravi,你應該檢查Log4Net。它記錄你想要的一切。 – Kadir 2013-02-15 14:46:46

+0

我不知道爲什麼這已被低估。這不是重複的,因爲我沒有問如何執行錯誤處理,我特別問了我是如何從我的代碼中得到堆棧跟蹤的。 – dotnetnoob 2013-02-15 15:40:39

回答

0
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