ExceptionLogEntry.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #region --------------- Copyright Dresser Wayne Pignone -------------
  2. /*
  3. * $Log: /Wrk/WayneLibraries/Wrk/Log/ExceptionLogEntry.cs $
  4. *
  5. * 3 08-02-13 9:26 Mattias.larsson
  6. * FxCop fixes.
  7. */
  8. #endregion
  9. using System;
  10. using System.Text;
  11. using System.Diagnostics.CodeAnalysis;
  12. namespace Wayne.Lib.Log
  13. {
  14. /// <summary>
  15. /// Log entry for exception errors.
  16. /// </summary>
  17. public class ExceptionLogEntry : ErrorLogEntry
  18. {
  19. #region Fields
  20. private Exception exception;
  21. #endregion
  22. #region Construction
  23. /// <summary>
  24. /// Constructor.
  25. /// </summary>
  26. /// <param name="entity"></param>
  27. /// <param name="severity"></param>
  28. /// <param name="logObject">The object to log.</param>
  29. /// <param name="exception"></param>
  30. [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "object")]
  31. public ExceptionLogEntry(IIdentifiableEntity entity, ErrorLogSeverity severity, object logObject, Exception exception)
  32. : base(entity, severity, logObject)
  33. {
  34. this.exception = exception;
  35. }
  36. /// <summary>
  37. /// Constructor.
  38. /// </summary>
  39. /// <param name="entity"></param>
  40. /// <param name="severity"></param>
  41. /// <param name="logObject">The object to log.</param>
  42. /// <param name="category"></param>
  43. /// <param name="exception"></param>
  44. [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "object")]
  45. public ExceptionLogEntry(IIdentifiableEntity entity, ErrorLogSeverity severity, object logObject, object category, Exception exception)
  46. : base(entity, severity, logObject, category)
  47. {
  48. this.exception = exception;
  49. }
  50. #endregion
  51. #region Properties
  52. /// <summary>
  53. /// Exception information.
  54. /// </summary>
  55. public Exception Exception
  56. {
  57. get { return exception; }
  58. }
  59. #endregion
  60. #region Methods: AppendTextToStringBuilder
  61. /// <summary>
  62. /// Appends the object to log to a StringBuilder-output.
  63. /// </summary>
  64. /// <param name="logWriter">The logwriter to be used for logging.</param>
  65. /// <param name="output">The StringBuilder.</param>
  66. /// <param name="indentLength">The indent to be used if many lines.</param>
  67. /// <param name="isFirstLine">Is this the first line to log?</param>
  68. /// <param name="indent">A string holding a generated indent-text (=a number of spaces). Use AppendIndent() to append the indent.</param>
  69. internal override void AppendToStringBuilder(LogWriter logWriter, StringBuilder output,
  70. int indentLength, ref bool isFirstLine, ref string indent)
  71. {
  72. base.AppendToStringBuilder(logWriter, output, indentLength, ref isFirstLine, ref indent);
  73. // Add the exception.
  74. if (exception != null)
  75. {
  76. StringLogObject.EnsureIndent(indentLength, ref indent);
  77. string headerText = "Exception: ";
  78. Exception ex = exception;
  79. while (ex != null)
  80. {
  81. StringLogObject.AppendStringToStringBuilder(headerText + ex.ToString(), output, logWriter, indentLength, ref isFirstLine, ref indent);
  82. // Check if there is an inner exception
  83. ex = ex.InnerException;
  84. if (ex != null)
  85. {
  86. // Indent 3 more spaces.
  87. indent += " ";
  88. headerText = "Inner Exception: ";
  89. }
  90. }
  91. //StringLogObject.AppendStringToStringBuilder("Exception: ", output, logWriter, indentLength, true, ref indent);
  92. //string newLineString = "\r\n" + Wayne.Lib.Strings.Indent(indentLength, true);
  93. //output.Append(newLineString);
  94. //output.Append("Exception: ");
  95. //Exception ex = exception;
  96. //while (ex != null)
  97. //{
  98. // StringLogObject.AppendObjectToStringBuilder(ex.Message, output, logWriter, indentLength, false, ref indent);
  99. // // Check if there is an inner exception
  100. // ex = ex.InnerException;
  101. // if (ex != null)
  102. // {
  103. // // Indent 3 more spaces.
  104. // newLineString += " ";
  105. // indentLength += 3;
  106. // output.Append(newLineString);
  107. // output.Append("Inner Exception: ");
  108. // }
  109. //}
  110. }
  111. }
  112. #endregion
  113. }
  114. }