ExceptionDetail.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System;
  7. using System.Globalization;
  8. using System.Text;
  9. using System.Runtime.Serialization;
  10. [DataContract]
  11. public class ExceptionDetail
  12. {
  13. string helpLink;
  14. ExceptionDetail innerException;
  15. string message;
  16. string stackTrace;
  17. string type;
  18. public ExceptionDetail(Exception exception)
  19. {
  20. if (exception == null)
  21. {
  22. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("exception");
  23. }
  24. this.helpLink = exception.HelpLink;
  25. this.message = exception.Message;
  26. this.stackTrace = exception.StackTrace;
  27. this.type = exception.GetType().ToString();
  28. if (exception.InnerException != null)
  29. {
  30. this.innerException = new ExceptionDetail(exception.InnerException);
  31. }
  32. }
  33. [DataMember]
  34. public string HelpLink
  35. {
  36. get { return this.helpLink; }
  37. set { this.helpLink = value; }
  38. }
  39. [DataMember]
  40. public ExceptionDetail InnerException
  41. {
  42. get { return this.innerException; }
  43. set { this.innerException = value; }
  44. }
  45. [DataMember]
  46. public string Message
  47. {
  48. get { return this.message; }
  49. set { this.message = value; }
  50. }
  51. [DataMember]
  52. public string StackTrace
  53. {
  54. get { return this.stackTrace; }
  55. set { this.stackTrace = value; }
  56. }
  57. [DataMember]
  58. public string Type
  59. {
  60. get { return this.type; }
  61. set { this.type = value; }
  62. }
  63. public override string ToString()
  64. {
  65. return string.Format(CultureInfo.InvariantCulture, "{0}\n{1}", SR.GetString(SR.SFxExceptionDetailFormat), this.ToStringHelper(false));
  66. }
  67. string ToStringHelper(bool isInner)
  68. {
  69. StringBuilder sb = new StringBuilder();
  70. sb.AppendFormat("{0}: {1}", this.Type, this.Message);
  71. if (this.InnerException != null)
  72. {
  73. sb.AppendFormat(" ----> {0}", this.InnerException.ToStringHelper(true));
  74. }
  75. else
  76. {
  77. sb.Append("\n");
  78. }
  79. sb.Append(this.StackTrace);
  80. if (isInner)
  81. {
  82. sb.AppendFormat("\n {0}\n", SR.GetString(SR.SFxExceptionDetailEndOfInner));
  83. }
  84. return sb.ToString();
  85. }
  86. }
  87. }