2013-02-15 49 views
1

我正在使用log4net和日誌記錄異常。但我想記錄當前對象的例外時間,但我不能。 我創建了異常並將對象添加到異常的數據屬性,然後通過log4net記錄異常。 log4net的異常消息不包含我的對象。Log4Net不記錄自定義異常正確

我的代碼如;

try 
       { 
        Exception exp = new Exception("critical error"); 
        exp.Data.Add("Eror Object", viewModel); 
        throw exp; 
       } 
       catch (Exception exp) 
       { 
        _logManager.Error(exp); 

       } 

my viewModel object;

[Serializable] 
    public class CartTransferViewModel 
    { 
     public CartTransferViewModel() 
     { 
      Model = new ModelObject(); 
     } 
     public ModelObject Model { get; set; } 
     public string InformatinMessage { get; set; } 
     public bool? IsActive { get; set; } 
    } 

而且我的Model對象也是可序列化的。但log4Net的異常消息是這樣的;

System.Exception: critical error 
    at FooProject.FooClass.FooMethod() in d:\FooProject\FooClass.cs:line 200 

我刪除可序列化的屬性,然後重新運行我的應用程序,錯誤代碼確實更改爲;

System.ArgumentException: Argument passed in is not serializable. 
Parameter name: value 
    at System.Collections.ListDictionaryInternal.Add(Object key, Object value) 
    at FooProject.FooClass.FooMethod() in d:\FooProject\FooClass.cs:line 200 

如何使用我的對象記錄我的自定義異常?

回答

0

好的,如果我有你的問題。我做了類似的事情。我使用記錄器來編寫例外。但我使用Exception.Data屬性。

這裏的例子中,例如包含 1)信息類,它需要被寫入與方法 2)樣品類,當發生異常 哪些格式的例外

[Serializable] 
    public class FlatFileItem 
    { 
     ArrayList errorlist = new ArrayList(); 

     public FlatFileItem() 
     { 
      if (errorlist == null) { errorlist = new ArrayList(); } 
     } 

     //Name of the file 
     public string FileName { get; set; } 
     public override string ToString() 
     { 
      return string.Format(@"FlatFileItem (Unzip FTPLineItem) => FileName:{0}", this.FileName); 
     } 
    } 


public class someclass { 
    public void somemethod(){ 
     try{ 
      // throw exception here 
     } catch (Exception ex) 
        { 
         ex.Data["flatfile"] = Convert.ToString(flatfile); //Using data property 
         flatfile.HasErrors = true; //not there in above example 
         flatfile.Parent.AddErrorInfo(ex); //not there in above example 
         logger.Error(String.Format(ex.Message)); //not there in above example 

         throw (new Exception ("yourmsg",ex)); //if you want to do this 
        } 
    } 
} 
3)實用類寫infoclass

//現在我用這個實用方法寫出非常頂級的一切異常

public class ExceptionInfoUtil 
{ 
    public static string GetAllExceptionInfo(Exception ex) 
    { 
     StringBuilder sbexception = new StringBuilder(); 

     int i = 1; 
     sbexception.Append(GetExceptionInfo(ex, i)); 

     while (ex.InnerException != null) 
     { 
      i++; 
      ex = ex.InnerException; 
      sbexception.Append(GetExceptionInfo(ex, i)); 
     } 

     return sbexception.ToString(); 
    } 

    private static string GetExceptionInfo(Exception ex, int count) 
    { 
     StringBuilder sbexception = new StringBuilder(); 
     sbexception.AppendLine(string.Format("")); 
     sbexception.AppendLine(string.Format("")); 
     sbexception.AppendLine(string.Format("************************************************")); 
     sbexception.AppendLine(string.Format("************************************************")); 
     sbexception.AppendLine(string.Format(" Inner Exception : No.{0} ", count)); 
     sbexception.AppendLine(string.Format("************************************************")); 
     sbexception.AppendLine(string.Format("==================================================")); 
     sbexception.AppendLine(string.Format(" Error Message : {0} ", ex.Message)); 
     sbexception.AppendLine(string.Format("==================================================")); 
     #region Mine Thru data dictionary 

     try 
     { 
      sbexception.AppendLine(string.Format("==================================================")); 
      sbexception.AppendLine(string.Format(" Data parameters Count at Source :{0}", ex.Data.Count)); 
      sbexception.AppendLine(string.Format("==================================================")); 

      string skey = string.Empty; 
      foreach (object key in ex.Data.Keys) 
      { 
       try 
       { 
        if (key != null) 
        { 
         skey = Convert.ToString(key); 
         sbexception.AppendLine(string.Format(" Key :{0} , Value:{1}", skey, Convert.ToString(ex.Data[key]))); 
        } 
        else 
        { 
         sbexception.AppendLine(string.Format(" Key is null")); 
        } 
       } 
       catch (Exception e1) 
       { 
        sbexception.AppendLine(string.Format("** Exception occurred when writting log *** [{0}] ", e1.Message)); 
       } 
      } 
     } 
     catch (Exception ex1) 
     { 
      sbexception.AppendLine(string.Format("** Exception occurred when writting log *** [{0}] ", ex1.Message)); 
     } 

     #endregion 
     sbexception.AppendLine(string.Format("==================================================")); 
     sbexception.AppendLine(string.Format(" Source : {0} ", ex.Source)); 
     sbexception.AppendLine(string.Format("==================================================")); 
     sbexception.AppendLine(string.Format(" StackTrace : {0} ", ex.StackTrace)); 
     sbexception.AppendLine(string.Format("==================================================")); 
     sbexception.AppendLine(string.Format(" TargetSite : {0} ", ex.TargetSite)); 
     sbexception.AppendLine(string.Format("************************************************")); 
     sbexception.AppendLine(string.Format(" Finished Writting Exception info :{0} ", count)); 
     sbexception.AppendLine(string.Format("************************************************")); 
     sbexception.AppendLine(string.Format("************************************************")); 
     sbexception.AppendLine(string.Format("")); 
     sbexception.AppendLine(string.Format("")); 

     return sbexception.ToString(); 

    } 
}