ExceptionPatternConverter.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System.Diagnostics;
  20. using System.IO;
  21. using log4net.Core;
  22. namespace log4net.Layout.Pattern
  23. {
  24. /// <summary>
  25. /// Write the exception text to the output
  26. /// </summary>
  27. /// <remarks>
  28. /// <para>
  29. /// If an exception object is stored in the logging event
  30. /// it will be rendered into the pattern output with a
  31. /// trailing newline.
  32. /// </para>
  33. /// <para>
  34. /// If there is no exception then nothing will be output
  35. /// and no trailing newline will be appended.
  36. /// It is typical to put a newline before the exception
  37. /// and to have the exception as the last data in the pattern.
  38. /// </para>
  39. /// </remarks>
  40. /// <author>Nicko Cadell</author>
  41. internal sealed class ExceptionPatternConverter : PatternLayoutConverter
  42. {
  43. /// <summary>
  44. /// Default constructor
  45. /// </summary>
  46. public ExceptionPatternConverter()
  47. {
  48. // This converter handles the exception
  49. IgnoresException = false;
  50. }
  51. /// <summary>
  52. /// Write the exception text to the output
  53. /// </summary>
  54. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  55. /// <param name="loggingEvent">the event being logged</param>
  56. /// <remarks>
  57. /// <para>
  58. /// If an exception object is stored in the logging event
  59. /// it will be rendered into the pattern output with a
  60. /// trailing newline.
  61. /// </para>
  62. /// <para>
  63. /// If there is no exception or the exception property specified
  64. /// by the Option value does not exist then nothing will be output
  65. /// and no trailing newline will be appended.
  66. /// It is typical to put a newline before the exception
  67. /// and to have the exception as the last data in the pattern.
  68. /// </para>
  69. /// <para>
  70. /// Recognized values for the Option parameter are:
  71. /// </para>
  72. /// <list type="bullet">
  73. /// <item>
  74. /// <description>Message</description>
  75. /// </item>
  76. /// <item>
  77. /// <description>Source</description>
  78. /// </item>
  79. /// <item>
  80. /// <description>StackTrace</description>
  81. /// </item>
  82. /// <item>
  83. /// <description>TargetSite</description>
  84. /// </item>
  85. /// <item>
  86. /// <description>HelpLink</description>
  87. /// </item>
  88. /// </list>
  89. /// </remarks>
  90. override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
  91. {
  92. if (loggingEvent.ExceptionObject != null && Option != null && Option.Length > 0)
  93. {
  94. switch (Option.ToLower())
  95. {
  96. case "message":
  97. WriteObject(writer, loggingEvent.Repository, loggingEvent.ExceptionObject.Message);
  98. break;
  99. #if !NETCF
  100. case "source":
  101. WriteObject(writer, loggingEvent.Repository, loggingEvent.ExceptionObject.Source);
  102. break;
  103. case "stacktrace":
  104. WriteObject(writer, loggingEvent.Repository, loggingEvent.ExceptionObject.StackTrace);
  105. break;
  106. #if !NETSTANDARD1_3
  107. case "targetsite":
  108. WriteObject(writer, loggingEvent.Repository, loggingEvent.ExceptionObject.TargetSite);
  109. break;
  110. #endif
  111. case "helplink":
  112. WriteObject(writer, loggingEvent.Repository, loggingEvent.ExceptionObject.HelpLink);
  113. break;
  114. #endif
  115. default:
  116. // do not output SystemInfo.NotAvailableText
  117. break;
  118. }
  119. }
  120. else
  121. {
  122. string exceptionString = loggingEvent.GetExceptionString();
  123. if (exceptionString != null && exceptionString.Length > 0)
  124. {
  125. writer.WriteLine(exceptionString);
  126. }
  127. else
  128. {
  129. // do not output SystemInfo.NotAvailableText
  130. }
  131. }
  132. }
  133. }
  134. }