Exception.Message vs Exception.ToString()

后端 未结 7 893
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 06:44

I have code that is logging Exception.Message. However, I read an article which states that it\'s better to use Exception.ToString(). With the latt

7条回答
  •  盖世英雄少女心
    2020-12-04 07:26

    Depends on the information you need. For debugging the stack trace & inner exception are useful:

        string message =
            "Exception type " + ex.GetType() + Environment.NewLine +
            "Exception message: " + ex.Message + Environment.NewLine +
            "Stack trace: " + ex.StackTrace + Environment.NewLine;
        if (ex.InnerException != null)
        {
            message += "---BEGIN InnerException--- " + Environment.NewLine +
                       "Exception type " + ex.InnerException.GetType() + Environment.NewLine +
                       "Exception message: " + ex.InnerException.Message + Environment.NewLine +
                       "Stack trace: " + ex.InnerException.StackTrace + Environment.NewLine +
                       "---END Inner Exception";
        }
    

提交回复
热议问题