1

log4netとロギング例外を使用しています。しかし、例外時間に現在のオブジェクトをログに記録したいのですが、できませんでした。例外を作成し、例外のデータプロパティにオブジェクトを追加してから、log4netで例外をログに記録します。log4netの例外メッセージに私のオブジェクトが含まれていません。

次のような私のコード。

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

                }

私のviewModelオブジェクト;

 [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

オブジェクトを使用してカスタム例外をログに記録するにはどうすればよいですか?

4

1 に答える 1

0

わかりました、質問があれば。私は似たようなことをします。私はロガーを使用して例外を記述します。しかし、私はException.Dataプロパティを使用します。

これが例です。例には1)記述が必要なInfoクラスが含まれています2)メソッドを使用したサンプルクラス、例外が発生したときにinfoclassを記述します3)例外をフォーマットするユーティリティクラス

 [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
                    }
    }
}

//今、私はこのユーティリティメソッドを使用して、非常にトップレベルの例外ですべてを書き出します

 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();

    }
}
于 2013-02-21T14:56:03.667 に答える